Skip to content

Commit

Permalink
Checking if the user data is coming.
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusSanchez committed Feb 3, 2024
1 parent 2fcf61e commit 350d2f9
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 83 deletions.
10 changes: 5 additions & 5 deletions src/controller/project/getProjectById.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ describe('Get Projets By ID E2E', () => {

expect(getProjectByIdResponse.statusCode).toEqual(200)
expect(getProjectByIdResponse.body.project).toEqual(
expect.objectContaining({ title }),
)

expect(getProjectByIdResponse.body.project).toEqual(
expect.objectContaining({ tags }),
expect.objectContaining({
title,
user: { name: 'John', surname: 'Doe', avatar_url: null },
tags,
}),
)
})

Expand Down
18 changes: 15 additions & 3 deletions src/controller/project/getProjectsByTags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,22 @@ describe('Get Projets By Tags E2E', () => {
.post(`/projects/tags`)
.send({ tags })

console.log('getProjectsByTagsResponse')
console.log(getProjectsByTagsResponse.body.projects[0])

expect(getProjectsByTagsResponse.statusCode).toEqual(200)
expect(getProjectsByTagsResponse.body.projects).toHaveLength(2)
expect(getProjectsByTagsResponse.body.projects[0]).toEqual(
expect.objectContaining({ title: 'Project 01' }),
expect.objectContaining({
title: 'Project 01',
user: { name: 'John', surname: 'Doe', avatar_url: null },
}),
)
expect(getProjectsByTagsResponse.body.projects[1]).toEqual(
expect.objectContaining({ title: 'Project 02' }),
expect.objectContaining({
title: 'Project 02',
user: { name: 'John', surname: 'Doe', avatar_url: null },
}),
)
})

Expand Down Expand Up @@ -94,7 +103,10 @@ describe('Get Projets By Tags E2E', () => {
expect(getProjectsByTagsResponse.statusCode).toEqual(200)
expect(getProjectsByTagsResponse.body.projects).toHaveLength(1)
expect(getProjectsByTagsResponse.body.projects[0]).toEqual(
expect.objectContaining({ title: 'Project 03' }),
expect.objectContaining({
title: 'Project 03',
user: { name: 'John', surname: 'Doe', avatar_url: null },
}),
)
})
})
75 changes: 65 additions & 10 deletions src/repositories/in-memory-db/inMemoryProjectRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { randomUUID } from 'crypto'

import { Prisma, Project } from '@prisma/client'
import { ProjectRepository } from '../project-repository'
import { ProjectWithUserData } from '../prisma/prisma-project-with-user-data-type'
import { InMemoryUserRepository } from './inMemoryUserRepository'

export class InMemoryProjectRepository implements ProjectRepository {
public dbProject: Project[] = []
public dbUser: InMemoryUserRepository = new InMemoryUserRepository()

constructor() {}

Expand All @@ -18,6 +21,7 @@ export class InMemoryProjectRepository implements ProjectRepository {
user_id: data.user_id,
created_at: new Date(),
updated_at: new Date(),
photo_url: null,
}

this.dbProject.push(project)
Expand All @@ -43,34 +47,85 @@ export class InMemoryProjectRepository implements ProjectRepository {
return projects
}

async fetchProjectById(projectId: string): Promise<Project | null> {
async fetchProjectById(
projectId: string,
): Promise<ProjectWithUserData | null> {
const project = this.dbProject.find((project) => project.id === projectId)
console.log('project - test')
console.log(project)
if (!project) {
return null
}
return project
}

const {
created_at,
description,
id,
link,
photo_url,
tags,
title,
updated_at,
user_id,
} = project
const foundUser = this.dbUser.db.find((user) => user.id === project.user_id)
console.log(this.dbUser.db)
console.log('foundUser - test')
console.log(foundUser)
if (!foundUser) {
return null
}

return {
created_at,
description,
id,
link,
photo_url,
tags,
title,
updated_at,
user_id,
user: {
name: foundUser?.name,
surname: foundUser?.surname,
avatar_url: foundUser?.avatar_url,
},
}
}

async addPhotoUrl(projectId: string, photoUrl: string): Promise<Project> {
throw new Error('Method not implemented.')
}

async deleteProjectByID(projectId: string): Promise<void> {
const index = this.dbProject.findIndex((project) => project.id === projectId);
const index = this.dbProject.findIndex(
(project) => project.id === projectId,
)

if (index !== -1) {
this.dbProject.splice(index, 1);
this.dbProject.splice(index, 1)
}

return
}
async fetchProjectByTags(tags: string[]): Promise<Project[]> {

async fetchProjectByTags(tags: string[]): Promise<ProjectWithUserData[]> {
const projects = this.dbProject.filter((project) =>
project.tags.some((tag) => tags.includes(tag)),
)

return projects
const projectPromises = projects.map(async (project) => {
const user = await this.dbUser.findById(project.user_id)

return {
...project,
user: {
name: user.name,
surname: user.surname,
avatar_url: user.avatar_url,
},
}
})

return Promise.all(projectPromises)
}
}
33 changes: 4 additions & 29 deletions src/repositories/in-memory-db/inMemoryUserRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,10 @@ import { UserRepository } from '../user-repository'
import { randomUUID } from 'crypto'

export class InMemoryUserRepository implements UserRepository {
private db: User[] = []
public 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;
constructor() {}

// 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 @@ -50,8 +27,6 @@ export class InMemoryUserRepository implements UserRepository {
return User
}

// 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,
Expand All @@ -60,7 +35,7 @@ export class InMemoryUserRepository implements UserRepository {
password_hash,
}: Prisma.UserCreateInput) {
const user: User = {
id: (id == undefined) ? randomUUID() : id,
id: id === undefined ? randomUUID() : id,
name,
surname,

Expand All @@ -69,11 +44,11 @@ export class InMemoryUserRepository implements UserRepository {

created_at: new Date(),
updated_at: new Date(),
avatar_url: null,
}

this.db.push(user)

return user
}

}
15 changes: 11 additions & 4 deletions src/use-cases/project/editProjectUseCase.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { expect, describe, it, beforeEach } from 'vitest'

