|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Controller, |
| 4 | + Get, |
| 5 | + HttpException, |
| 6 | + Logger, |
| 7 | + Param, |
| 8 | + Post, |
| 9 | + Put, |
| 10 | +} from '@nestjs/common'; |
| 11 | +import { ApiTags } from '@nestjs/swagger'; |
| 12 | + |
| 13 | +import { ShelterSupplyService } from './shelter-supply.service'; |
| 14 | +import { ServerResponse } from '../utils'; |
| 15 | + |
| 16 | +@ApiTags('Suprimento de abrigos') |
| 17 | +@Controller('shelter/supplies') |
| 18 | +export class ShelterSupplyController { |
| 19 | + private logger = new Logger(ShelterSupplyController.name); |
| 20 | + |
| 21 | + constructor(private readonly shelterSupplyService: ShelterSupplyService) {} |
| 22 | + |
| 23 | + @Get(':shelterId') |
| 24 | + async index(@Param('shelterId') shelterId: string) { |
| 25 | + try { |
| 26 | + const data = await this.shelterSupplyService.index(shelterId); |
| 27 | + return new ServerResponse(200, 'Successfully get shelter supplies', data); |
| 28 | + } catch (err: any) { |
| 29 | + this.logger.error(`Failed to get shelter supplies: ${err}`); |
| 30 | + throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + @Post('') |
| 35 | + async store(@Body() body) { |
| 36 | + try { |
| 37 | + const data = await this.shelterSupplyService.store(body); |
| 38 | + return new ServerResponse( |
| 39 | + 200, |
| 40 | + 'Successfully created shelter supply', |
| 41 | + data, |
| 42 | + ); |
| 43 | + } catch (err: any) { |
| 44 | + this.logger.error(`Failed to create shelter supply: ${err}`); |
| 45 | + throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Put(':shelterId/:supplyId') |
| 50 | + async update( |
| 51 | + @Body() body, |
| 52 | + @Param('shelterId') shelterId: string, |
| 53 | + @Param('supplyId') supplyId: string, |
| 54 | + ) { |
| 55 | + try { |
| 56 | + const data = await this.shelterSupplyService.update({ |
| 57 | + where: { shelterId, supplyId }, |
| 58 | + data: body, |
| 59 | + }); |
| 60 | + return new ServerResponse( |
| 61 | + 200, |
| 62 | + 'Successfully updated shelter supply', |
| 63 | + data, |
| 64 | + ); |
| 65 | + } catch (err: any) { |
| 66 | + this.logger.error(`Failed to update shelter supply: ${err}`); |
| 67 | + throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments