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.
103 lines
2.1 KiB
103 lines
2.1 KiB
9 months ago
|
import {
|
||
|
BadRequestException,
|
||
|
Controller,
|
||
|
Get,
|
||
|
Inject,
|
||
|
NotFoundException,
|
||
|
Param,
|
||
|
Query,
|
||
|
} from '@nestjs/common';
|
||
|
import { EnrichedDataService } from './types';
|
||
|
import { IsIn, IsInt, IsOptional, IsString, Length } from 'class-validator';
|
||
|
import { Type } from 'class-transformer';
|
||
|
|
||
|
const ENTRY_TYPES = ['movie', 'series', 'episode'] as const;
|
||
|
|
||
|
class SearchDto {
|
||
|
@IsString()
|
||
|
@Length(1)
|
||
|
@IsOptional()
|
||
|
actors?: string;
|
||
|
|
||
|
@IsString()
|
||
|
@Length(2, 2)
|
||
|
@IsOptional()
|
||
|
availableLanguages?: string;
|
||
|
|
||
|
@IsString()
|
||
|
@Length(1)
|
||
|
@IsOptional()
|
||
|
directors?: string;
|
||
|
|
||
|
@IsString()
|
||
|
@Length(1)
|
||
|
@IsOptional()
|
||
|
localTitle?: string;
|
||
|
|
||
|
@IsString()
|
||
|
@Length(2, 2)
|
||
|
@IsOptional()
|
||
|
originalLanguage?: string;
|
||
|
|
||
|
@IsString()
|
||
|
@Length(1)
|
||
|
@IsOptional()
|
||
|
productionCountries?: string;
|
||
|
|
||
|
@IsInt()
|
||
|
@Type(() => Number)
|
||
|
@IsOptional()
|
||
|
productionYear?: number;
|
||
|
|
||
|
@IsString()
|
||
|
@Length(1)
|
||
|
@IsOptional()
|
||
|
studios?: string;
|
||
|
|
||
|
@IsString()
|
||
|
@Length(1)
|
||
|
@IsOptional()
|
||
|
title?: string;
|
||
|
|
||
|
@IsString()
|
||
|
@IsIn(ENTRY_TYPES)
|
||
|
@IsOptional()
|
||
|
type?: (typeof ENTRY_TYPES)[number];
|
||
|
|
||
|
@IsString()
|
||
|
@Length(1)
|
||
|
@IsOptional()
|
||
|
writers?: string;
|
||
|
}
|
||
|
|
||
|
@Controller('/api/movies')
|
||
|
export class MoviesController {
|
||
|
constructor(
|
||
|
@Inject('enrichedDataService')
|
||
|
private readonly enrichedDataService: EnrichedDataService,
|
||
|
) {}
|
||
|
|
||
|
@Get()
|
||
|
searchMovies(@Query() query: SearchDto) {
|
||
|
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) {
|
||
|
const result = /^\d+$/.test(id)
|
||
|
? this.enrichedDataService.getMetadataByInternalId(parseInt(id, 10))
|
||
|
: this.enrichedDataService.getMetadataByImdbId(id);
|
||
|
|
||
|
if (!result) {
|
||
|
throw new NotFoundException();
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
}
|