Small Nest.js-based project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
test-assignment-joyn/src/movies.controller.ts

114 lines
2.4 KiB

import {
BadRequestException,
Controller,
Get,
Inject,
NotFoundException,
Param,
Query,
} from '@nestjs/common';
import { ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsIn, IsInt, IsOptional, IsString, Length } from 'class-validator';
import { EnrichedDataService, MovieData } from './types';
const ENTRY_TYPES = ['movie', 'series', 'episode'] as const;
class SearchDto {
@ApiPropertyOptional()
@IsString()
@Length(1)
@IsOptional()
actors?: string;
@ApiPropertyOptional()
@IsString()
@Length(2, 2)
@IsOptional()
availableLanguages?: string;
@ApiPropertyOptional()
@IsString()
@Length(1)
@IsOptional()
directors?: string;
@ApiPropertyOptional()
@IsString()
@Length(1)
@IsOptional()
localTitle?: string;
@ApiPropertyOptional()
@IsString()
@Length(2, 2)
@IsOptional()
originalLanguage?: string;
@ApiPropertyOptional()
@IsString()
@Length(1)
@IsOptional()
productionCountries?: string;
@ApiPropertyOptional()
@IsInt()
@Type(() => Number)
@IsOptional()
productionYear?: number;
@ApiPropertyOptional()
@IsString()
@Length(1)
@IsOptional()
studios?: string;
@ApiPropertyOptional()
@IsString()
@Length(1)
@IsOptional()
title?: string;
@ApiPropertyOptional()
@IsString()
@IsIn(ENTRY_TYPES)
@IsOptional()
type?: (typeof ENTRY_TYPES)[number];
@ApiPropertyOptional()
@IsString()
@Length(1)
@IsOptional()
writers?: string;
}
@Controller('/api/movies')
export class MoviesController {
constructor(
@Inject('enrichedDataService')
private readonly enrichedDataService: EnrichedDataService,
) {}
@Get()
searchMovies(@Query() query: SearchDto): MovieData[] {
if (!Object.values(query).some((filterValue) => filterValue)) {
throw new BadRequestException('Query should not be empty');
}
const result = this.enrichedDataService.getSearchResults(query);
return result;
}
@Get(':id')
getMovie(@Param('id') id: string): MovieData {
const result = /^\d+$/.test(id)
? this.enrichedDataService.getMetadataByInternalId(parseInt(id, 10))
: this.enrichedDataService.getMetadataByImdbId(id);
if (!result) {
throw new NotFoundException();
}
return result;
}
}