Skip to content
Merged

Dev #30

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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="
2 changes: 2 additions & 0 deletions src/env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions src/http/controllers/products/getDrinks.ts
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 25 additions & 0 deletions src/http/controllers/products/getPizzas.ts
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 9 additions & 0 deletions src/http/controllers/products/routes.ts
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 2 additions & 0 deletions src/http/controllers/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}