Skip to content

Commit 27e23cc

Browse files
committed
implemeent user get by id
1 parent d10fd7a commit 27e23cc

File tree

6 files changed

+54
-3
lines changed

6 files changed

+54
-3
lines changed

Projects/BooksLibrary/src/api/controllers/user.controller.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,26 @@ export class UserController {
2626
};
2727

2828
getById = async (request: express.Request, response: express.Response): Promise<void> => {
29-
throw new Error('Method not implemented.');
29+
try {
30+
// request.context = 'User.create';
31+
32+
const userId: string = await UserValidator.get(request, response);
33+
34+
const userdetails: UserDetailsDto = await this._service.getById(userId);
35+
36+
ResponseHandler.success(
37+
request,
38+
response,
39+
'User get by id!',
40+
200,
41+
{
42+
entity: userdetails,
43+
},
44+
false
45+
);
46+
} catch (err) {
47+
ResponseHandler.handleError(request, response, err);
48+
}
3049
};
3150

3251
search = async (request: express.Request, response: express.Response): Promise<void> => {

Projects/BooksLibrary/src/api/routes/user.routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const register = (app: express.Application): void => {
1010
const controller = new UserController();
1111

1212
router.post('/', controller.create);
13-
// router.get('/', authenticator.authenticateUser, controller.search);
13+
router.get('/:id', controller.getById);
1414
// router.get('/:id', authenticator.authenticateUser, controller.getById);
1515
// router.delete('/:id', authenticator.authenticateUser, controller.delete);
1616

Projects/BooksLibrary/src/api/validators/user.validator.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,20 @@ import express from 'express';
66
import { body, oneOf, param, query, validationResult } from 'express-validator';
77

88
export class UserValidator {
9-
//
9+
static get = async (request: express.Request, response: express.Response): Promise<string> => {
10+
try {
11+
await param('id').trim().escape().isUUID().run(request);
12+
13+
const result = validationResult(request);
14+
if (!result.isEmpty()) {
15+
Helper.handleValidationError(result);
16+
}
17+
18+
return request.params.id;
19+
} catch (err) {
20+
ResponseHandler.handleError(request, response, err);
21+
}
22+
};
1023

1124
static loginWithPassword = async (
1225
request: express.Request,

Projects/BooksLibrary/src/database/repository.interfaces/user.repo.intrerface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { UserDomainModel } from 'domain.types/user/user.domain.model';
22
import { UserDetailsDto } from 'domain.types/user/user.dto';
33

44
export interface IUserRepo {
5+
getById(userId: string): Promise<UserDetailsDto>;
56
getUserHashedPassword(id: string): Promise<string>;
67
findOneUser(options: UserFindOptions): Promise<UserDetailsDto>;
78
findUsersByRoleId(id: string): Promise<UserDetailsDto[]>;

Projects/BooksLibrary/src/database/sql/sequelize/repositories/user.repo.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ import { UserMapper } from '../mapper/user.mapper';
66
import User from '../models/user.model';
77

88
export class UserRepo implements IUserRepo {
9+
getById = async (userId: string): Promise<UserDetailsDto> => {
10+
const user: User = await User.findOne({
11+
where: {
12+
id: userId,
13+
},
14+
});
15+
16+
const details: UserDetailsDto = await UserMapper.toDetailsDto(user);
17+
18+
return details;
19+
};
20+
921
async getUserHashedPassword(userId: string): Promise<string> {
1022
const user: User = await User.findOne({
1123
where: {

Projects/BooksLibrary/src/services/user.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ import { IUserRepo } from '../database/repository.interfaces/user.repo.intrerfac
1414
export class UserService {
1515
constructor(@inject('IUserRepo') private _userRepo: IUserRepo, @inject('IRoleRepo') private _roleRepo: IRoleRepo) {}
1616

17+
getById = async (userId: string): Promise<UserDetailsDto> => {
18+
const userDetailsDto: UserDetailsDto = await this._userRepo.getById(userId);
19+
20+
return userDetailsDto;
21+
};
22+
1723
create = async (userDetails: UserDomainModel): Promise<UserDetailsDto> => {
1824
const userRole: RoleDto = await this._roleRepo.getByName(Roles.User);
1925
userDetails.RoleId = userRole.id;

0 commit comments

Comments
 (0)