Skip to content

Commit

Permalink
Merge branch 'master' into SFD-65-controller-get-projects-from-user
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusSanchez committed Jan 29, 2024
2 parents 2aee59e + d55019f commit 97cb068
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 19 deletions.
51 changes: 51 additions & 0 deletions src/controller/project/createProject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { FastifyReply, FastifyRequest } from 'fastify'
import { z } from 'zod'
import { CreateProjectUseCase } from '../../use-cases/createProjectUseCase'
import { PrismaProjectRepository } from '../../repositories/prisma/prisma-project-repository'
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository'
import { ResourceNotFoundError } from '../../use-cases/errors/ResourceNotFoundError'

export async function createProject(
request: FastifyRequest,
response: FastifyReply,
) {
const createProjectBodySchema = z.object({
title: z.string(),
tags: z.string(),
link: z.string(),
description: z.string(),
})

const createProjectParamsSchema = z.object({
userId: z.string().uuid(),
})

const { title, tags, link, description } = createProjectBodySchema.parse(
request.body,
)
const { userId } = createProjectParamsSchema.parse(request.params)

const userRepository = new PrismaUsersRepository()
const projectRepository = new PrismaProjectRepository()
const createProjectUseCase = new CreateProjectUseCase(
projectRepository,
userRepository,
)
try {
await createProjectUseCase.execute({
userId,
title,
tags,
link,
description,
})
} catch (error) {
if (error instanceof ResourceNotFoundError) {
return response.status(404).send({ message: error.message })
}

throw error
}

return response.status(201).send()
}
2 changes: 2 additions & 0 deletions src/controller/project/routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { FastifyInstance } from 'fastify'
import { createProject } from './createProject'
import { getProjectsByUserId } from './getProjectsByUserId'

export async function projectRoutes(app: FastifyInstance) {
app.get('/project/:userId', getProjectsByUserId)
app.post('/user/:userId/project', createProject)
}
37 changes: 21 additions & 16 deletions src/controller/session/authUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,38 @@ import { FastifyReply, FastifyRequest } from 'fastify'
import { AuthUserUseCase } from '../../use-cases/authUserUseCase'
import { z } from 'zod'
import { PrismaUsersRepository } from '../../repositories/prisma/prisma-users-repository'
import { InvalidCredentialsError } from '../../use-cases/errors/InvalidCredentialsError'

export async function authUser(
export async function authUser(
request: FastifyRequest,
response: FastifyReply,
) {

const userRepository = new PrismaUsersRepository()
const authUserUseCase = new AuthUserUseCase(userRepository)

const AuthUserUseCaseSchema = z.object({
email: z.string().email(),
password: z.string()
password: z.string(),
})

const { email, password } = AuthUserUseCaseSchema.parse(request.body)
const { email, password } = AuthUserUseCaseSchema.parse(request.body)

const { user } = await authUserUseCase.execute({ email, password })
try {
const { user } = await authUserUseCase.execute({ email, password })

const token = await response.jwtSign(
{},
{
sign: {
sub: user.id
const token = await response.jwtSign(
{},
{
sign: {
sub: user.id,
},
},
},
)


return response.status(200).send({ user, token })
}
)

return response.status(200).send({ user, token })
} catch (e) {
if (e instanceof InvalidCredentialsError) {
return response.status(401).send()
}
}
}
8 changes: 6 additions & 2 deletions src/repositories/prisma/prisma-project-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { ProjectRepository } from '../project-repository'
import { prisma } from '../../lib/prisma'

export class PrismaProjectRepository implements ProjectRepository {
create(_: Prisma.ProjectUncheckedCreateInput): Promise<Project> {
throw new Error('Method not implemented.')
async create(data: Prisma.ProjectUncheckedCreateInput): Promise<Project> {
const project = await prisma.project.create({
data,
})

return project
}

async fetchProjectsByUserId(userId: string): Promise<Project[]> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, describe, it, beforeEach } from 'vitest'

import { CreateProjectUseCase } from './addProjectUseCase'
import { CreateProjectUseCase } from './createProjectUseCase'

import { InMemoryProjectRepository } from '../repositories/in-memory-db/inMemoryProjectRepository'
import { InMemoryUserRepository } from '../repositories/in-memory-db/inMemoryUserRepository'
Expand Down
File renamed without changes.

0 comments on commit 97cb068

Please sign in to comment.