import { InMemoryProjectRepository } from '../../repositories/in-memory-db/inMemoryProjectRepository'
import { ProjectRepository } from '../../repositories/project-repository'
import { ResourceNotFoundError } from '../errors/ResourceNotFoundError'
import { EditProjectUseCase } from './editProjectUseCase'
import { User } from '@prisma/client'

let projectRepository: ProjectRepository
let projectRepository: InMemoryProjectRepository
let editProjectUseCase: EditProjectUseCase
let newUser: User

describe('Edit Project By Id Use Case', () => {
beforeEach(() => {
beforeEach(async () => {
projectRepository = new InMemoryProjectRepository()
editProjectUseCase = new EditProjectUseCase(projectRepository)
newUser = await projectRepository.dbUser.create({
name: 'John',
surname: 'Doe',
email: '[email protected]',
password_hash: '123456',
})
})

it('should be able edit one project by ID', async () => {
Expand All @@ -20,7 +27,7 @@ describe('Edit Project By Id Use Case', () => {
description: 'Best Project',
tags: ['react', 'node'],
link: 'https://github.com/luiseduardo3/nodets-petcanil',
user_id: 'user_id',
user_id: newUser.id,
})

const { project: projectEdited } = await editProjectUseCase.execute({
Expand Down
12 changes: 9 additions & 3 deletions src/use-cases/project/getProjectsByIdUseCase.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { expect, describe, it, beforeEach } from 'vitest'

import { InMemoryProjectRepository } from '../../repositories/in-memory-db/inMemoryProjectRepository'
import { ProjectRepository } from '../../repositories/project-repository'
import { ResourceNotFoundError } from '../errors/ResourceNotFoundError'
import { GetProjectsByIdUseCase } from './getProjectsByIdUseCase'

let projectRepository: ProjectRepository
let projectRepository: InMemoryProjectRepository
let getProjectByIdUseCase: GetProjectsByIdUseCase

describe('Get Project By Id Use Case', () => {
Expand All @@ -15,12 +14,18 @@ describe('Get Project By Id Use Case', () => {
})

it('should be able get project by ID', async () => {
const newUser = await projectRepository.dbUser.create({
name: 'John',
surname: 'Doe',
email: '[email protected]',
password_hash: '123456',
})
const newProject = await projectRepository.create({
title: 'React Typescript 1',
description: 'Best Project',
tags: ['react', 'node'],
link: 'https://github.com/luiseduardo3/nodets-petcanil',
user_id: 'user_id',
user_id: newUser.id,
})

const { project } = await getProjectByIdUseCase.execute({
Expand All @@ -32,6 +37,7 @@ describe('Get Project By Id Use Case', () => {
title: 'React Typescript 1',
id: newProject.id,
tags: ['react', 'node'],
user: { name: 'John', surname: 'Doe', avatar_url: null },
}),
)
})
Expand Down
5 changes: 2 additions & 3 deletions src/use-cases/project/getProjectsByIdUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Project } from '@prisma/client'

import { ProjectRepository } from '../../repositories/project-repository'

import { ResourceNotFoundError } from '../errors/ResourceNotFoundError'
import { ProjectWithUserData } from '../../repositories/prisma/prisma-project-with-user-data-type'

interface GetProjectByIdRequest {
projectId: string
}

interface GetProjectByIdResponse {
project: Project
project: ProjectWithUserData
}

export class GetProjectsByIdUseCase {
Expand Down
Loading

0 comments on commit 350d2f9

Please sign in to comment.