Skip to content

Commit

Permalink
Refactor(Video, Image): Turn image/video key required
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur-Poffo committed Feb 25, 2024
1 parent c036c57 commit 0fbdaac
Show file tree
Hide file tree
Showing 20 changed files with 50 additions and 196 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Warnings:
- Made the column `file_key` on table `images` required. This step will fail if there are existing NULL values in that column.
- Made the column `file_key` on table `videos` required. This step will fail if there are existing NULL values in that column.
*/
-- DropForeignKey
ALTER TABLE "images" DROP CONSTRAINT "images_file_key_fkey";

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

-- AlterTable
ALTER TABLE "images" ALTER COLUMN "file_key" SET NOT NULL;

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

-- AddForeignKey
ALTER TABLE "videos" ADD CONSTRAINT "videos_file_key_fkey" FOREIGN KEY ("file_key") REFERENCES "files"("key") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "images" ADD CONSTRAINT "images_file_key_fkey" FOREIGN KEY ("file_key") REFERENCES "files"("key") ON DELETE RESTRICT ON UPDATE CASCADE;
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ model Video {
classes Class[]
fileKey String? @unique() @map("file_key")
fileKey String @unique() @map("file_key")
@@map("videos")
}
Expand All @@ -203,7 +203,7 @@ model Image {
id String @id @default(uuid())
file File? @relation(fields: [fileKey], references: [key])
fileKey String? @unique() @map("file_key")
fileKey String @unique() @map("file_key")
@@map("images")
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ describe('Get image details use case', async () => {
it('should be able to get image details', async () => {
const name = 'john-doe-image.jpeg'

const image = makeImage({ imageName: name })
const image = makeImage({ imageName: name, imageKey: '23323232-image.jpeg' })

await inMemoryImagesRepository.create(image)

const result = await sut.exec({
imageId: image.id.toString()
fileKey: image.imageKey
})

expect(result.isRight()).toBe(true)
Expand All @@ -33,7 +33,7 @@ describe('Get image details use case', async () => {

it('should not be able to get image details of a inexistent image', async () => {
const result = await sut.exec({
imageId: 'inexistentImageId'
fileKey: 'inexistentImageId'
})

expect(result.isLeft()).toBe(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { type Image } from '../../enterprise/entities/image'
import { type ImagesRepository } from '../repositories/images-repository'

interface GetImageDetailsUseCaseRequest {
imageId: string
fileKey: string
}

type GetImageDetailsUseCaseResponse = Either<
Expand All @@ -21,9 +21,9 @@ export class GetImageDetailsUseCase implements UseCase<GetImageDetailsUseCaseReq
) { }

async exec({
imageId
fileKey
}: GetImageDetailsUseCaseRequest): Promise<GetImageDetailsUseCaseResponse> {
const image = await this.imagesRepository.findById(imageId)
const image = await this.imagesRepository.findByImageKey(fileKey)

if (!image) {
return left(new ResourceNotFoundError())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ describe('Get video details use case', async () => {
it('should be able to get video details', async () => {
const name = 'john-doe-video.mp4'

const video = makeVideo({ videoName: name })
const video = makeVideo({ videoName: name, videoKey: '2332323-video.mp4' })

await inMemoryVideosRepository.create(video)

const result = await sut.exec({
videoId: video.id.toString()
fileKey: video.videoKey
})

expect(result.isRight()).toBe(true)
Expand All @@ -33,7 +33,7 @@ describe('Get video details use case', async () => {

it('should not be able to get video details of a inexistent video', async () => {
const result = await sut.exec({
videoId: 'inexistentVideoId'
fileKey: 'inexistentVideoId'
})

expect(result.isLeft()).toBe(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { type Video } from '../../enterprise/entities/video'
import { type VideosRepository } from '../repositories/videos-repository'

interface GetVideoDetailsUseCaseRequest {
videoId: string
fileKey: string
}

type GetVideoDetailsUseCaseResponse = Either<
Expand All @@ -21,9 +21,9 @@ export class GetVideoDetailsUseCase implements UseCase<GetVideoDetailsUseCaseReq
) { }

async exec({
videoId
fileKey
}: GetVideoDetailsUseCaseRequest): Promise<GetVideoDetailsUseCaseResponse> {
const video = await this.videosRepository.findById(videoId)
const video = await this.videosRepository.findByVideoKey(fileKey)

if (!video) {
return left(new ResourceNotFoundError())
Expand Down

This file was deleted.

49 changes: 0 additions & 49 deletions src/domain/course-management/application/use-cases/upload-image.ts

This file was deleted.

This file was deleted.

52 changes: 0 additions & 52 deletions src/domain/course-management/application/use-cases/upload-video.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/domain/course-management/enterprise/entities/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface ImageProps {
imageType: 'image/jpeg' | 'image/png'
body: Buffer
size: number
imageKey?: string | null
imageKey: string
storedAt: Date
}

Expand Down
2 changes: 1 addition & 1 deletion src/domain/course-management/enterprise/entities/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface VideoProps {
body: Buffer
duration: number
size: number
videoKey?: string | null
videoKey: string
storedAt: Date
}

Expand Down
6 changes: 3 additions & 3 deletions src/infra/http/controllers/get-image-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { z } from 'zod'
import { ImagePresenter } from '../presenters/image-presenter'

const getImageDetailsParamsSchema = z.object({
imageId: z.string().uuid()
fileKey: z.string()
})

export async function getImageDetailsController(request: FastifyRequest, reply: FastifyReply) {
const { imageId } = getImageDetailsParamsSchema.parse(request.params)
const { fileKey } = getImageDetailsParamsSchema.parse(request.params)

const getImageInfoUseCase = makeGetImageDetailsUseCase()

const result = await getImageInfoUseCase.exec({
imageId
fileKey
})

if (result.isLeft()) {
Expand Down
6 changes: 3 additions & 3 deletions src/infra/http/controllers/get-video-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { z } from 'zod'
import { VideoPresenter } from '../presenters/video-presenter'

const getVideoDetailsParamsSchema = z.object({
videoId: z.string().uuid()
fileKey: z.string()
})

export async function getVideoDetailsController(request: FastifyRequest, reply: FastifyReply) {
const { videoId } = getVideoDetailsParamsSchema.parse(request.params)
const { fileKey } = getVideoDetailsParamsSchema.parse(request.params)

const getVideoInfoUseCase = makeGetVideoDetailsUseCase()

const result = await getVideoInfoUseCase.exec({
videoId
fileKey
})

if (result.isLeft()) {
Expand Down
2 changes: 1 addition & 1 deletion src/infra/http/routes/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { getImageDetailsController } from '../controllers/get-image-details'
import { verifyJwt } from '../middlewares/verify-jwt'

export async function imageRoutes(app: FastifyInstance) {
app.get('/images/:imageId', { onRequest: [verifyJwt] }, getImageDetailsController)
app.get('/images/:fileKey', { onRequest: [verifyJwt] }, getImageDetailsController)
}
2 changes: 1 addition & 1 deletion src/infra/http/routes/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { getVideoDetailsController } from '../controllers/get-video-details'
import { verifyJwt } from '../middlewares/verify-jwt'

export async function videoRoutes(app: FastifyInstance) {
app.get('/videos/:videoId', { onRequest: [verifyJwt] }, getVideoDetailsController)
app.get('/videos/:videoKey', { onRequest: [verifyJwt] }, getVideoDetailsController)
}
12 changes: 0 additions & 12 deletions src/infra/use-cases/factories/make-upload-image-use-case.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/infra/use-cases/factories/make-upload-video-use-case.ts

This file was deleted.

1 change: 1 addition & 0 deletions test/factories/make-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function makeImage(
imageName: faker.company.name(),
imageType: 'image/jpeg',
body: Buffer.from(faker.lorem.slug()),
imageKey: override.imageKey ?? '12345-image.png',
size: faker.number.int(),
...override
},
Expand Down
1 change: 1 addition & 0 deletions test/factories/make-video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function makeVideo(
videoName: faker.company.name(),
videoType: 'video/mp4',
body: Buffer.from(faker.lorem.slug()),
videoKey: override.videoKey ?? '12345-video.mp4',
size: faker.number.int(),
duration: faker.number.float(),
...override
Expand Down

0 comments on commit 0fbdaac

Please sign in to comment.