-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(Repositories): Some prisma repositories
- Loading branch information
1 parent
6543659
commit a12bb0e
Showing
14 changed files
with
472 additions
and
11 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
prisma/migrations/20240215201244_attached_at_field_on_course_tag/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: '' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/infra/database/prisma/mappers/student-certificate-mapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/infra/database/prisma/repositories/prisma-certificates-repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/infra/database/prisma/repositories/prisma-course-tags-repository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.