Skip to content

Commit 1dd314c

Browse files
authored
Merge pull request #30 from MLKP1/dev
Dev
2 parents 13f8d53 + 42c709e commit 1dd314c

6 files changed

Lines changed: 72 additions & 0 deletions

File tree

.env.example

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
DATABASE_URL="postgresql://tcc:tcc123@localhost:5433/tcc-back"
2+
PORT=3334
3+
NODE_ENV="dev"
4+
JWT_SECRET=""
5+
6+
RESEND_API_KEY="re_"
7+
8+
API_URL="https://api.pizzastars.shop"
9+
API_TOKEN="auth="

src/env/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const envSchema = z.object({
77
NODE_ENV: z.enum(['dev', 'test', 'prod']).default('dev'),
88
JWT_SECRET: z.string(),
99
RESEND_API_KEY: z.string(),
10+
API_URL: z.string().url(),
11+
API_TOKEN: z.string(),
1012
})
1113

1214
const _env = envSchema.safeParse(process.env)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { FastifyReply, FastifyRequest } from 'fastify'
2+
import { env } from '@/env'
3+
4+
interface GetDrinksQuery {
5+
pageIndex?: number | string
6+
}
7+
8+
export async function getDrinks(request: FastifyRequest, reply: FastifyReply) {
9+
const cookie = env.API_TOKEN
10+
if (!cookie) return reply.status(401).send({ error: 'Sessão não encontrada' })
11+
12+
const { pageIndex } = request.query as GetDrinksQuery
13+
14+
const response = await fetch(
15+
`${env.API_URL}/products/drinks?active=true&pageIndex=${pageIndex || 0}`,
16+
{
17+
headers: {
18+
Cookie: cookie,
19+
},
20+
},
21+
)
22+
23+
const drinks = await response.json()
24+
reply.send(drinks)
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { FastifyReply, FastifyRequest } from 'fastify'
2+
import { env } from '@/env'
3+
4+
interface GetPizzasQuery {
5+
pageIndex?: number | string
6+
}
7+
8+
export async function getPizzas(request: FastifyRequest, reply: FastifyReply) {
9+
const cookie = env.API_TOKEN
10+
if (!cookie) return reply.status(401).send({ error: 'Sessão não encontrada' })
11+
12+
const { pageIndex } = request.query as GetPizzasQuery
13+
14+
const response = await fetch(
15+
`${env.API_URL}/products/pizzas?active=true&pageIndex=${pageIndex || 0}`,
16+
{
17+
headers: {
18+
Cookie: cookie,
19+
},
20+
},
21+
)
22+
23+
const pizzas = await response.json()
24+
reply.send(pizzas)
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { FastifyInstance } from 'fastify'
2+
3+
import { getPizzas } from './getPizzas'
4+
import { getDrinks } from './getDrinks'
5+
6+
export async function productsRoutes(app: FastifyInstance) {
7+
app.get('/pizzas', getPizzas)
8+
app.get('/drinks', getDrinks)
9+
}

src/http/controllers/routes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import type { FastifyInstance } from 'fastify'
22

33
import { addressRoutes } from './address/routes'
44
import { usersRoutes } from './users/routes'
5+
import { productsRoutes } from './products/routes'
56

67
export async function appRoutes(app: FastifyInstance) {
78
app.register(usersRoutes)
89
app.register(addressRoutes)
10+
app.register(productsRoutes)
911
}

0 commit comments

Comments
 (0)