-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d95d816
commit 574d0c9
Showing
7 changed files
with
123 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
Warnings: | ||
- A unique constraint covering the columns `[name]` on the table `category_supplies` will be added. If there are existing duplicate values, this will fail. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "shelters" ALTER COLUMN "pix" DROP NOT NULL; | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "category_supplies_name_key" ON "category_supplies"("name"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "shelters" ALTER COLUMN "contact" DROP NOT NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,55 @@ | ||
import { Controller } from '@nestjs/common'; | ||
import { | ||
Body, | ||
Controller, | ||
Get, | ||
HttpException, | ||
Logger, | ||
Param, | ||
Post, | ||
Put, | ||
} from '@nestjs/common'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
|
||
import { ServerResponse } from '../utils'; | ||
import { SupplyService } from './supply.service'; | ||
|
||
@ApiTags('Suprimentos') | ||
@Controller('supply') | ||
export class SupplyController {} | ||
@Controller('supplies') | ||
export class SupplyController { | ||
private logger = new Logger(SupplyController.name); | ||
|
||
constructor(private readonly supplyServices: SupplyService) {} | ||
|
||
@Get(':shelterId') | ||
async index(@Param('shelterId') shelterId: string) { | ||
try { | ||
const data = await this.supplyServices.index(shelterId); | ||
return new ServerResponse(200, 'Successfully get supplies', data); | ||
} catch (err: any) { | ||
this.logger.error(`Failed to get supplies: ${err}`); | ||
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400); | ||
} | ||
} | ||
|
||
@Post('') | ||
async store(@Body() body) { | ||
try { | ||
const data = await this.supplyServices.store(body); | ||
return new ServerResponse(200, 'Successfully created supply', data); | ||
} catch (err: any) { | ||
this.logger.error(`Failed to create supply: ${err}`); | ||
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400); | ||
} | ||
} | ||
|
||
@Put(':id') | ||
async update(@Param('id') id: string, @Body() body) { | ||
try { | ||
const data = await this.supplyServices.update(id, body); | ||
return new ServerResponse(200, 'Successfully updated supply', data); | ||
} catch (err: any) { | ||
this.logger.error(`Failed to update supply: ${err}`); | ||
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,55 @@ | ||
import z from 'zod'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { PrismaService } from '../prisma/prisma.service'; | ||
import { CreateSupplySchema, UpdateSupplySchema } from './types'; | ||
|
||
@Injectable() | ||
export class SupplyService { | ||
constructor(private readonly prismaService: PrismaService) {} | ||
|
||
async store(body: z.infer<typeof CreateSupplySchema>) { | ||
const payload = CreateSupplySchema.parse(body); | ||
await this.prismaService.supply.create({ | ||
data: { | ||
...payload, | ||
createdAt: new Date().toISOString(), | ||
}, | ||
}); | ||
} | ||
|
||
async update(id: string, body: z.infer<typeof UpdateSupplySchema>) { | ||
const payload = UpdateSupplySchema.parse(body); | ||
await this.prismaService.supply.update({ | ||
where: { | ||
id, | ||
}, | ||
data: { | ||
...payload, | ||
updatedAt: new Date().toISOString(), | ||
}, | ||
}); | ||
} | ||
|
||
async index(id: string) { | ||
const data = await this.prismaService.supply.findMany({ | ||
where: { | ||
shelterId: id, | ||
}, | ||
select: { | ||
id: true, | ||
name: true, | ||
status: true, | ||
supplyCategory: { | ||
select: { | ||
id: true, | ||
name: true, | ||
}, | ||
}, | ||
createdAt: true, | ||
updatedAt: true, | ||
}, | ||
}); | ||
return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters