// Copyright IBM Corp. and LoopBack contributors 2020. All Rights Reserved. // Node module: @loopback/example-multi-tenancy // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT import {inject} from '@loopback/core'; import { Count, CountSchema, Filter, FilterExcludingWhere, repository, Where, } from '@loopback/repository'; import { del, get, getModelSchemaRef, param, patch, post, put, requestBody, } from '@loopback/rest'; import {User} from '../models'; import {MultiTenancyBindings, Tenant} from '../multi-tenancy'; import {UserRepository} from '../repositories'; export class UserController { constructor( @repository(UserRepository) public userRepository: UserRepository, @inject(MultiTenancyBindings.CURRENT_TENANT, {optional: true}) private tenant?: Tenant, ) {} @post('/users', { responses: { '200': { description: 'User model instance', content: {'application/json': {schema: getModelSchemaRef(User)}}, }, }, }) async create( @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(User, { title: 'NewUser', exclude: ['id'], }), }, }, }) user: Omit, ): Promise { // Inject the tenant id for creation if (this.tenant?.id != null) { user.tenantId = this.tenant?.id; } else { user.tenantId = ''; } return this.userRepository.create(user); } @get('/users/count', { responses: { '200': { description: 'User model count', content: {'application/json': {schema: CountSchema}}, }, }, }) async count(@param.where(User) where?: Where): Promise { return this.userRepository.count(where); } @get('/users', { responses: { '200': { description: 'Array of User model instances', content: { 'application/json': { schema: { type: 'array', items: getModelSchemaRef(User, {includeRelations: true}), }, }, }, }, }, }) async find(@param.filter(User) filter?: Filter): Promise { return this.userRepository.find(filter); } @patch('/users', { responses: { '200': { description: 'User PATCH success count', content: {'application/json': {schema: CountSchema}}, }, }, }) async updateAll( @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(User, {partial: true}), }, }, }) user: User, @param.where(User) where?: Where, ): Promise { return this.userRepository.updateAll(user, where); } @get('/users/{id}', { responses: { '200': { description: 'User model instance', content: { 'application/json': { schema: getModelSchemaRef(User, {includeRelations: true}), }, }, }, }, }) async findById( @param.path.string('id') id: string, @param.filter(User, {exclude: 'where'}) filter?: FilterExcludingWhere, ): Promise { return this.userRepository.findById(id, filter); } @patch('/users/{id}', { responses: { '204': { description: 'User PATCH success', }, }, }) async updateById( @param.path.string('id') id: string, @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(User, {partial: true}), }, }, }) user: User, ): Promise { await this.userRepository.updateById(id, user); } @put('/users/{id}', { responses: { '204': { description: 'User PUT success', }, }, }) async replaceById( @param.path.string('id') id: string, @requestBody() user: User, ): Promise { await this.userRepository.replaceById(id, user); } @del('/users/{id}', { responses: { '204': { description: 'User DELETE success', }, }, }) async deleteById(@param.path.string('id') id: string): Promise { await this.userRepository.deleteById(id); } }