Skip to content

Commit

Permalink
Merge pull request #26 from Artur-Poffo/feat-implement-prisma-reposit…
Browse files Browse the repository at this point in the history
…ories

Feat(Prisma repositories): Implement prisma repositories
  • Loading branch information
Artur-Poffo authored Feb 15, 2024
2 parents 993d226 + 4157384 commit a2cdad8
Show file tree
Hide file tree
Showing 53 changed files with 2,690 additions and 583 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@typescript-eslint/no-misused-promises": "off",
"@typescript-eslint/no-unused-expressions": "off",
"no-new": "off",
"no-self-assign": "off"
"no-self-assign": "off",
"@typescript-eslint/no-unsafe-argument": "off"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Warnings:
- Changed the type of `item_type` on the `EnrollmentCompletedItem` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
*/
-- CreateEnum
CREATE TYPE "EnrollmentCompletedItemTypes" AS ENUM ('CLASS', 'MODULE');

-- CreateEnum
CREATE TYPE "VideoTypes" AS ENUM ('MP4', 'AVI');

-- CreateEnum
CREATE TYPE "ImageTypes" AS ENUM ('JPEG', 'PNG');

-- AlterTable
ALTER TABLE "EnrollmentCompletedItem" DROP COLUMN "item_type",
ADD COLUMN "item_type" "EnrollmentCompletedItemTypes" NOT NULL;

-- DropEnum
DROP TYPE "EnrollmentCompletedItemType";

