refactor: descriptions of funcs

This commit is contained in:
Sergey Yarkov 2021-08-06 19:40:11 +03:00
parent f8e3417f1f
commit 7ef12795d9
4 changed files with 61 additions and 8 deletions

View File

@ -11,7 +11,7 @@ export default class ContactRepository {
}
/**
* Creates user contacts in database
* Create user contacts in database
*
* @param user User for associate the contacts
* @param data Data contacts
@ -27,12 +27,16 @@ export default class ContactRepository {
return contacts;
}
/**
* Update user contacts
*
* @param user User
* @param data Data to update
* @returns
*/
public async update(user: User, data: Pick<UpdateUserValidator['schema']['props'], 'email'>) {
const contacts = await this.Contact.query().where('user_id', user.id).first();
/**
* Update user contacts
*/
if (contacts) {
contacts.email = data.email;

View File

@ -7,6 +7,12 @@ export default class RoleRepository {
this.Role = Role;
}
/**
* Get list of roles
*
* @param params Params to find roles
* @returns Array of roles
*/
public async getAll(params?: any) {
const { roles }: any = params || {};
const query = this.Role.query();
@ -19,8 +25,14 @@ export default class RoleRepository {
return data;
}
/**
* Get role by slug
*
* @param slug Role slug
* @returns Role or null
*/
public async getBySlug(slug: string) {
const role = await this.Role.query().where('slug', slug);
const role = await this.Role.query().where('slug', slug).first();
return role;
}
}

View File

@ -10,11 +10,23 @@ export default class UserRepository {
this.User = User;
}
/**
* Get user by id
*
* @param id User id
* @returns User or null
*/
public async getById(id: string | number) {
const data = await this.User.query().preload('contacts').preload('roles').where('id', id).first();
return data;
}
/**
* Get users by role
*
* @param role Role
* @returns Array of users with role
*/
public async getAllByRole(role: string) {
const data = await this.User.query()
.preload('contacts')
@ -23,6 +35,12 @@ export default class UserRepository {
return data;
}
/**
* Get list users
*
* @param params Params to find
* @returns Array of users
*/
public async getAll(params?: any) {
const { email, login, first_name, last_name, role }: any = params || {};
const query = this.User.query();
@ -51,6 +69,12 @@ export default class UserRepository {
return data;
}
/**
* Create user
*
* @param data Data for create user
* @returns Created user
*/
public async create(data: CreateUserValidator['schema']['props']) {
const createdUser = await this.User.create({
first_name: data.first_name,

View File

@ -71,7 +71,7 @@ export default class UserService {
* Create new user
*
* @param data Input for create new user
* @returns Response data
* @returns Response
*/
public async createUser(data: CreateUserValidator['schema']['props']): Promise<IResponse> {
const user = await this.userRepository.create(data);
@ -87,6 +87,13 @@ export default class UserService {
};
}
/**
* Update user
*
* @param id User id
* @param data Data to update
* @returns Response
*/
public async updateUser(id: string | number, data: UpdateUserValidator['schema']['props']): Promise<IResponse> {
const user = await this.userRepository.update(id, data);
@ -115,6 +122,12 @@ export default class UserService {
};
}
/**
* Delete user
*
* @param id User id
* @returns Response
*/
public async deleteUser(id: string | number): Promise<IResponse> {
const user = await this.userRepository.delete(id);
@ -143,7 +156,7 @@ export default class UserService {
*
* @param id User id
* @param input Roles
* @returns Result
* @returns Response
*/
public async attachRolesToUser(id: string | number, input: string[]): Promise<IResponse> {
const user = await this.userRepository.getById(id);
@ -204,7 +217,7 @@ export default class UserService {
*
* @param id User id
* @param input Roles
* @returns Result
* @returns Response
*/
public async detachRolesFromUser(id: string | number, input: string[]): Promise<IResponse> {
const user = await this.userRepository.getById(id);