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 { UserServiceBindings, UserService, User, UserRepository, UserCredentials, UserGroup, UserGroupRepository, UserToGroup, UserToGroupRepository, authenticate, ErrorSchema, StatusSchema, } from '../..'; export class UserGroupController { constructor( @inject(UserServiceBindings.REPO_USER) public userRepository: UserRepository, @inject(UserServiceBindings.REPO_GROUP) public userGroupRepository: UserGroupRepository, @inject(UserServiceBindings.REPO_USR2GRP) public userToGroupRepository: UserToGroupRepository, ) {} @post('/user-groups', { responses: { '200': { description: 'UserGroup model instance', content: {'application/json': {schema: {'x-ts-type': UserGroup}}}, }, "401": { description: "Unauthorized - token is invalid/expired or absent", content: { "application/json": { schema: ErrorSchema, }, }, } }, security: [ { jwt: ['jwt'], }, ], }) @authenticate('jwt') async create(@requestBody() userGroup: UserGroup): Promise { const _userGroup = await this.userGroupRepository.findOne({ where: { name: userGroup.name }, }); if (_userGroup) throw new HttpErrors.UnprocessableEntity('Error: field is not unique'); return await this.userGroupRepository.create(userGroup); } @get('/user-groups/count', { responses: { '200': { description: 'UserGroup 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(UserGroup)) where?: Where, ): Promise { return await this.userGroupRepository.count(where); } @get('/user-groups', { responses: { '200': { description: 'Array of UserGroup model instances', content: { 'application/json': { schema: {type: 'array', items: {'x-ts-type': UserGroup}}, }, }, }, "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(UserGroup)) filter?: Filter, ): Promise { let userGroups: UserGroup[] = await this.userGroupRepository.find(filter); return userGroups; } @patch('/user-groups', { responses: { '200': { description: 'UserGroup 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() userGroup: UserGroup, @param.query.object('where', getWhereSchemaFor(UserGroup)) where?: Where, ): Promise { return await this.userGroupRepository.updateAll(userGroup, where); } @get('/user-groups/{id}', { responses: { '200': { description: 'UserGroup model instance', content: {'application/json': {schema: {'x-ts-type': UserGroup}}}, }, "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 userGroup: UserGroup = await this.userGroupRepository.findById(id); return userGroup; } @patch('/user-groups/{id}', { responses: { '204': { description: 'UserGroup 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() userGroup: UserGroup, ): Promise { await this.userGroupRepository.updateById(id, userGroup); } @put('/user-groups/{id}', { responses: { '204': { description: 'UserGroup 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() userGroup: UserGroup, ): Promise { await this.userGroupRepository.replaceById(id, userGroup); } @del('/user-groups/{id}', { responses: { '204': { description: 'UserGroup 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.userGroupRepository.deleteById(id); } @post('/user-groups/{groupId}/push/{userId}', { responses: { '204': { description: 'Add User to UserGroup', }, "401": { description: "Unauthorized - token is invalid/expired or absent", content: { "application/json": { schema: ErrorSchema, }, }, } }, security: [ { jwt: ['jwt'], }, ], }) @authenticate('jwt') async push( @param.path.number('groupId') groupId: number, @param.path.number('userId') userId: number, ): Promise { const user: User = await this.userRepository.findById(userId); const userGroup: UserGroup = await this.userGroupRepository.findById(groupId); let userToGroup: UserToGroup|null = await this.userToGroupRepository.findOne({ where: { groupId, userId }, }); if (userToGroup) throw new HttpErrors.UnprocessableEntity(`Error: user group with id ${groupId} has user with id ${userId}`); userToGroup = await this.userToGroupRepository.create(new UserToGroup({ userId, groupId })); } @post('/user-groups/{groupId}/pop/{userId}', { responses: { '204': { description: 'Delete User from UserGroup', }, "401": { description: "Unauthorized - token is invalid/expired or absent", content: { "application/json": { schema: ErrorSchema, }, }, } }, security: [ { jwt: ['jwt'], }, ], }) @authenticate('jwt') async pop( @param.path.number('groupId') groupId: number, @param.path.number('userId') userId: number, ): Promise { const user: User = await this.userRepository.findById(userId); const userGroup: UserGroup = await this.userGroupRepository.findById(groupId); const userToGroup: UserToGroup|null = await this.userToGroupRepository.findOne({ where: { groupId, userId }, }); if (!userToGroup) throw new HttpErrors.NotFound(`Error: user group with id ${groupId} has NO user with id ${userId}`); await this.userToGroupRepository.deleteAll({ userId, groupId }); } }