/** @file * @author Edouard DUPIN * @copyright 2018, Edouard DUPIN, all right reserved * @license PROPRIETARY (see license file) */ import { Injectable } from '@angular/core'; import { HttpWrapperService, BddService } from 'common/service'; import { DataInterface, TypeCheck, isArrayOf } from 'common/utils'; import { isNodeData, NodeData } from 'common/model'; import { GenericInterfaceModelDB } from './GenericInterfaceModelDB'; @Injectable() export class ArtistService extends GenericInterfaceModelDB { constructor(http: HttpWrapperService, bdd: BddService) { super('artist', http, bdd); } /** * Get all the track for a specific artist * @param idArtist - Id of the artist. * @returns a promise on the list of track elements */ getTrackNoAlbum(idArtist:number): Promise { let self = this; return new Promise((resolve, reject) => { self.bdd.get('track') .then((response: DataInterface) => { let data = response.getsWhere([ { check: TypeCheck.CONTAINS, key: 'artists', value: idArtist, }, { check: TypeCheck.EQUAL, key: 'albumId', value: undefined, }, ], [ 'track', 'name' ]); resolve(data); }).catch((response) => { reject(response); }); }); } /** * Get all the track for a specific artist * @param idArtist - Id of the artist. * @returns a promise on the list of track elements */ getAllTracks(idArtist:number): Promise { let self = this; return new Promise((resolve, reject) => { self.bdd.get('track') .then((response: DataInterface) => { let data = response.getsWhere([ { check: TypeCheck.CONTAINS, key: 'artists', value: idArtist, } ], [ 'track', 'name' ]); resolve(data); }).catch((response) => { reject(response); }); }); } /** * Get the number of track in this artist ID * @param id - Id of the artist. * @returns The number of track present in this artist */ countTrack(artistId:number): Promise { let self = this; return new Promise((resolve, reject) => { self.bdd.get('track') .then((response:DataInterface) => { let data = response.getsWhere([ { check: TypeCheck.CONTAINS, key: 'artists', value: artistId, }, ]); resolve(data.length); }).catch((response) => { reject(response); }); }); } /** * Get all the album of a specific artist * @param idArtist - ID of the artist * @returns the required List. */ getAlbum(idArtist:number): Promise { let self = this; return new Promise((resolve, reject) => { self.bdd.get('track') .then((response:DataInterface) => { console.log(" <<<========================================>>> " + idArtist); let data = response.getsWhere([ { check: TypeCheck.CONTAINS, //< this is for array containing key: 'artists', value: idArtist, }, ], [ 'id' ]); //console.log("==> get all tracks of the artist: " + JSON.stringify(data, null, 2)); // extract a single time all value "id" in an array const listAlbumId = DataInterface.extractLimitOne(data, "albumId"); //console.log("==> List Of ids: " + JSON.stringify(listAlbumId, null, 2)); self.bdd.get('album') .then((response:DataInterface) => { let dataAlbum = response.getsWhere([ { check: TypeCheck.EQUAL, key: 'id', value: listAlbumId, }, ], [ 'publication', 'name', 'id' ]); resolve(dataAlbum); //console.log("==> get all albums: " + JSON.stringify(dataAlbum, null, 2)); return; }).catch((response) => { reject(response); }); return; }).catch((response) => { reject(response); }); }); } countAlbum(idArtist:number): Promise { let self = this; return new Promise((resolve, reject) => { self.bdd.get('track') .then((response:DataInterface) => { console.log(" <<<========================================>>> " + idArtist); let data = response.getsWhere([ { check: TypeCheck.CONTAINS, //< this is for array containing key: 'artists', value: idArtist, }, ], [ 'id' ]); //console.log("==> get all tracks of the artist: " + JSON.stringify(data, null, 2)); // extract a single time all value "id" in an array const listAlbumId = DataInterface.extractLimitOne(data, "albumId"); resolve(listAlbumId.length); }).catch((response) => { reject(response); }); }); } }