search by id

This commit is contained in:
mProjectsCode 2022-05-11 23:41:55 +02:00
parent 62a4f2ec56
commit 92f3e6bab8
13 changed files with 183 additions and 29 deletions

View file

@ -42,6 +42,16 @@ export class APIManager {
}
}
getApiByName(name: string): APIModel {
for (const api of this.apis) {
if (api.apiName === name) {
return api;
}
}
return null;
}
registerAPI(api: APIModel): void {
this.apis.push(api);
}

View file

@ -1,30 +1,30 @@
import {MediaTypeModel} from '../models/MediaTypeModel';
export abstract class APIModel {
apiName: string;
apiUrl: string;
apiDescription: string;
types: string[];
apiName: string;
apiUrl: string;
apiDescription: string;
types: string[];
/**
* This function should query the api and return a list of matches. The matches should be caped at 20.
*
* @param title the title to query for
*/
abstract searchByTitle(title: string): Promise<MediaTypeModel[]>;
/**
* This function should query the api and return a list of matches. The matches should be caped at 20.
*
* @param title the title to query for
*/
abstract searchByTitle(title: string): Promise<MediaTypeModel[]>;
abstract getById(item: MediaTypeModel): Promise<MediaTypeModel>;
abstract getById(item: MediaTypeModel): Promise<MediaTypeModel>;
hasType(type: string): boolean {
return this.types.contains(type);
}
hasType(type: string): boolean {
return this.types.contains(type);
}
hasTypeOverlap(types: string[]): boolean {
for (const type of types) {
if (this.hasType(type)) {
return true;
}
}
return false;
}
hasTypeOverlap(types: string[]): boolean {
for (const type of types) {
if (this.hasType(type)) {
return true;
}
}
return false;
}
}

View file

@ -77,6 +77,7 @@ export class MALAPI extends APIModel {
title: result.title,
year: result.year ?? result.aired?.prop?.from?.year ?? '',
dataSource: this.apiName,
url: result.url,
id: result.mal_id,
genres: result.genres?.map((x: any) => x.name) ?? [],
@ -100,6 +101,7 @@ export class MALAPI extends APIModel {
title: result.title,
year: result.year ?? result.aired?.prop?.from?.year ?? '',
dataSource: this.apiName,
url: result.url,
id: result.mal_id,
genres: result.genres?.map((x: any) => x.name) ?? [],

View file

@ -32,6 +32,10 @@ export class OMDbAPI extends APIModel {
}
const data = await fetchData.json();
if (data.Response === 'False') {
throw Error(`Received error from ${this.apiName}: ${data.Error}`);
}
if (!data.Search) {
return [];
}
@ -76,14 +80,20 @@ export class OMDbAPI extends APIModel {
}
const result = await fetchData.json();
console.log(result);
if (result.Response === 'False') {
throw Error(`Received error from ${this.apiName}: ${result.Error}`);
}
if (result.Type === 'movie') {
const model = new MovieModel({
type: 'movie',
title: result.Title,
year: result.Year,
dataSource: this.apiName,
url: `https://www.imdb.com/title/${result.imdbID}/`,
id: result.imdbID,
genres: result.Genre?.split(', ') ?? [],
@ -107,6 +117,7 @@ export class OMDbAPI extends APIModel {
title: result.Title,
year: result.Year,
dataSource: this.apiName,
url: `https://www.imdb.com/title/${result.imdbID}/`,
id: result.imdbID,
genres: result.Genre?.split(', ') ?? [],