-
Notifications
You must be signed in to change notification settings - Fork 0
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
Fabio Brasileiro
authored and
Fabio Brasileiro
committed
Sep 27, 2024
1 parent
f3ac4e1
commit 30eea0e
Showing
25 changed files
with
739 additions
and
34 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,32 @@ | ||
import { makeCreateGymUseCase } from '@/use-cases/factories/make-create-gym-use-case' | ||
import type { FastifyReply, FastifyRequest } from 'fastify' | ||
import { z } from 'zod' | ||
|
||
export async function create(request: FastifyRequest, reply: FastifyReply) { | ||
const createGymBodySchema = z.object({ | ||
title: z.string(), | ||
description: z.string().nullable(), | ||
phone: z.string().nullable(), | ||
latitude: z.number().refine(value => { | ||
return Math.abs(value) <= 90 | ||
}), | ||
|
||
longitude: z.number().refine(value => { | ||
return Math.abs(value) <= 180 | ||
}), | ||
}) | ||
|
||
const { title, description, phone, latitude, longitude } = | ||
createGymBodySchema.parse(request.body) | ||
const createGymUseCase = makeCreateGymUseCase() | ||
|
||
await createGymUseCase.execute({ | ||
title, | ||
description, | ||
phone, | ||
latitude, | ||
longitude, | ||
}) | ||
|
||
return reply.status(201).send() | ||
} |
Empty file.
Empty file.
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,10 @@ | ||
import { FastifyInstance } from "fastify"; | ||
|
||
import { verifyJWT } from "@/http/middlewares/verify-jwt"; | ||
import { create } from "../gyms/create"; | ||
|
||
export async function checkInsRoutes(app: FastifyInstance) { | ||
app.addHook('onRequest', verifyJWT) | ||
|
||
app.post('/gyms/:gymId/check-ins', create) | ||
} |
Empty file.
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,32 @@ | ||
import { makeCreateGymUseCase } from '@/use-cases/factories/make-create-gym-use-case' | ||
import type { FastifyReply, FastifyRequest } from 'fastify' | ||
import { z } from 'zod' | ||
|
||
export async function create(request: FastifyRequest, reply: FastifyReply) { | ||
const createGymBodySchema = z.object({ | ||
title: z.string(), | ||
description: z.string().nullable(), | ||
phone: z.string().nullable(), | ||
latitude: z.number().refine(value => { | ||
return Math.abs(value) <= 90 | ||
}), | ||
|
||
longitude: z.number().refine(value => { | ||
return Math.abs(value) <= 180 | ||
}), | ||
}) | ||
|
||
const { title, description, phone, latitude, longitude } = | ||
createGymBodySchema.parse(request.body) | ||
const createGymUseCase = makeCreateGymUseCase() | ||
|
||
await createGymUseCase.execute({ | ||
title, | ||
description, | ||
phone, | ||
latitude, | ||
longitude, | ||
}) | ||
|
||
return reply.status(201).send() | ||
} |
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,26 @@ | ||
import { makeFetchNearbyGymsUseCase } from '@/use-cases/factories/make-fetch-nearby-gyms-use-case' | ||
import type { FastifyReply, FastifyRequest } from 'fastify' | ||
import { z } from 'zod' | ||
|
||
export async function nearby(request: FastifyRequest, reply: FastifyReply) { | ||
const nearbyGymQuerySchema = z.object({ | ||
latitude: z.number().refine(value => { | ||
return Math.abs(value) <= 90 | ||
}), | ||
longitude: z.number().refine(value => { | ||
return Math.abs(value) <= 180 | ||
}), | ||
}) | ||
|
||
const { latitude, longitude } = nearbyGymQuerySchema.parse(request.body) | ||
const fetchNearbyGymsUseCase = makeFetchNearbyGymsUseCase() | ||
|
||
const { gyms } = await fetchNearbyGymsUseCase.execute({ | ||
userLatitude: latitude, | ||
userLogngitude: longitude, | ||
}) | ||
|
||
return reply.status(200).send({ | ||
gyms, | ||
}) | ||
} |
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,15 @@ | ||
import { FastifyInstance } from 'fastify' | ||
|
||
import { verifyJWT } from '@/http/middlewares/verify-jwt' | ||
import { search } from './search' | ||
import { nearby } from './nearby' | ||
import {create} from './create' | ||
|
||
export async function gymsRoutes(app: FastifyInstance) { | ||
app.addHook('onRequest', verifyJWT) | ||
|
||
app.get('/gyms/search', search) | ||
app.get('/gyms/nearby',nearby) | ||
|
||
app.post('/gyms', create) | ||
} |
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,22 @@ | ||
import { makeSearchGymsUseCase } from '@/use-cases/factories/make-search-gyms-use-case' | ||
import type { FastifyReply, FastifyRequest } from 'fastify' | ||
import { z } from 'zod' | ||
|
||
export async function search(request: FastifyRequest, reply: FastifyReply) { | ||
const serchGymQuerySchema = z.object({ | ||
q: z.string(), | ||
page: z.coerce.number().min(1).default(1), | ||
}) | ||
|
||
const { q, page } = serchGymQuerySchema.parse(request.body) | ||
const searchGymsUseCase = makeSearchGymsUseCase() | ||
|
||
const { gyms } = await searchGymsUseCase.execute({ | ||
query: q, | ||
page, | ||
}) | ||
|
||
return reply.status(200).send({ | ||
gyms, | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
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,31 @@ | ||
import request from 'supertest' | ||
import { app } from '@/app' | ||
import { afterAll, beforeAll, describe, expect, it } from 'vitest' | ||
|
||
describe('Authenticate (e2e)', () => { | ||
beforeAll(async () => { | ||
await app.ready() | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
it('should be able to authenticate', async () => { | ||
await request(app.server).post('/users').send({ | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
password: '123456', | ||
}) | ||
|
||
const response = await request(app.server).post('/sessions').send({ | ||
email: '[email protected]', | ||
password: '123456', | ||
}) | ||
|
||
expect(response.statusCode).toEqual(200) | ||
expect(response.body).toEqual({ | ||
token: expect.any(String), | ||
}) | ||
}) | ||
}) |
File renamed without changes.
File renamed without changes.
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,40 @@ | ||
import request from 'supertest' | ||
import { app } from '@/app' | ||
import { afterAll, beforeAll, describe, expect, it } from 'vitest' | ||
|
||
describe('Profile (e2e)', () => { | ||
beforeAll(async () => { | ||
await app.ready() | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
it('should be able to user profile', async () => { | ||
await request(app.server).post('/users').send({ | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
password: '123456', | ||
}) | ||
|
||
const authResponse = await request(app.server).post('/sessions').send({ | ||
email: '[email protected]', | ||
password: '123456', | ||
}) | ||
|
||
const { token } = authResponse.body | ||
|
||
const profileResponse = await request(app.server) | ||
.get('/me') | ||
.set('Authorization', `Bearer ${token}`) | ||
.send() | ||
|
||
expect(profileResponse.statusCode).toEqual(200) | ||
expect(profileResponse.body.user).toEqual( | ||
expect.objectContaining({ | ||
email: '[email protected]', | ||
}) | ||
) | ||
}) | ||
}) |
File renamed without changes.
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,23 @@ | ||
import request from 'supertest' | ||
import { app } from '@/app' | ||
import { afterAll, beforeAll, describe, expect, it } from 'vitest' | ||
|
||
describe('Register (e2e)', () => { | ||
beforeAll(async () => { | ||
await app.ready() | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
it('should be able to register', async () => { | ||
const response = await request(app.server).post('/users').send({ | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
password: '123456', | ||
}) | ||
|
||
expect(response.statusCode).toEqual(201) | ||
}) | ||
}) |
File renamed without changes.
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,16 @@ | ||
import type { FastifyInstance } from 'fastify' | ||
import { authenticate } from './authenticate' | ||
import { register } from './register' | ||
import { baseRouter } from './baseRouter' | ||
import { profile } from './profile' | ||
import { verifyJWT } from '@/http/middlewares/verify-jwt' | ||
|
||
export async function usersRoutes(app: FastifyInstance) { | ||
app.get('/', baseRouter) | ||
|
||
app.post('/users', register) | ||
|
||
app.post('/sessions', authenticate) | ||
/** Authentication */ | ||
app.get('/me', { onRequest: [verifyJWT] }, profile) | ||
} |
This file was deleted.
Oops, something went wrong.
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,42 +1,44 @@ | ||
import { PrismaClient } from '@prisma/client' | ||
import 'dotenv/config' | ||
|
||
import { randomUUID } from 'node:crypto' | ||
import { execSync } from 'node:child_process' | ||
import { Environment } from 'vitest' | ||
import { PrismaClient } from '@prisma/client' | ||
|
||
const prisma = new PrismaClient() | ||
|
||
function generateDatabaseURL(schema: string) { | ||
if (!process.env.DATABASE_URL) { | ||
throw new Error('Please provide a DATABASE_URL environment variable.') | ||
} | ||
|
||
const url = new URL(process.env.DATABASE_URL) | ||
|
||
url.searchParams.set('schema', schema) | ||
|
||
return url.toString() | ||
} | ||
|
||
const PrismaTestEnvironment: Environment = { | ||
export default (<Environment>{ | ||
name: 'prisma', | ||
async setup() { | ||
const schema = randomUUID() | ||
const databaseURL = generateDatabaseURL(schema) | ||
console.log(generateDatabaseURL(schema)) | ||
|
||
process.env.DATABASE_URL = databaseURL | ||
// Perform any setup you need for the Prisma environment | ||
|
||
execSync('npx prisma migrate deploy') | ||
|
||
return { | ||
async teardown() { | ||
await prisma.$executeRawUnsafe( | ||
`DROP SCHEMA IF EXISTS "${schema}" CASCADE` | ||
) | ||
// Perform any teardown you need for the Prisma environment | ||
await prisma.$disconnect | ||
|
||
await prisma.$disconnect() | ||
}, | ||
transformMode: 'ssr', // Use either 'ssr' or 'web', depending on your needs | ||
transformMode: 'ssr', | ||
} | ||
}, | ||
transformMode: 'ssr', | ||
} | ||
|
||
export default PrismaTestEnvironment | ||
}) |