Skip to content

Commit

Permalink
feat(api): ✨ Add new endpoint for storing movies
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasberggren committed Apr 13, 2024
1 parent 9ff038c commit 3038198
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
14 changes: 13 additions & 1 deletion apps/api/src/movies/dto/create-movie.dto.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
/* eslint-disable @typescript-eslint/no-extraneous-class */
export class CreateMovieDto {}
export class CreateMovieDto {
genre: string
director: string
duration: number
subtitles: string[]
releaseDate: Date
movieTitles: CreateMovieTitleDto[]
}

export class CreateMovieTitleDto {
language: string
title: string
}
13 changes: 10 additions & 3 deletions apps/api/src/movies/movies.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, Query } from '@nestjs/common'
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, BadRequestException } from '@nestjs/common'

import { MoviesService } from './movies.service'
import { CreateMovieDto } from './dto/create-movie.dto'
import { UpdateMovieDto } from './dto/update-movie.dto'
Expand All @@ -20,8 +21,14 @@ export class MoviesController {
*/

@Post()
create (@Body() createMovieDto: CreateMovieDto) {
return this.moviesService.create(createMovieDto)
async create (@Body() createMovieDto: CreateMovieDto) {
if (createMovieDto.movieTitles.length === 0) {
throw new BadRequestException('At least one movie title is required')
}

await this.moviesService.create(createMovieDto)

return { message: 'Movie created successfully' }
}

@Get()
Expand Down
38 changes: 37 additions & 1 deletion apps/api/src/movies/movies.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
import { Test, TestingModule } from '@nestjs/testing'

import { PrismaService } from '../database/prisma.service'

import { MoviesService } from './movies.service'
import { CreateMovieDto } from './dto/create-movie.dto'

describe('MoviesService', () => {
let service: MoviesService
let prismaService: PrismaService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [MoviesService]
providers: [
MoviesService,
{
provide: PrismaService,
useValue: {
movie: {
create: jest.fn().mockResolvedValue({})
}
}
}
]
}).compile()

service = module.get<MoviesService>(MoviesService)
prismaService = module.get<PrismaService>(PrismaService)
})

it('should be defined', () => {
expect(service).toBeDefined()
})

it('should create a movie', async () => {
const createMovieDto: CreateMovieDto = {
genre: 'Action',
director: 'John Doe',
duration: 120,
subtitles: ['English', 'Spanish'],
releaseDate: new Date(),
movieTitles: [{ language: 'English', title: 'Example Title' }]
}

const expectedResult = {
...createMovieDto,
id: 1
}

jest.spyOn(prismaService.movie, 'create').mockResolvedValue(expectedResult)

await expect(service.create(createMovieDto)).resolves.toEqual(expectedResult)
})
})
25 changes: 23 additions & 2 deletions apps/api/src/movies/movies.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
import { Injectable } from '@nestjs/common'

import { PrismaService } from '../database/prisma.service'

import { CreateMovieDto } from './dto/create-movie.dto'
import { UpdateMovieDto } from './dto/update-movie.dto'

@Injectable()
export class MoviesService {
create (createMovieDto: CreateMovieDto) {
return 'This action adds a new movie'
constructor (private readonly db: PrismaService) {}
async create (createMovieDto: CreateMovieDto) {
const { movieTitles, ...movieData } = createMovieDto

const movie = await this.db.movie.create({
data: {
...movieData,
movieTitle: {
create: movieTitles.map((title) => ({
title: title.title,
language: title.language
}))
}
},
include: {
movieTitle: true
}
})

return movie
}

findAll () {
Expand Down

0 comments on commit 3038198

Please sign in to comment.