Skip to content

Commit

Permalink
Merge pull request #8 from MatheusSanchez/SFD-24-controller-get-user
Browse files Browse the repository at this point in the history
Controller Get User
  • Loading branch information
maxyuri13 authored Jan 24, 2024
2 parents ce5930f + 700a8e8 commit 08c465f
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 16 deletions.
22 changes: 22 additions & 0 deletions src/app.ts
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' })
})
25 changes: 25 additions & 0 deletions src/controller/user/getUserByEmail.ts
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 })
}
25 changes: 25 additions & 0 deletions src/controller/user/getUserById.ts
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 })
}
9 changes: 9 additions & 0 deletions src/controller/user/routes.ts
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)
}
28 changes: 27 additions & 1 deletion src/repositories/in-memory-db/inMemoryUserRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,

Expand Down
20 changes: 5 additions & 15 deletions src/server.ts
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 🔥🔥🔥')
})

0 comments on commit 08c465f

Please sign in to comment.