mirror of
https://github.com/sergeyyarkov/educt-server.git
synced 2026-10-11 18:29:15 +03:00
feat: return paginated users base on parameters
This commit is contained in:
parent
9238b4bd4a
commit
9eaca99e3b
@ -64,7 +64,7 @@ export default class UserRepository {
|
|||||||
*/
|
*/
|
||||||
public async getAll(params?: any) {
|
public async getAll(params?: any) {
|
||||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||||
const { search, email, login, first_name, last_name, role, page = 1, limit = 10 }: any = params || {};
|
const { search, email, login, first_name, last_name, role, page, limit }: any = params || {};
|
||||||
|
|
||||||
const query = this.User.query();
|
const query = this.User.query();
|
||||||
|
|
||||||
@ -97,8 +97,21 @@ export default class UserRepository {
|
|||||||
query.whereHas('roles', q => q.where('slug', role));
|
query.whereHas('roles', q => q.where('slug', role));
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await query.preload('contacts').preload('roles').orderBy('created_at', 'desc').paginate(page, limit);
|
query.preload('contacts').preload('roles').orderBy('created_at', 'desc');
|
||||||
return data.toJSON();
|
|
||||||
|
/**
|
||||||
|
* Return paginated users if provided params
|
||||||
|
*/
|
||||||
|
if (limit && page) {
|
||||||
|
const data = await query.paginate(page, limit);
|
||||||
|
return data.toJSON();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all users
|
||||||
|
*/
|
||||||
|
const data = await query;
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -76,16 +76,24 @@ export default class UserService {
|
|||||||
*/
|
*/
|
||||||
public async fetchUsers(params?: any): Promise<IResponse> {
|
public async fetchUsers(params?: any): Promise<IResponse> {
|
||||||
const users = await this.userRepository.getAll(params);
|
const users = await this.userRepository.getAll(params);
|
||||||
|
const result: IResponse = {
|
||||||
return {
|
|
||||||
success: true,
|
success: true,
|
||||||
status: HttpStatusEnum.OK,
|
status: HttpStatusEnum.OK,
|
||||||
message: 'Fetched all users.',
|
message: 'Fetched all users.',
|
||||||
data: users.data,
|
data: {},
|
||||||
meta: {
|
|
||||||
pagination: users.meta,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (users instanceof Array) {
|
||||||
|
result.data = users;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.data = users.data;
|
||||||
|
result.meta = {
|
||||||
|
pagination: users.meta,
|
||||||
|
};
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user