Skip to content

Commit

Permalink
Fixing prisma types
Browse files Browse the repository at this point in the history
  • Loading branch information
MatheusSanchez committed Feb 3, 2024
1 parent 9218cb1 commit 2fcf61e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
52 changes: 34 additions & 18 deletions src/repositories/prisma/prisma-project-repository.ts
Original file line number Diff line number Diff line change
@@ -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<Project> {
Expand All @@ -21,17 +22,6 @@ export class PrismaProjectRepository implements ProjectRepository {
return projects
}

async fetchProjectById(projectId: string): Promise<Project | null> {
const project = await prisma.project.findUnique({
where: {
id: projectId,
},
})

return project
}


async addPhotoUrl(projectId: string, photoUrl: string): Promise<Project> {
const project = await prisma.project.update({
where: {
Expand All @@ -45,27 +35,53 @@ export class PrismaProjectRepository implements ProjectRepository {
return project
}

async fetchProjectByTags(tags: string[]): Promise<Project[]> {
async fetchProjectByTags(tags: string[]): Promise<ProjectWithUserData[]> {
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<void> {
await prisma.project.delete({
async fetchProjectById(
projectId: string,
): Promise<ProjectWithUserData | null> {
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<void> {
await prisma.project.delete({
where: {
id: projectId,
},
})
}

async edit(data: Prisma.ProjectUncheckedCreateInput): Promise<Project> {
const project = await prisma.project.update({
Expand Down
13 changes: 13 additions & 0 deletions src/repositories/prisma/prisma-project-with-user-data-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Prisma } from '@prisma/client'

const userAvatarUrlAndNameData = Prisma.validator<Prisma.UserDefaultArgs>()({
select: { avatar_url: true, name: true, surname: true },
})

const projectsWithUserData = Prisma.validator<Prisma.ProjectDefaultArgs>()({
include: { user: userAvatarUrlAndNameData },
})

export type ProjectWithUserData = Prisma.ProjectGetPayload<
typeof projectsWithUserData
>
5 changes: 3 additions & 2 deletions src/repositories/project-repository.ts
Original file line number Diff line number Diff line change
@@ -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<Project>
fetchProjectsByUserId(userId: string): Promise<Project[]>
fetchProjectById(projectId: string): Promise<Project | null>
fetchProjectById(projectId: string): Promise<ProjectWithUserData | null>
addPhotoUrl(projectId: string, photoUrl: string): Promise<Project>
fetchProjectByTags(tags: string[]): Promise<Project[]>
fetchProjectByTags(tags: string[]): Promise<ProjectWithUserData[]>
edit(data: Prisma.ProjectUncheckedCreateInput): Promise<Project>
deleteProjectByID(projectID: string): Promise<void>
}

0 comments on commit 2fcf61e

Please sign in to comment.