Skip to content

Commit

Permalink
Feat(Repositories): Some prisma repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur-Poffo committed Feb 15, 2024
1 parent 6543659 commit a12bb0e
Show file tree
Hide file tree
Showing 14 changed files with 472 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "course_tags" ADD COLUMN "attachedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
7 changes: 4 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ model Tag {
}

model CourseTag {
id String @id @default(uuid())
course Course @relation(fields: [courseId], references: [id])
tag Tag @relation(fields: [tagId], references: [id])
id String @id @default(uuid())
attachedAt DateTime @default(now())
course Course @relation(fields: [courseId], references: [id])
tag Tag @relation(fields: [tagId], references: [id])
courseId String @map("course_id")
tagId String @map("tag_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export class CourseTag extends Entity<CourseTagProps> {
return this.props.tagId
}

get attachedAt() {
return this.props.attachedAt
}

static create(
props: Optional<CourseTagProps, 'attachedAt'>,
id?: UniqueEntityID
Expand Down
23 changes: 23 additions & 0 deletions src/infra/database/prisma/mappers/certificate-mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { UniqueEntityID } from '@/core/entities/unique-entity-id'
import { Certificate } from '@/domain/course-management/enterprise/entities/certificate'
import { type Prisma, type Certificate as PrismaCertificate } from '@prisma/client'

export class CertificateMapper {
static toDomain(raw: PrismaCertificate): Certificate {
return Certificate.create(
{
courseId: new UniqueEntityID(raw.courseId),
imageId: new UniqueEntityID('')
},
new UniqueEntityID(raw.id)
)
}

static toPrisma(certificate: Certificate): Prisma.CertificateUncheckedCreateInput {
return {
id: certificate.id.toString(),
courseId: certificate.courseId.toString(),
imageKey: ''
}
}
}
25 changes: 25 additions & 0 deletions src/infra/database/prisma/mappers/course-tag-mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { UniqueEntityID } from '@/core/entities/unique-entity-id'
import { CourseTag } from '@/domain/course-management/enterprise/entities/course-tag'
import { type Prisma, type CourseTag as PrismaCourseTag } from '@prisma/client'

export class CourseTagMapper {
static toDomain(raw: PrismaCourseTag): CourseTag {
return CourseTag.create(
{
courseId: new UniqueEntityID(raw.courseId),
tagId: new UniqueEntityID(raw.tagId),
attachedAt: raw.attachedAt
},
new UniqueEntityID(raw.id)
)
}

static toPrisma(courseTag: CourseTag): Prisma.CourseTagUncheckedCreateInput {
return {
id: courseTag.id.toString(),
courseId: courseTag.courseId.toString(),
tagId: courseTag.tagId.toString(),
attachedAt: courseTag.attachedAt
}
}
}
27 changes: 27 additions & 0 deletions src/infra/database/prisma/mappers/evaluation-mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { UniqueEntityID } from '@/core/entities/unique-entity-id'
import { Evaluation } from '@/domain/course-management/enterprise/entities/evaluation'
import { type Prisma, type Evaluation as PrismaEvaluation } from '@prisma/client'

export class EvaluationMapper {
static toDomain(raw: PrismaEvaluation): Evaluation {
return Evaluation.create(
{
classId: new UniqueEntityID(raw.classId),
studentId: new UniqueEntityID(raw.userId),
value: raw.value,
createdAt: raw.createdAt
},
new UniqueEntityID(raw.id)
)
}

static toPrisma(evaluation: Evaluation): Prisma.EvaluationUncheckedCreateInput {
return {
id: evaluation.id.toString(),
classId: evaluation.classId.toString(),
userId: evaluation.studentId.toString(),
value: evaluation.value,
createdAt: evaluation.createdAt
}
}
}
25 changes: 25 additions & 0 deletions src/infra/database/prisma/mappers/student-certificate-mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { UniqueEntityID } from '@/core/entities/unique-entity-id'
import { StudentCertificate } from '@/domain/course-management/enterprise/entities/student-certificate'
import { type Prisma, type StudentCertificate as PrismaStudentCertificate } from '@prisma/client'

export class StudentCertificateMapper {
static toDomain(raw: PrismaStudentCertificate): StudentCertificate {
return StudentCertificate.create(
{
certificateId: new UniqueEntityID(raw.certificateId),
studentId: new UniqueEntityID(raw.userId),
issuedAt: raw.issuedAt
},
new UniqueEntityID(raw.id)
)
}

static toPrisma(studentCertificate: StudentCertificate): Prisma.StudentCertificateUncheckedCreateInput {
return {
id: studentCertificate.id.toString(),
certificateId: studentCertificate.certificateId.toString(),
userId: studentCertificate.studentId.toString(),
issuedAt: studentCertificate.issuedAt
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { type CertificatesRepository } from '@/domain/course-management/application/repositories/certificates-repository'
import { type Certificate } from '@/domain/course-management/enterprise/entities/certificate'
import { prisma } from '..'
import { CertificateMapper } from '../mappers/certificate-mapper'

export class PrismaCertificatesRepository implements CertificatesRepository {
async findById(id: string): Promise<Certificate | null> {
const certificate = await prisma.certificate.findUnique({
where: {
id
}
})

if (!certificate) {
return null
}

const domainCertificate = CertificateMapper.toDomain(certificate)

return domainCertificate
}

async findByCourseId(courseId: string): Promise<Certificate | null> {
const certificate = await prisma.certificate.findUnique({
where: {
courseId
}
})

if (!certificate) {
return null
}

const domainCertificate = CertificateMapper.toDomain(certificate)

return domainCertificate
}

async create(certificate: Certificate): Promise<Certificate> {
const infraCertificate = CertificateMapper.toPrisma(certificate)

await prisma.certificate.create({
data: infraCertificate
})

return certificate
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { type CourseTagsRepository } from '@/domain/course-management/application/repositories/course-tags-repository'
import { type CourseTag } from '@/domain/course-management/enterprise/entities/course-tag'
import { prisma } from '..'
import { CourseTagMapper } from '../mappers/course-tag-mapper'

export class PrismaCourseTagsRepository implements CourseTagsRepository {
async findById(id: string): Promise<CourseTag | null> {
const courseTag = await prisma.courseTag.findUnique({
where: {
id
}
})

if (!courseTag) {
return null
}

const domainCourseTag = CourseTagMapper.toDomain(courseTag)

return domainCourseTag
}

async findManyByCourseId(courseId: string): Promise<CourseTag[]> {
const courseTags = await prisma.courseTag.findMany({
where: {
courseId
}
})

return courseTags.map(courseTag => CourseTagMapper.toDomain(courseTag))
}

async findManyByTagId(tagId: string): Promise<CourseTag[]> {
const courseTags = await prisma.courseTag.findMany({
where: {
tagId
}
})

return courseTags.map(courseTag => CourseTagMapper.toDomain(courseTag))
}

async findAll(): Promise<CourseTag[]> {
const courseTags = await prisma.courseTag.findMany()
return courseTags.map(courseTag => CourseTagMapper.toDomain(courseTag))
}

async create(courseTag: CourseTag): Promise<CourseTag> {
const infraCourseTag = CourseTagMapper.toPrisma(courseTag)

await prisma.courseTag.create({
data: infraCourseTag
})

return courseTag
}
}
Loading

0 comments on commit a12bb0e

Please sign in to comment.