Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 2x 2x | import { inject } from '@loopback/core'; import { repository, Where } from '@loopback/repository'; import { get, param, patch, post, requestBody, response, Response, RestBindings } from '@loopback/rest'; import { Roles } from '../models'; import { RolesRepository } from '../repositories'; import { failureHandler, getUID, successHandler, validatePayload } from './components'; import { countReq, roleReq } from "./requestSpecs"; import { log } from "../utils/logMethod"; import errorMessages from '../utils/errorMessages'; export class RolesController { userRepository: any; constructor( @repository(RolesRepository) public rolesRepository: RolesRepository, @inject(RestBindings.Http.RESPONSE) protected res: Response, ) { } @post('/roles') @log() @response(200, roleReq) async create(@requestBody(roleReq) roles: Roles,): Promise<Response> { try { validatePayload(roles, roleReq) const roleExist = await this.rolesRepository.findOne({ where: { roleType: roles.roleType } }) if (!roleExist) { roles.UID = getUID() const newRole = await this.rolesRepository.create(roles); return successHandler(this.res, 200, newRole) } else { return this.failureHandler(this.res, 202, errorMessages.dataAlreadyExist) } } catch (error) { return this.failureHandler(this.res, 403, error) } } @get('/roles') @log() async find(): Promise<Response> { try { const result = await this.rolesRepository.find(); return successHandler(this.res, 200, result) } catch (error) { return this.failureHandler(this.res, 403, error) } } @patch('/roles/{UID}') @log() async updateById(@param.path.string('UID') UID: string, @requestBody(roleReq) roles: Roles,): Promise<Response> { try { validatePayload(roles, roleReq) const result = await this.rolesRepository.updateById(UID, roles); return successHandler(this.res, 200, result) } catch (error) { return this.failureHandler(this.res, 403, error) } } private failureHandler(res: Response, code: number, error: any): Promise<Response> { let className = error.className || RolesController.name return failureHandler(res, code, error, className) } } |