diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..a404703 --- /dev/null +++ b/.env.example @@ -0,0 +1,9 @@ +DATABASE_URL="postgresql://tcc:tcc123@localhost:5433/tcc-back" +PORT=3334 +NODE_ENV="dev" +JWT_SECRET="" + +RESEND_API_KEY="re_" + +API_URL="https://api.pizzastars.shop" +API_TOKEN="auth=" diff --git a/src/env/index.ts b/src/env/index.ts index 01a19a9..2c4f3e4 100644 --- a/src/env/index.ts +++ b/src/env/index.ts @@ -7,6 +7,8 @@ const envSchema = z.object({ NODE_ENV: z.enum(['dev', 'test', 'prod']).default('dev'), JWT_SECRET: z.string(), RESEND_API_KEY: z.string(), + API_URL: z.string().url(), + API_TOKEN: z.string(), }) const _env = envSchema.safeParse(process.env) diff --git a/src/http/controllers/products/getDrinks.ts b/src/http/controllers/products/getDrinks.ts new file mode 100644 index 0000000..fe3226b --- /dev/null +++ b/src/http/controllers/products/getDrinks.ts @@ -0,0 +1,25 @@ +import type { FastifyReply, FastifyRequest } from 'fastify' +import { env } from '@/env' + +interface GetDrinksQuery { + pageIndex?: number | string +} + +export async function getDrinks(request: FastifyRequest, reply: FastifyReply) { + const cookie = env.API_TOKEN + if (!cookie) return reply.status(401).send({ error: 'Sessão não encontrada' }) + + const { pageIndex } = request.query as GetDrinksQuery + + const response = await fetch( + `${env.API_URL}/products/drinks?active=true&pageIndex=${pageIndex || 0}`, + { + headers: { + Cookie: cookie, + }, + }, + ) + + const drinks = await response.json() + reply.send(drinks) +} diff --git a/src/http/controllers/products/getPizzas.ts b/src/http/controllers/products/getPizzas.ts new file mode 100644 index 0000000..35374bd --- /dev/null +++ b/src/http/controllers/products/getPizzas.ts @@ -0,0 +1,25 @@ +import type { FastifyReply, FastifyRequest } from 'fastify' +import { env } from '@/env' + +interface GetPizzasQuery { + pageIndex?: number | string +} + +export async function getPizzas(request: FastifyRequest, reply: FastifyReply) { + const cookie = env.API_TOKEN + if (!cookie) return reply.status(401).send({ error: 'Sessão não encontrada' }) + + const { pageIndex } = request.query as GetPizzasQuery + + const response = await fetch( + `${env.API_URL}/products/pizzas?active=true&pageIndex=${pageIndex || 0}`, + { + headers: { + Cookie: cookie, + }, + }, + ) + + const pizzas = await response.json() + reply.send(pizzas) +} diff --git a/src/http/controllers/products/routes.ts b/src/http/controllers/products/routes.ts new file mode 100644 index 0000000..5d74ab9 --- /dev/null +++ b/src/http/controllers/products/routes.ts @@ -0,0 +1,9 @@ +import type { FastifyInstance } from 'fastify' + +import { getPizzas } from './getPizzas' +import { getDrinks } from './getDrinks' + +export async function productsRoutes(app: FastifyInstance) { + app.get('/pizzas', getPizzas) + app.get('/drinks', getDrinks) +} diff --git a/src/http/controllers/routes.ts b/src/http/controllers/routes.ts index c942564..5d1dcf4 100644 --- a/src/http/controllers/routes.ts +++ b/src/http/controllers/routes.ts @@ -2,8 +2,10 @@ import type { FastifyInstance } from 'fastify' import { addressRoutes } from './address/routes' import { usersRoutes } from './users/routes' +import { productsRoutes } from './products/routes' export async function appRoutes(app: FastifyInstance) { app.register(usersRoutes) app.register(addressRoutes) + app.register(productsRoutes) }