feat(F-048): completed feature
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import type { FastifySchema } from 'fastify';
|
||||
import type pg from 'pg';
|
||||
import { z } from 'zod';
|
||||
import { requireRole, type Authenticate } from '../../../shared/auth.js';
|
||||
import { AppError } from '../../../shared/errors.js';
|
||||
import { errorSchema } from '../../../shared/swagger.js';
|
||||
import { parseJson } from '../../../shared/http-input.js';
|
||||
import {
|
||||
CreateBrand,
|
||||
@@ -50,12 +52,22 @@ export async function registerBrandsRoutes(
|
||||
const createBrand = new CreateBrand(repository);
|
||||
const updateBrand = new UpdateBrand(repository);
|
||||
|
||||
app.get('/brands', async (_request, reply) => {
|
||||
const listBrandsSchema: FastifySchema = {
|
||||
tags: ['Brands'],
|
||||
summary: 'List brands (público)',
|
||||
};
|
||||
app.get('/brands', { schema: listBrandsSchema }, async (_request, reply) => {
|
||||
const items = await listBrands.execute();
|
||||
return reply.send({ items: items.map(serializeBrand) });
|
||||
});
|
||||
|
||||
app.get('/marca/:slug', async (request, reply) => {
|
||||
const publicBrandSchema: FastifySchema = {
|
||||
tags: ['Brands'],
|
||||
summary: 'Get brand by slug (público)',
|
||||
params: { type: 'object', required: ['slug'], properties: { slug: { type: 'string' } } },
|
||||
response: { 404: errorSchema },
|
||||
};
|
||||
app.get('/marca/:slug', { schema: publicBrandSchema }, async (request, reply) => {
|
||||
const { slug } = parseJson(slugParamSchema, request.params);
|
||||
const brand = await getBySlug.execute(slug);
|
||||
if (!brand) {
|
||||
@@ -64,7 +76,13 @@ export async function registerBrandsRoutes(
|
||||
return reply.send(serializeBrand(brand));
|
||||
});
|
||||
|
||||
app.post('/brands', async (request, reply) => {
|
||||
const createBrandSchema: FastifySchema = {
|
||||
tags: ['Brands'],
|
||||
summary: 'Create brand (admin)',
|
||||
body: { type: 'object' },
|
||||
response: { 201: { type: 'object' }, 401: errorSchema, 403: errorSchema },
|
||||
};
|
||||
app.post('/brands', { schema: createBrandSchema }, async (request, reply) => {
|
||||
const user = await deps.authenticate(request);
|
||||
requireRole(user, 'admin');
|
||||
const input = parseJson(newBrandSchema, request.body);
|
||||
@@ -76,7 +94,18 @@ export async function registerBrandsRoutes(
|
||||
}
|
||||
});
|
||||
|
||||
app.patch('/brands/:id', async (request, reply) => {
|
||||
const updateBrandSchema: FastifySchema = {
|
||||
tags: ['Brands'],
|
||||
summary: 'Update brand (admin)',
|
||||
params: {
|
||||
type: 'object',
|
||||
required: ['id'],
|
||||
properties: { id: { type: 'string', format: 'uuid' } },
|
||||
},
|
||||
body: { type: 'object' },
|
||||
response: { 401: errorSchema, 403: errorSchema, 404: errorSchema },
|
||||
};
|
||||
app.patch('/brands/:id', { schema: updateBrandSchema }, async (request, reply) => {
|
||||
const user = await deps.authenticate(request);
|
||||
requireRole(user, 'admin');
|
||||
const { id } = parseJson(idParamSchema, request.params);
|
||||
@@ -93,7 +122,17 @@ export async function registerBrandsRoutes(
|
||||
});
|
||||
|
||||
// DELETE /brands/:id
|
||||
app.delete('/brands/:id', async (request, reply) => {
|
||||
const deleteBrandSchema: FastifySchema = {
|
||||
tags: ['Brands'],
|
||||
summary: 'Delete brand (admin)',
|
||||
params: {
|
||||
type: 'object',
|
||||
required: ['id'],
|
||||
properties: { id: { type: 'string', format: 'uuid' } },
|
||||
},
|
||||
response: { 204: { type: 'null' }, 401: errorSchema, 403: errorSchema },
|
||||
};
|
||||
app.delete('/brands/:id', { schema: deleteBrandSchema }, async (request, reply) => {
|
||||
const user = await deps.authenticate(request);
|
||||
requireRole(user, 'admin');
|
||||
const { id } = parseJson(idParamSchema, request.params);
|
||||
|
||||
Reference in New Issue
Block a user