-- CreateTable
CREATE TABLE "files" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"key" TEXT NOT NULL,
"size" DECIMAL(65,30) NOT NULL,
"stored_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "files_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "videos" (
"id" TEXT NOT NULL,
"type" "VideoTypes" NOT NULL,
"duration" DECIMAL(65,30) NOT NULL,
"file_id" TEXT NOT NULL,

CONSTRAINT "videos_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "images" (
"id" TEXT NOT NULL,
"type" "ImageTypes" NOT NULL,
"file_id" TEXT NOT NULL,

CONSTRAINT "images_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "files_key_key" ON "files"("key");

-- AddForeignKey
ALTER TABLE "videos" ADD CONSTRAINT "videos_file_id_fkey" FOREIGN KEY ("file_id") REFERENCES "files"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "images" ADD CONSTRAINT "images_file_id_fkey" FOREIGN KEY ("file_id") REFERENCES "files"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
31 changes: 31 additions & 0 deletions prisma/migrations/20240214204313_global_file_model/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Warnings:
- You are about to drop the `images` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `videos` table. If the table is not empty, all the data it contains will be lost.
- Added the required column `type` to the `files` table without a default value. This is not possible if the table is not empty.
*/
-- CreateEnum
CREATE TYPE "FileTypes" AS ENUM ('MP4', 'AVI', 'JPEG', 'PNG');

-- DropForeignKey
ALTER TABLE "images" DROP CONSTRAINT "images_file_id_fkey";

-- DropForeignKey
ALTER TABLE "videos" DROP CONSTRAINT "videos_file_id_fkey";

-- AlterTable
ALTER TABLE "files" ADD COLUMN "type" "FileTypes" NOT NULL;

-- DropTable
DROP TABLE "images";

-- DropTable
DROP TABLE "videos";

-- DropEnum
DROP TYPE "ImageTypes";

-- DropEnum
DROP TYPE "VideoTypes";
12 changes: 12 additions & 0 deletions prisma/migrations/20240214204910_file_type_to_string/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:
- Changed the type of `type` on the `files` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
*/
-- AlterTable
ALTER TABLE "files" DROP COLUMN "type",
ADD COLUMN "type" TEXT NOT NULL;

-- DropEnum
DROP TYPE "FileTypes";
25 changes: 25 additions & 0 deletions prisma/migrations/20240214205840_video_model/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Warnings:
- You are about to drop the column `video_key` on the `classes` table. All the data in the column will be lost.
- Added the required column `video_id` to the `classes` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "classes" DROP COLUMN "video_key",
ADD COLUMN "video_id" TEXT NOT NULL;

-- CreateTable
CREATE TABLE "videos" (
"id" TEXT NOT NULL,
"duration" DECIMAL(65,30) NOT NULL,
"file_id" TEXT NOT NULL,

CONSTRAINT "videos_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "classes" ADD CONSTRAINT "classes_video_id_fkey" FOREIGN KEY ("video_id") REFERENCES "videos"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "videos" ADD CONSTRAINT "videos_file_id_fkey" FOREIGN KEY ("file_id") REFERENCES "files"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
17 changes: 17 additions & 0 deletions prisma/migrations/20240214222404_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Warnings:
- A unique constraint covering the columns `[file_id]` on the table `videos` will be added. If there are existing duplicate values, this will fail.
*/
-- DropForeignKey
ALTER TABLE "videos" DROP CONSTRAINT "videos_file_id_fkey";

-- AlterTable
ALTER TABLE "videos" ALTER COLUMN "file_id" DROP NOT NULL;

-- CreateIndex
CREATE UNIQUE INDEX "videos_file_id_key" ON "videos"("file_id");

-- AddForeignKey
ALTER TABLE "videos" ADD CONSTRAINT "videos_file_id_fkey" FOREIGN KEY ("file_id") REFERENCES "files"("id") ON DELETE SET NULL ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Warnings:
- Made the column `file_id` on table `videos` required. This step will fail if there are existing NULL values in that column.
*/
-- DropForeignKey
ALTER TABLE "videos" DROP CONSTRAINT "videos_file_id_fkey";

-- AlterTable
ALTER TABLE "videos" ALTER COLUMN "file_id" SET NOT NULL;

-- AddForeignKey
ALTER TABLE "videos" ADD CONSTRAINT "videos_file_id_fkey" FOREIGN KEY ("file_id") REFERENCES "files"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:
- Added the required column `instructor_id` to the `courses` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "courses" ADD COLUMN "instructor_id" TEXT NOT NULL;

-- AddForeignKey
ALTER TABLE "courses" ADD CONSTRAINT "courses_instructor_id_fkey" FOREIGN KEY ("instructor_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
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;
73 changes: 52 additions & 21 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ model User {
bannerImageKey String? @map("banner_image_key")
registeredAt DateTime @default(now()) @map("registered_at")
Enrollment Enrollment[]
StudentCertificate StudentCertificate[]
Evaluation Evaluation[]
enrollments Enrollment[]
studentCertificates StudentCertificate[]
evaluations Evaluation[]
courses Course[]
@@map("users")
}
Expand All @@ -39,11 +40,14 @@ model Course {
coverImageKey String? @map("cover_image_key")
bannerImageKey String? @map("banner_image_key")
createdAt DateTime @default(now()) @map("created_at")
instructor User @relation(fields: [instructorId], references: [id])
CourseTag CourseTag[]
Enrollment Enrollment[]
Module Module[]
Certificate Certificate?
courseTags CourseTag[]
enrollments Enrollment[]
modules Module[]
certificate Certificate?
instructorId String @map("instructor_id")
@@map("courses")
}
Expand All @@ -53,15 +57,16 @@ model Tag {
value String @unique()
addedAt DateTime @default(now()) @map("added_at")
CourseTag CourseTag[]
courseTags CourseTag[]
@@map("tags")
}

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 All @@ -84,16 +89,16 @@ model Enrollment {
@@map("enrollments")
}

enum EnrollmentCompletedItemType {
enum EnrollmentCompletedItemTypes {
CLASS
MODULE
}

model EnrollmentCompletedItem {
id Int @id @default(autoincrement())
enrollment Enrollment @relation(fields: [enrollmentId], references: [id])
itemType EnrollmentCompletedItemType @map("item_type")
itemId String @map("item_id")
id Int @id @default(autoincrement())
enrollment Enrollment @relation(fields: [enrollmentId], references: [id])
itemType EnrollmentCompletedItemTypes @map("item_type")
itemId String @map("item_id")
enrollmentId String @map("enrollment_id")
}
Expand All @@ -118,7 +123,7 @@ model Module {
moduleNumber Int @map("module_number")
course Course @relation(fields: [courseId], references: [id])
Class Class[]
classes Class[]
courseId String @map("course_id")
Expand All @@ -130,7 +135,7 @@ model Certificate {
imageKey String @unique() @map("image_key")
course Course @relation(fields: [courseId], references: [id])
studentCertificate StudentCertificate[]
studentCertificates StudentCertificate[]
courseId String @unique @map("course_id")
Expand All @@ -153,12 +158,38 @@ model Class {
id String @id @default(uuid())
name String
description String
videoKey String @map("video_key")
classNumber Int @map("class_number")
video Video @relation(fields: [videoId], references: [id])
module Module @relation(fields: [moduleId], references: [id])
moduleId String @map("module_id")
Evaluation Evaluation[]
evaluations Evaluation[]
moduleId String @map("module_id")
videoId String @map("video_id")
@@map("classes")
}

model File {
id String @id @default(uuid())
name String
type String
key String @unique()
size Decimal
storedAt DateTime @default(now()) @map("stored_at")
video Video?
@@map("files")
}

model Video {
id String @id @default(uuid())
duration Decimal
file File? @relation(fields: [fileId], references: [id])
classes Class[]
fileId String @unique() @map("file_id")
@@map("videos")
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface EvaluationsRepository {
findById: (id: string) => Promise<Evaluation | null>
findByStudentIdAndClassId: (studentId: string, classId: string) => Promise<Evaluation | null>
findManyByCourseId: (courseId: string) => Promise<Evaluation[]>
findManyByClassId: (classId: string) => Promise<Evaluation[]>
create: (evaluation: Evaluation) => Promise<Evaluation>
save: (evaluation: Evaluation) => Promise<void>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { type Video } from '../../enterprise/entities/video'
export interface VideosRepository {
findById: (id: string) => Promise<Video | null>
appendVideoKey: (videoKey: string, videoId: string) => Promise<Video | null>
create: (video: Video) => Promise<Video>
create: (video: Video) => Promise<Video | null>
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import { type File } from '../../enterprise/entities/file'

export interface FilesRepository {
findById: (id: string) => Promise<File | null>
findByKey: (key: string) => Promise<File | null>
create: (file: File) => Promise<File>
}
6 changes: 6 additions & 0 deletions src/infra/database/prisma/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { env } from '@/infra/env'
import { PrismaClient } from '@prisma/client'

export const prisma = new PrismaClient({
log: env.NODE_ENV === 'development' ? ['query'] : []
})
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: ''
}
}
}
Loading

0 comments on commit a2cdad8

Please sign in to comment.