diff --git a/src/repositories/prisma/prisma-project-repository.ts b/src/repositories/prisma/prisma-project-repository.ts index 24fc4c8..e8fcb9f 100644 --- a/src/repositories/prisma/prisma-project-repository.ts +++ b/src/repositories/prisma/prisma-project-repository.ts @@ -1,6 +1,7 @@ import { Prisma, Project } from '@prisma/client' import { ProjectRepository } from '../project-repository' import { prisma } from '../../lib/prisma' +import { ProjectWithUserData } from './prisma-project-with-user-data-type' export class PrismaProjectRepository implements ProjectRepository { async create(data: Prisma.ProjectUncheckedCreateInput): Promise { @@ -21,17 +22,6 @@ export class PrismaProjectRepository implements ProjectRepository { return projects } - async fetchProjectById(projectId: string): Promise { - const project = await prisma.project.findUnique({ - where: { - id: projectId, - }, - }) - - return project - } - - async addPhotoUrl(projectId: string, photoUrl: string): Promise { const project = await prisma.project.update({ where: { @@ -45,27 +35,53 @@ export class PrismaProjectRepository implements ProjectRepository { return project } - async fetchProjectByTags(tags: string[]): Promise { + async fetchProjectByTags(tags: string[]): Promise { const project = await prisma.project.findMany({ where: { tags: { hasEvery: tags }, - + }, + include: { + user: { + select: { + avatar_url: true, + name: true, + surname: true, + }, + }, }, }) return project } - async deleteProjectByID(projectId: string):Promise { - await prisma.project.delete({ + async fetchProjectById( + projectId: string, + ): Promise { + const project = await prisma.project.findUnique({ where: { - id: projectId - } + id: projectId, + }, + include: { + user: { + select: { + avatar_url: true, + name: true, + surname: true, + }, + }, + }, }) - return + return project } + async deleteProjectByID(projectId: string): Promise { + await prisma.project.delete({ + where: { + id: projectId, + }, + }) + } async edit(data: Prisma.ProjectUncheckedCreateInput): Promise { const project = await prisma.project.update({ diff --git a/src/repositories/prisma/prisma-project-with-user-data-type.ts b/src/repositories/prisma/prisma-project-with-user-data-type.ts new file mode 100644 index 0000000..6e5f295 --- /dev/null +++ b/src/repositories/prisma/prisma-project-with-user-data-type.ts @@ -0,0 +1,13 @@ +import { Prisma } from '@prisma/client' + +const userAvatarUrlAndNameData = Prisma.validator()({ + select: { avatar_url: true, name: true, surname: true }, +}) + +const projectsWithUserData = Prisma.validator()({ + include: { user: userAvatarUrlAndNameData }, +}) + +export type ProjectWithUserData = Prisma.ProjectGetPayload< + typeof projectsWithUserData +> diff --git a/src/repositories/project-repository.ts b/src/repositories/project-repository.ts index 6796689..5f6ac90 100644 --- a/src/repositories/project-repository.ts +++ b/src/repositories/project-repository.ts @@ -1,11 +1,12 @@ import { Prisma, Project } from '@prisma/client' +import { ProjectWithUserData } from './prisma/prisma-project-with-user-data-type' export interface ProjectRepository { create(data: Prisma.ProjectUncheckedCreateInput): Promise fetchProjectsByUserId(userId: string): Promise - fetchProjectById(projectId: string): Promise + fetchProjectById(projectId: string): Promise addPhotoUrl(projectId: string, photoUrl: string): Promise - fetchProjectByTags(tags: string[]): Promise + fetchProjectByTags(tags: string[]): Promise edit(data: Prisma.ProjectUncheckedCreateInput): Promise deleteProjectByID(projectID: string): Promise }