import { inject } from '@loopback/context'; import { Count, CountSchema, Filter, repository, Where, } from '@loopback/repository'; import { HttpErrors, post, param, get, patch, put, del, getFilterSchemaFor, getWhereSchemaFor, requestBody, } from '@loopback/rest'; import { AuthenticationBindings } from '@loopback/authentication'; import { PasswordServiceBindings, UserServiceBindings, PasswordService, UserService, User, UserRepository, UserCredentials, UserGroup, UserToGroup, UserProfile, authenticate, ErrorSchema, StatusSchema, } from '../..'; export class UserController { constructor( @inject(UserServiceBindings.REPO_USER) public userRepository: UserRepository, @inject(PasswordServiceBindings.SERVICE) private _passService: PasswordService, ) {} @post('/users', { responses: { '200': { description: 'User model instance', content: {'application/json': {schema: {'x-ts-type': User}}}, }, "401": { description: "Unauthorized - token is invalid/expired or absent", content: { "application/json": { schema: ErrorSchema, }, }, } }, security: [ { jwt: ['jwt'], }, ], }) @authenticate('jwt') async create(@requestBody() user: User): Promise { if (user.id) delete user.id; const _user = await this.userRepository.findOne({ where: { or: [{ email: user.email }, { username: user.username }] }, }); if (_user) throw new HttpErrors.UnprocessableEntity('Error: field is not unique'); user.password = await this._passService.hash(user.password); return await this.userRepository.create(user); } @get('/users/count', { responses: { '200': { description: 'User model count', content: {'application/json': {schema: CountSchema}}, }, "401": { description: "Unauthorized - token is invalid/expired or absent", content: { "application/json": { schema: ErrorSchema, }, }, } }, security: [ { jwt: ['jwt'], }, ], }) @authenticate('jwt') async count( @param.query.object('where', getWhereSchemaFor(User)) where?: Where, ): Promise { return await this.userRepository.count(where); } @get('/users', { responses: { '200': { description: 'Array of User model instances', content: { 'application/json': { schema: {type: 'array', items: {'x-ts-type': User}}, }, }, }, "401": { description: "Unauthorized - token is invalid/expired or absent", content: { "application/json": { schema: ErrorSchema, }, }, } }, security: [ { jwt: ['jwt'], }, ], }) @authenticate('jwt') async find( @param.query.object('filter', getFilterSchemaFor(User)) filter?: Filter, ): Promise { let users: User[] = await this.userRepository.find(filter); return users; } @patch('/users', { responses: { '200': { description: 'User PATCH success count', content: {'application/json': {schema: CountSchema}}, }, "401": { description: "Unauthorized - token is invalid/expired or absent", content: { "application/json": { schema: ErrorSchema, }, }, } }, security: [ { jwt: ['jwt'], }, ], }) @authenticate('jwt') async updateAll( @requestBody() user: User, @param.query.object('where', getWhereSchemaFor(User)) where?: Where, ): Promise { return await this.userRepository.updateAll(user, where); } @get('/users/{id}', { responses: { '200': { description: 'User model instance', content: {'application/json': {schema: {'x-ts-type': User}}}, }, "401": { description: "Unauthorized - token is invalid/expired or absent", content: { "application/json": { schema: ErrorSchema, }, }, } }, security: [ { jwt: ['jwt'], }, ], }) @authenticate('jwt') async findById(@param.path.number('id') id: number): Promise { let user: User = await this.userRepository.findById(id); return user; } @patch('/users/{id}', { responses: { '204': { description: 'User PATCH success', }, "401": { description: "Unauthorized - token is invalid/expired or absent", content: { "application/json": { schema: ErrorSchema, }, }, } }, security: [ { jwt: ['jwt'], }, ], }) @authenticate('jwt') async updateById( @param.path.number('id') id: number, @requestBody() user: User, ): Promise { await this.userRepository.updateById(id, user); } @put('/users/{id}', { responses: { '204': { description: 'User PUT success', }, "401": { description: "Unauthorized - token is invalid/expired or absent", content: { "application/json": { schema: ErrorSchema, }, }, } }, security: [ { jwt: ['jwt'], }, ], }) @authenticate('jwt') async replaceById( @param.path.number('id') id: number, @requestBody() user: User, ): Promise { await this.userRepository.replaceById(id, user); } @del('/users/{id}', { responses: { '204': { description: 'User DELETE success', }, "401": { description: "Unauthorized - token is invalid/expired or absent", content: { "application/json": { schema: ErrorSchema, }, }, } }, security: [ { jwt: ['jwt'], }, ], }) @authenticate('jwt') async deleteById(@param.path.number('id') id: number): Promise { await this.userRepository.deleteById(id); } // ME @get('/users/me', { responses: { '200': { description: 'User model instance', content: {'application/json': {schema: {'x-ts-type': User}}}, }, "401": { description: "Unauthorized - token is invalid/expired or absent", content: { "application/json": { schema: ErrorSchema, }, }, } }, security: [ { jwt: ['jwt'], }, ], }) @authenticate('jwt') async findMe( @inject(AuthenticationBindings.CURRENT_USER) userProfile: UserProfile, ): Promise { let user: User = await this.userRepository.findById(Number(userProfile.id)); return user; } @patch('/users/me', { responses: { '204': { description: 'User PATCH success', }, "401": { description: "Unauthorized - token is invalid/expired or absent", content: { "application/json": { schema: ErrorSchema, }, }, } }, security: [ { jwt: ['jwt'], }, ], }) @authenticate('jwt') async updateMe( @inject(AuthenticationBindings.CURRENT_USER) userProfile: UserProfile, @requestBody() patchUser: User, ): Promise { await this.userRepository.updateById(Number(userProfile.id), patchUser); } @put('/users/me', { responses: { '204': { description: 'User PUT success', }, "401": { description: "Unauthorized - token is invalid/expired or absent", content: { "application/json": { schema: ErrorSchema, }, }, } }, security: [ { jwt: ['jwt'], }, ], }) @authenticate('jwt') async replaceMe( @inject(AuthenticationBindings.CURRENT_USER) userProfile: UserProfile, @requestBody() putUser: User, ): Promise { await this.userRepository.replaceById(Number(userProfile.id), putUser); } @del('/users/me', { responses: { '204': { description: 'User DELETE success', }, "401": { description: "Unauthorized - token is invalid/expired or absent", content: { "application/json": { schema: ErrorSchema, }, }, } }, security: [ { jwt: ['jwt'], }, ], }) @authenticate('jwt') async deleteMe( @inject(AuthenticationBindings.CURRENT_USER) userProfile: UserProfile, ): Promise { await this.userRepository.deleteById(Number(userProfile.id)); } }