-
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.
Refactor(Video, Image, Upload): Refactor upload routes
- Loading branch information
1 parent
bee7a15
commit 092c5a7
Showing
24 changed files
with
238 additions
and
27 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
"cSpell.words": [ | ||
"accesstoken", | ||
"authtoken", | ||
"codespark", | ||
"dtos", | ||
"fastify", | ||
"originalname" | ||
|
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 |
---|---|---|
|
@@ -55,4 +55,4 @@ | |
"vite-tsconfig-paths": "^4.3.1", | ||
"vitest": "^1.2.1" | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
prisma/migrations/20240222230341_turn_file_key_optional/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 "files" ALTER COLUMN "key" DROP NOT NULL; |
13 changes: 13 additions & 0 deletions
13
prisma/migrations/20240222232146_create_image_model/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,13 @@ | ||
-- CreateTable | ||
CREATE TABLE "images" ( | ||
"id" TEXT NOT NULL, | ||
"file_key" TEXT, | ||
|
||
CONSTRAINT "images_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "images_file_key_key" ON "images"("file_key"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "images" ADD CONSTRAINT "images_file_key_fkey" FOREIGN KEY ("file_key") REFERENCES "files"("key") ON DELETE SET NULL ON UPDATE CASCADE; |
8 changes: 8 additions & 0 deletions
8
prisma/migrations/20240222232304_reset_file_key_optional/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,8 @@ | ||
/* | ||
Warnings: | ||
- Made the column `key` on table `files` required. This step will fail if there are existing NULL values in that column. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "files" ALTER COLUMN "key" SET NOT NULL; |
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
File renamed without changes.
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
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
6 changes: 3 additions & 3 deletions
6
src/infra/database/prisma/mappers/factories/make-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
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,34 @@ | ||
import { UniqueEntityID } from '@/core/entities/unique-entity-id' | ||
import { Image } from '@/domain/course-management/enterprise/entities/image' | ||
import { type Prisma } from '@prisma/client' | ||
|
||
export class ImageMapper { | ||
static toDomain(raw: Prisma.ImageGetPayload<{ include: { file: true } }>): Image | null { | ||
if (!raw.file) { | ||
return null | ||
} | ||
|
||
if (raw.file.type !== 'image/jpeg' && raw.file.type !== 'image/png') { | ||
return null | ||
} | ||
|
||
return Image.create( | ||
{ | ||
imageName: raw.file.name, | ||
imageKey: raw.fileKey, | ||
body: raw.file.body, | ||
imageType: raw.file.type as 'image/jpeg' | 'image/png', | ||
size: Number(raw.file.size), | ||
storedAt: raw.file.storedAt | ||
}, | ||
new UniqueEntityID(raw.id) | ||
) | ||
} | ||
|
||
static toPrisma(image: Image): Prisma.ImageUncheckedCreateInput { | ||
return { | ||
id: image.id.toString(), | ||
fileKey: image.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
83 changes: 83 additions & 0 deletions
83
src/infra/database/prisma/repositories/prisma-images-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,83 @@ | ||
import { DomainEvents } from '@/core/events/domain-events' | ||
import { type ImagesRepository } from '@/domain/course-management/application/repositories/images-repository' | ||
import { type Image } from '@/domain/course-management/enterprise/entities/image' | ||
import { prisma } from '..' | ||
import { ImageMapper } from '../mappers/image-mapper' | ||
|
||
export class PrismaImagesRepository implements ImagesRepository { | ||
async findById(id: string): Promise<Image | null> { | ||
const image = await prisma.image.findUnique({ | ||
where: { | ||
id | ||
}, | ||
include: { | ||
file: true | ||
} | ||
}) | ||
|
||
if (!image) { | ||
return null | ||
} | ||
|
||
const domainImage = ImageMapper.toDomain(image) | ||
|
||
return domainImage | ||
} | ||
|
||
async findByImageKey(key: string): Promise<Image | null> { | ||
const image = await prisma.image.findUnique({ | ||
where: { | ||
fileKey: key | ||
}, | ||
include: { | ||
file: true | ||
} | ||
}) | ||
|
||
if (!image) { | ||
return null | ||
} | ||
|
||
const domainImage = ImageMapper.toDomain(image) | ||
|
||
return domainImage | ||
} | ||
|
||
async appendImageKey(imageKey: string, imageId: string): Promise<Image | null> { | ||
const image = await prisma.image.findUnique({ | ||
where: { | ||
id: imageId | ||
}, | ||
include: { | ||
file: true | ||
} | ||
}) | ||
|
||
if (!image) { | ||
return null | ||
} | ||
|
||
await prisma.image.update({ | ||
where: { id: imageId }, | ||
data: { | ||
fileKey: imageKey | ||
} | ||
}) | ||
|
||
const domainImage = ImageMapper.toDomain(image) | ||
|
||
return domainImage | ||
} | ||
|
||
async create(image: Image): Promise<Image> { | ||
const infraImage = ImageMapper.toPrisma(image) | ||
|
||
await prisma.image.create({ | ||
data: infraImage | ||
}) | ||
|
||
DomainEvents.dispatchEventsForAggregate(image.id) | ||
|
||
return image | ||
} | ||
} |
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
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,10 @@ | ||
import { type Prisma } from '@prisma/client' | ||
|
||
export class ImagePresenter { | ||
static toHTTP(image: Prisma.ImageUncheckedCreateInput) { | ||
return { | ||
id: image.id, | ||
imageKey: image.fileKey | ||
} | ||
} | ||
} |
Oops, something went wrong.