-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from MatheusSanchez/SFD-24-controller-get-user
Controller Get User
- Loading branch information
Showing
6 changed files
with
113 additions
and
16 deletions.
There are no files selected for viewing
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,3 +1,25 @@ | ||
import fastify from 'fastify' | ||
import { userRoutes } from './controller/user/routes' | ||
import { env } from './env' | ||
import { ZodError } from 'zod' | ||
|
||
|
||
export const app = fastify() | ||
app.register(userRoutes) | ||
|
||
|
||
|
||
app.setErrorHandler((error, _, response) => { | ||
if (env.NODE_ENV !== 'production') { | ||
console.error(error) | ||
} else { | ||
// TODO: log this error somewhere | ||
} | ||
if (error instanceof ZodError) { | ||
return response | ||
.status(400) | ||
.send({ message: 'Validation Error', issues: error.format() }) | ||
} | ||
|
||
return response.status(500).send({ message: 'Internal Server Error' }) | ||
}) |
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,25 @@ | ||
import { FastifyReply, FastifyRequest } from 'fastify' | ||
import { InMemoryUserRepository } from '../../repositories/in-memory-db/inMemoryUserRepository' | ||
import { z } from 'zod' | ||
import { GetUserByEmailUseCase } from '../../use-cases/getUserByEmailUseCase' | ||
|
||
export async function getUserByEmail( | ||
request: FastifyRequest, | ||
response: FastifyReply, | ||
) { | ||
|
||
const userRepository = new InMemoryUserRepository() | ||
const getUserByEmailUseCase = new GetUserByEmailUseCase(userRepository) | ||
|
||
const getUserByEmailBodySchema = z.object({ | ||
email: z.string().email(), | ||
}) | ||
|
||
const { email } = getUserByEmailBodySchema.parse(request.query) | ||
|
||
const { user } = await getUserByEmailUseCase.execute({ | ||
email, | ||
}) | ||
|
||
return response.status(200).send({ user }) | ||
} |
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,25 @@ | ||
import { FastifyReply, FastifyRequest } from 'fastify' | ||
import { InMemoryUserRepository } from '../../repositories/in-memory-db/inMemoryUserRepository' | ||
import { z } from 'zod' | ||
import { GetUserByIdUseCase } from '../../use-cases/getUserByIdUseCase' | ||
|
||
export async function getUserById( | ||
request: FastifyRequest, | ||
response: FastifyReply, | ||
) { | ||
|
||
const userRepository = new InMemoryUserRepository() | ||
const getUserByIdUseCase = new GetUserByIdUseCase(userRepository) | ||
|
||
const getUserByIdBodySchema = z.object({ | ||
id: z.string().uuid(), | ||
}) | ||
|
||
const { id } = getUserByIdBodySchema.parse(request.params) | ||
|
||
const { user } = await getUserByIdUseCase.execute({ | ||
id, | ||
}) | ||
|
||
return response.status(200).send({ user }) | ||
} |
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,9 @@ | ||
import { FastifyInstance } from 'fastify' | ||
import { getUserById } from './getUserById' | ||
import { getUserByEmail } from './getUserByEmail' | ||
|
||
export async function userRoutes(app: FastifyInstance) { | ||
|
||
app.get('/user/:id', getUserById) | ||
app.get('/user', getUserByEmail) | ||
} |
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 |
---|---|---|
|
@@ -5,6 +5,31 @@ import { randomUUID } from 'crypto' | |
export class InMemoryUserRepository implements UserRepository { | ||
private db: User[] = [] | ||
|
||
|
||
// This is a way to test our controllers without necessartralyy add the | ||
// db repository; Once the program starts, one user is added to User[] and | ||
// you can get http://localhost:3333/user/9600de4f-8d18-4e69-ba7a-ed7fa210618d | ||
// to check the routes; | ||
|
||
// this constructor will be delete later; | ||
constructor(){ | ||
|
||
const email = '[email protected]' | ||
const name = 'John' | ||
const surname = 'Doe' | ||
const password_hash = 'password_hash' | ||
const id = '9600de4f-8d18-4e69-ba7a-ed7fa210618d' | ||
|
||
this.create({ | ||
id, | ||
name, | ||
surname, | ||
email, | ||
password_hash, | ||
}) | ||
|
||
} | ||
|
||
async findByEmail(email: string): Promise<User | null> { | ||
const User = this.db.find((User) => User.email === email) | ||
|
||
|
@@ -28,13 +53,14 @@ export class InMemoryUserRepository implements UserRepository { | |
// create in a in-memory database is just used to help us on unit tests; | ||
// that's why is not in our interface :) | ||
async create({ | ||
id, | ||
name, | ||
surname, | ||
email, | ||
password_hash, | ||
}: Prisma.UserCreateInput) { | ||
const user: User = { | ||
id: randomUUID(), | ||
id: (id == undefined) ? randomUUID() : id, | ||
name, | ||
surname, | ||
|
||
|
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,19 +1,9 @@ | ||
import { app } from './app' | ||
import { env } from './env' | ||
import { prisma } from './lib/prisma' | ||
|
||
app.get('/', async (req, res) => { | ||
const user = await prisma.user.create({ | ||
data: { | ||
name: 'Max', | ||
surname: 'Sousa', | ||
email: '[email protected]', | ||
password_hash: '123', | ||
} | ||
app.listen({ | ||
port: env.PORT, | ||
}) | ||
res.send('Lets go Força da Natureza!') | ||
}) | ||
|
||
app.listen(env.PORT, () => { | ||
console.log('🔥🔥🔥 HTTP Server Running 🔥🔥🔥') | ||
}) | ||
.then(() => { | ||
console.log('🔥🔥🔥 HTTP Server Running 🔥🔥🔥') | ||
}) |