From 887763286c481e9ec1425032553535de8fa7f397 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 14 Jun 2022 00:23:33 +0200 Subject: [PATCH] [DEV] generize code for model access --- front/src/app/app.module.ts | 5 +- .../src/app/scene/season-edit/season-edit.ts | 8 +- front/src/app/scene/video-edit/video-edit.ts | 7 +- front/src/app/scene/video/video.ts | 2 +- .../app/service/GenericInterfaceModelDB.ts | 96 +++++++ front/src/app/service/ariane.ts | 11 +- front/src/app/service/data.ts | 5 - front/src/app/service/index.ts | 2 - front/src/app/service/season.ts | 102 ++----- front/src/app/service/series.ts | 125 ++------- front/src/app/service/type.ts | 62 ++-- front/src/app/service/universe.ts | 90 +----- front/src/app/service/video.ts | 103 +++---- front/src/{app => common}/service/bdd.ts | 49 +--- front/src/common/service/http-wrapper.ts | 265 +----------------- front/src/common/service/index.ts | 2 + front/src/common/utils/index.ts | 3 +- 17 files changed, 244 insertions(+), 693 deletions(-) create mode 100644 front/src/app/service/GenericInterfaceModelDB.ts rename front/src/{app => common}/service/bdd.ts (83%) diff --git a/front/src/app/app.module.ts b/front/src/app/app.module.ts index c974fe5..7e20394 100644 --- a/front/src/app/app.module.ts +++ b/front/src/app/app.module.ts @@ -28,9 +28,8 @@ import { AppComponent } from './app.component'; import { ErrorComponent } from '../common/error/error'; import { HomeScene, HelpScene, TypeScene, UniverseScene, SeriesScene, SeasonScene, VideoScene, SettingsScene, VideoEditScene, SeasonEditScene, SeriesEditScene } from './scene'; -import { BddService, TypeService, - DataService, UniverseService, SeriesService, SeasonService, VideoService, ArianeService } from './service'; -import { CookiesService, HttpWrapperService, PopInService, SessionService, SSOService, StorageService, UserService } from 'common/service'; +import { TypeService, DataService, UniverseService, SeriesService, SeasonService, VideoService, ArianeService } from './service'; +import { BddService, CookiesService, HttpWrapperService, PopInService, SessionService, SSOService, StorageService, UserService } from 'common/service'; import { ErrorViewerScene, SsoScene } from 'common/scene'; import { UploadScene } from './scene/upload/upload'; diff --git a/front/src/app/scene/season-edit/season-edit.ts b/front/src/app/scene/season-edit/season-edit.ts index 78b13ad..b672cd7 100644 --- a/front/src/app/scene/season-edit/season-edit.ts +++ b/front/src/app/scene/season-edit/season-edit.ts @@ -8,9 +8,11 @@ import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { SeasonService , ArianeService, DataService} from 'app/service'; +import { NodeData } from 'common/model'; import { UploadProgress } from 'common/popin/upload-progress/upload-progress'; import { PopInService } from 'common/service'; +import { isNumberFinite } from 'common/utils'; export interface ElementList { value: number; @@ -79,9 +81,11 @@ export class SeasonEditScene implements OnInit { this.idSeason = this.arianeService.getSeasonId(); let self = this; this.seasonService.get(this.idSeason) - .then((response) => { + .then((response: NodeData) => { console.log(`get response of season : ${ JSON.stringify(response, null, 2)}`); - self.numberVal = response.name; + if (isNumberFinite(response.name)) { + self.numberVal = response.name; + } self.description = response.description; self.updateCoverList(response.covers); self.itemIsLoading = false; diff --git a/front/src/app/scene/video-edit/video-edit.ts b/front/src/app/scene/video-edit/video-edit.ts index 881872e..891fdf0 100644 --- a/front/src/app/scene/video-edit/video-edit.ts +++ b/front/src/app/scene/video-edit/video-edit.ts @@ -12,6 +12,7 @@ import { DataService, TypeService, UniverseService, SeriesService, VideoService, import { UploadProgress } from 'common/popin/upload-progress/upload-progress'; import { NodeData } from 'common/model'; import { PopInService } from 'common/service'; +import { Media } from 'app/model'; export interface ElementList { value?: number; @@ -200,18 +201,18 @@ export class VideoEditScene implements OnInit { console.log(`get response3 : ${ JSON.stringify(response3, null, 2)}`); }); this.videoService.get(this.idVideo) - .then((response) => { + .then((response: Media) => { console.log(`get response of video : ${ JSON.stringify(response, null, 2)}`); self.data.name = response.name; self.data.description = response.description; self.data.episode = response.episode; - self.data.universeId = response.universId; + self.data.universeId = response.universeId; if(self.data.universeId === undefined) { self.data.universeId = null; } self.data.dataId = response.dataId; self.data.time = response.time; - self.data.generatedName = response.generatedName; + self.data.generatedName = "????????? TODO ????????? "; // response.generatedName; self.onChangeType(response.typeId); self.onChangeSeries(response.seriesId); self.data.seasonId = response.seasonId; diff --git a/front/src/app/scene/video/video.ts b/front/src/app/scene/video/video.ts index 329ead6..06a5db2 100644 --- a/front/src/app/scene/video/video.ts +++ b/front/src/app/scene/video/video.ts @@ -206,7 +206,7 @@ export class VideoScene implements OnInit { self.seasonId = response.seasonId; self.dataId = response.dataId; self.time = response.time; - self.generatedName = response.generatedName; + self.generatedName = "????????? TODO: ???????" //response.generatedName; if(self.dataId !== -1) { self.videoSource = self.httpService.createRESTCall2({ api: `data/${ self.dataId}/${self.generatedName}`, diff --git a/front/src/app/service/GenericInterfaceModelDB.ts b/front/src/app/service/GenericInterfaceModelDB.ts new file mode 100644 index 0000000..c3515b9 --- /dev/null +++ b/front/src/app/service/GenericInterfaceModelDB.ts @@ -0,0 +1,96 @@ +import { NodeData } from "common/model"; +import { HttpWrapperService, BddService } from "common/service"; +import { DataInterface, isNullOrUndefined } from "common/utils"; + +export class GenericInterfaceModelDB { + constructor( + protected serviceName:string, + protected http: HttpWrapperService, + protected bdd: BddService) { + // nothing to do ... + } + + get(id:number): Promise { + let self = this; + return new Promise((resolve, reject) => { + self.bdd.get(self.serviceName) + .then((response: DataInterface) => { + let data = response.get(id); + if(isNullOrUndefined(data)) { + reject('Data does not exist in the local BDD'); + return; + } + resolve(data); + return; + }).catch((response) => { + reject(response); + }); + }); + } + + getData(): Promise { + let self = this; + return new Promise((resolve, reject) => { + self.bdd.get(self.serviceName) + .then((response:DataInterface) => { + let data = response.gets(); + resolve(data); + }).catch((response) => { + console.log(`[E] ${ self.constructor.name }: can not retrive BDD values`); + reject(response); + }); + }); + } + + put(id:number, data:any):any { + let ret = this.http.putSpecific([this.serviceName, id], data); + return this.bdd.setAfterPut(this.serviceName, id, ret); + } + + delete(id:number):any { + let ret = this.http.deleteSpecific([this.serviceName, id]); + return this.bdd.delete(this.serviceName, id, ret); + } + + deleteCover(nodeId:number, coverId:number) { + let self = this; + return new Promise((resolve, reject) => { + self.http.getSpecific([this.serviceName, nodeId, 'rm_cover', coverId]) + .then((response) => { + let data = response; + if(data === null || data === undefined) { + reject('error retrive data from server'); + return; + } + self.bdd.asyncSetInDB(self.serviceName, nodeId, data); + resolve(data); + }).catch((response) => { + reject(response); + }); + }); + } + uploadCover(file:File, + nodeId:number, + progress:any = null) { + const formData = new FormData(); + formData.append('fileName', file.name); + formData.append('id', nodeId.toString()); + formData.append('file', file); + let self = this; + return new Promise((resolve, reject) => { + self.http.uploadMultipart(`${this.serviceName }/${nodeId}/add_cover/`, formData, progress) + .then((response) => { + let data = response; + if(data === null || data === undefined) { + reject('error retrive data from server'); + return; + } + self.bdd.asyncSetInDB(self.serviceName, nodeId, data); + resolve(data); + }).catch((response) => { + reject(response); + }); + }); + } + +} \ No newline at end of file diff --git a/front/src/app/service/ariane.ts b/front/src/app/service/ariane.ts index 583217c..8281cee 100644 --- a/front/src/app/service/ariane.ts +++ b/front/src/app/service/ariane.ts @@ -14,6 +14,7 @@ import { SeriesService } from './series'; import { SeasonService } from './season'; import { VideoService } from './video'; import { environment } from 'environments/environment'; +import { NodeData } from 'common/model'; export class InputOrders { public typeId: number = null; @@ -51,7 +52,7 @@ export class ArianeService { private seriesService: SeriesService, private seasonService: SeasonService, private videoService: VideoService) { - console.log('Start ArianeService'); + //console.log('Start ArianeService'); } updateParams(params) { console.log(`sparams ${ params}`); @@ -205,8 +206,8 @@ export class ArianeService { } let self = this; this.universeService.get(id) - .then((response) => { - self.universeName = response.number; + .then((response: NodeData) => { + self.universeName = response.name; self.universeChange.emit(self.universeId); }).catch((response) => { self.universeChange.emit(self.universeId); @@ -234,7 +235,7 @@ export class ArianeService { } let self = this; this.seriesService.get(id) - .then((response) => { + .then((response: NodeData) => { self.seriesName = response.name; self.seriesChange.emit(self.seriesId); }).catch((response) => { @@ -263,7 +264,7 @@ export class ArianeService { } let self = this; this.seasonService.get(id) - .then((response) => { + .then((response: NodeData) => { // self.setSeries(response.seriesId); self.seasonName = response.name; self.seasonChange.emit(self.seasonId); diff --git a/front/src/app/service/data.ts b/front/src/app/service/data.ts index f97dd74..badd3e6 100644 --- a/front/src/app/service/data.ts +++ b/front/src/app/service/data.ts @@ -26,11 +26,6 @@ export class DataService { return this.http.getSpecific([this.serviceName, _id]); } - sendFile(_file:File) { - // return this.http.uploadFileMultipart(this.serviceName, null, _file); - return this.http.uploadFileBase64(this.serviceName, null, _file); - } - uploadFile(_form:FormData, _progress:any = null) { // return this.http.uploadFileMultipart(this.serviceName, null, _file); return this.http.uploadMultipart(`${this.serviceName }/upload/`, _form, _progress); diff --git a/front/src/app/service/index.ts b/front/src/app/service/index.ts index f296d1e..149f6b6 100644 --- a/front/src/app/service/index.ts +++ b/front/src/app/service/index.ts @@ -1,5 +1,4 @@ import { ArianeService } from "./ariane"; -import { BddService } from "./bdd"; import { DataService } from "./data"; import { SeasonService } from "./season"; import { SeriesService } from "./series"; @@ -11,7 +10,6 @@ import { VideoService } from "./video"; export { ArianeService, - BddService, DataService, SeasonService, SeriesService, diff --git a/front/src/app/service/season.ts b/front/src/app/service/season.ts index 95b9de9..17a9adc 100644 --- a/front/src/app/service/season.ts +++ b/front/src/app/service/season.ts @@ -5,50 +5,30 @@ */ import { Injectable } from '@angular/core'; +import { NodeData } from 'common/model'; -import { HttpWrapperService } from '../../common/service/http-wrapper'; -import { DataInterface } from 'common/utils'; -import { BddService } from './bdd'; -import { TypeCheck } from 'common/utils/dataInterface'; +import { HttpWrapperService, BddService } from 'common/service'; +import { DataInterface, TypeCheck } from 'common/utils'; +import { GenericInterfaceModelDB } from './GenericInterfaceModelDB'; @Injectable() -export class SeasonService { - private serviceName:string = 'season'; - - constructor(private http: HttpWrapperService, - private bdd: BddService) { - console.log('Start SeasonService'); - } - - - get(id:number):any { - let self = this; - return new Promise((resolve, reject) => { - self.bdd.getSeason() - .then((response: DataInterface) => { - let data = response.get(id); - if(data === null || data === undefined) { - reject('Data does not exist in the local BDD'); - return; - } - resolve(data); - }).catch((response) => { - reject(response); - }); - }); +export class SeasonService extends GenericInterfaceModelDB { + + constructor(http: HttpWrapperService, + bdd: BddService) { + super('season', http, bdd); } /** * Get all the video for a specific season * @param id - Id of the season. - * @returns a promise on the list of season elements + * @returns a promise on the list of video elements */ - getVideo(id:number):any { + getVideo(id:number): Promise { let self = this; return new Promise((resolve, reject) => { - self.bdd.getVideo() + self.bdd.get('video') .then((response: DataInterface) => { - // let data = response.getsWhere([["==", "seasonId", id]], ["id", "name", "episode"], ["episode", "name"]) let data = response.getsWhere([ { check: TypeCheck.EQUAL, @@ -69,73 +49,23 @@ export class SeasonService { * @param id - Id of the season. * @returns The number of element present in this saison */ - countVideo(id:number):any { + countVideo(id:number): Promise { let self = this; return new Promise((resolve, reject) => { - self.bdd.getVideo() + self.bdd.get('video') .then((response: DataInterface) => { - // let data = response.getsWhere([["==", "seasonId", _id]], ["id", "name", "episode"], ["episode", "name"]) let data = response.getsWhere([ { check: TypeCheck.EQUAL, key: 'seasonId', value: id, - } ] ); + }, + ]); resolve(data.length); }).catch((response) => { reject(response); }); }); } - put(id:number, data:any):any { - let ret = this.http.putSpecific([this.serviceName, id], data); - return this.bdd.setAfterPut(this.serviceName, id, ret); - } - - delete(id:number):any { - let ret = this.http.deleteSpecific([this.serviceName, id]); - return this.bdd.delete(this.serviceName, id, ret); - } - deleteCover(nodeId:number, - coverId:number) { - let self = this; - return new Promise((resolve, reject) => { - self.http.getSpecific([this.serviceName, nodeId, 'rm_cover', coverId]) - .then((response) => { - let data = response; - if(data === null || data === undefined) { - reject('error retrive data from server'); - return; - } - self.bdd.asyncSetInDB(self.serviceName, nodeId, data); - resolve(data); - }).catch((response) => { - reject(response); - }); - }); - } - uploadCover(file:File, - nodeId:number, - progress:any = null) { - const formData = new FormData(); - formData.append('fileName', file.name); - formData.append('nodeId', nodeId.toString()); - formData.append('file', file); - let self = this; - return new Promise((resolve, reject) => { - self.http.uploadMultipart(`${this.serviceName }/${ nodeId }/add_cover/`, formData, progress) - .then((response) => { - let data = response; - if(data === null || data === undefined) { - reject('error retrive data from server'); - return; - } - self.bdd.asyncSetInDB(self.serviceName, nodeId, data); - resolve(data); - }).catch((response) => { - reject(response); - }); - }); - } } diff --git a/front/src/app/service/series.ts b/front/src/app/service/series.ts index 7e71fa8..b6b8420 100644 --- a/front/src/app/service/series.ts +++ b/front/src/app/service/series.ts @@ -6,58 +6,25 @@ import { Injectable } from '@angular/core'; -import { HttpWrapperService } from '../../common/service/http-wrapper'; -import { DataInterface, TypeCheck } from 'common/utils/dataInterface'; -import { BddService } from './bdd'; +import { HttpWrapperService, BddService } from 'common/service'; +import { DataInterface, TypeCheck, isArrayOf } from 'common/utils'; import { isNodeData, NodeData } from 'common/model'; -import { isArrayOf, isNullOrUndefined } from 'common/utils'; +import { GenericInterfaceModelDB } from './GenericInterfaceModelDB'; + @Injectable() -export class SeriesService { - private serviceName:string = 'series'; +export class SeriesService extends GenericInterfaceModelDB { - constructor(private http: HttpWrapperService, - private bdd: BddService) { - console.log('Start SeriesService'); - } - - get(id:number): Promise { - let self = this; - return new Promise((resolve, reject) => { - self.bdd.getSeries() - .then((response:DataInterface) => { - let data = response.get(id); - if(isNullOrUndefined(data)) { - reject('Data does not exist in the local BDD'); - return; - } - resolve(data); - return; - }).catch((response) => { - reject(response); - }); - }); - } - - getData(): Promise { - let self = this; - return new Promise((resolve, reject) => { - self.bdd.getSeries() - .then((response:DataInterface) => { - let data = response.gets(); - resolve(data); - }).catch((response) => { - console.log(`[E] ${ self.constructor.name }: can not retrive BDD values`); - reject(response); - }); - }); + constructor(http: HttpWrapperService, + bdd: BddService) { + super('series', http, bdd); } getOrder(): Promise { let self = this; return new Promise((resolve, reject) => { - self.bdd.getSeries() + self.bdd.get(self.serviceName) .then((response: DataInterface) => { let data = response.getsWhere([ { @@ -78,11 +45,16 @@ export class SeriesService { }); } - getVideo(id:number):Promise { + /** + * Get all the video for a specific series + * @param id - Id of the series. + * @returns a promise on the list of video elements + */ + getVideo(id:number): Promise { let self = this; return new Promise((resolve, reject) => { - self.bdd.getVideo() - .then((response:DataInterface) => { + self.bdd.get('video') + .then((response: DataInterface) => { let data = response.getsWhere([ { check: TypeCheck.EQUAL, @@ -101,10 +73,16 @@ export class SeriesService { }); }); } + + /** + * Get the number of video in this series ID + * @param id - Id of the series. + * @returns The number of video present in this series + */ countVideo(id:number): Promise { let self = this; return new Promise((resolve, reject) => { - self.bdd.getVideo() + self.bdd.get('video') .then((response:DataInterface) => { let data = response.getsWhere([ { @@ -128,7 +106,7 @@ export class SeriesService { getSeason(id:number): Promise { let self = this; return new Promise((resolve, reject) => { - self.bdd.getSeason() + self.bdd.get('season') .then((response:DataInterface) => { let data = response.getsWhere([ { @@ -145,20 +123,10 @@ export class SeriesService { }); } - put(id:number, data:any):any { - let ret = this.http.putSpecific([this.serviceName, id], data); - return this.bdd.setAfterPut(this.serviceName, id, ret); - } - - delete(id:number):any { - let ret = this.http.deleteSpecific([this.serviceName, id]); - return this.bdd.delete(this.serviceName, id, ret); - } - getLike(nameSeries:string):any { let self = this; return new Promise((resolve, reject) => { - self.bdd.getSeries() + self.bdd.get('series') .then((response:DataInterface) => { let data = response.getNameLike(nameSeries); if(data === null || data === undefined || data.length === 0) { @@ -172,46 +140,5 @@ export class SeriesService { }); }); } - deleteCover(nodeId:number, - coverId:number) { - let self = this; - return new Promise((resolve, reject) => { - self.http.getSpecific([this.serviceName, nodeId, 'rm_cover', coverId]) - .then((response) => { - let data = response; - if(data === null || data === undefined) { - reject('error retrive data from server'); - return; - } - self.bdd.asyncSetInDB(self.serviceName, nodeId, data); - resolve(data); - }).catch((response) => { - reject(response); - }); - }); - } - uploadCover(file:File, - nodeId:number, - progress:any = null) { - const formData = new FormData(); - formData.append('fileName', file.name); - formData.append('nodeId', nodeId.toString()); - formData.append('file', file); - let self = this; - return new Promise((resolve, reject) => { - self.http.uploadMultipart(`${this.serviceName }/${nodeId}/add_cover/`, formData, progress) - .then((response) => { - let data = response; - if(data === null || data === undefined) { - reject('error retrive data from server'); - return; - } - self.bdd.asyncSetInDB(self.serviceName, nodeId, data); - resolve(data); - }).catch((response) => { - reject(response); - }); - }); - } } diff --git a/front/src/app/service/type.ts b/front/src/app/service/type.ts index dd34923..dda0228 100644 --- a/front/src/app/service/type.ts +++ b/front/src/app/service/type.ts @@ -6,11 +6,10 @@ import { Injectable } from '@angular/core'; -import { HttpWrapperService } from '../../common/service/http-wrapper'; -import { BddService } from './bdd'; -import { DataInterface, isNullOrUndefined } from 'common/utils'; +import { HttpWrapperService, BddService } from 'common/service'; +import { DataInterface, isNullOrUndefined, TypeCheck } from 'common/utils'; import { NodeData } from 'common/model'; -import { TypeCheck } from 'common/utils/dataInterface'; +import { GenericInterfaceModelDB } from './GenericInterfaceModelDB'; export interface MessageLogIn { id: number; @@ -19,34 +18,20 @@ export interface MessageLogIn { } @Injectable() -export class TypeService { +export class TypeService extends GenericInterfaceModelDB { - constructor(private http: HttpWrapperService, - private bdd: BddService) { - console.log('Start TypeService'); + constructor(http: HttpWrapperService, + bdd: BddService) { + super('type', http, bdd); } - getData(): Promise { + get(id:number): Promise { let self = this; return new Promise((resolve, reject) => { - self.bdd.getType() + self.bdd.get(self.serviceName) .then((response: DataInterface) => { - let data = response.gets(); - resolve(data); - }).catch((response) => { - console.log(`[E] ${ self.constructor.name }: can not retrive BDD values`); - reject(response); - }); - }); - } - - get(_id:number): Promise { - let self = this; - return new Promise((resolve, reject) => { - self.bdd.getType() - .then((response: DataInterface) => { - let data = response.get(_id); + let data = response.get(id); if(isNullOrUndefined(data)) { reject('Data does not exist in the local BDD'); return; @@ -58,10 +43,29 @@ export class TypeService { }); } + getData(): Promise { + let self = this; + return new Promise((resolve, reject) => { + self.bdd.get(self.serviceName) + .then((response: DataInterface) => { + let data = response.gets(); + resolve(data); + }).catch((response) => { + console.log(`[E] ${ self.constructor.name }: can not retrive BDD values`); + reject(response); + }); + }); + } + + + + + + countVideo(id:number): Promise { let self = this; return new Promise((resolve, reject) => { - self.bdd.getVideo() + self.bdd.get('video') .then((response: DataInterface) => { let data = response.getsWhere([ { @@ -79,7 +83,7 @@ export class TypeService { getSubVideo(id:number): Promise { let self = this; return new Promise((resolve, reject) => { - self.bdd.getVideo() + self.bdd.get('video') .then((response: DataInterface) => { let data = response.getsWhere([ { @@ -106,7 +110,7 @@ export class TypeService { getSubSeries(id:number): Promise { let self = this; return new Promise((resolve, reject) => { - self.bdd.getSeries() + self.bdd.get('series') .then((response: DataInterface) => { let data = response.getsWhere([ { @@ -125,7 +129,7 @@ export class TypeService { getSubUniverse(id:number): Promise { let self = this; return new Promise((resolve, reject) => { - self.bdd.getVideo() + self.bdd.get('video') .then((response: DataInterface) => { let data = response.getsWhere([ { diff --git a/front/src/app/service/universe.ts b/front/src/app/service/universe.ts index 2123385..f3f8a85 100644 --- a/front/src/app/service/universe.ts +++ b/front/src/app/service/universe.ts @@ -6,48 +6,15 @@ import { Injectable } from '@angular/core'; -import { HttpWrapperService } from '../../common/service/http-wrapper'; -import { BddService } from './bdd'; +import { HttpWrapperService, BddService } from 'common/service'; +import { GenericInterfaceModelDB } from './GenericInterfaceModelDB'; @Injectable() -export class UniverseService { - // 0: Not hide password; 1 hide password; - private identificationVersion: number = 1; - private serviceName:string = 'universe'; +export class UniverseService extends GenericInterfaceModelDB { - constructor(private http: HttpWrapperService, - private bdd: BddService) { - console.log('Start universeService'); - } - - getData():any { - let self = this; - return new Promise((resolve, reject) => { - self.bdd.getUniverse() - .then((response) => { - let data = response.gets(); - resolve(data); - }).catch((response) => { - reject(response); - }); - }); - } - - get(id:number):any { - let self = this; - return new Promise((resolve, reject) => { - self.bdd.getUniverse() - .then((response) => { - let data = response.get(id); - if(data === null || data === undefined) { - reject('Data does not exist in the local BDD'); - return; - } - resolve(data); - }).catch((response) => { - reject(response); - }); - }); + constructor(http: HttpWrapperService, + bdd: BddService) { + super('universe', http, bdd); } getSubSeries(id:number, select:Array = []):any { @@ -58,50 +25,5 @@ export class UniverseService { // this.checkLocalBdd(); } - put(id:number, data:any):any { - let ret = this.http.putSpecific([this.serviceName, id], data); - return this.bdd.setAfterPut(this.serviceName, id, ret); - } - deleteCover(nodeId:number, - coverId:number) { - let self = this; - return new Promise((resolve, reject) => { - self.http.getSpecific([this.serviceName, nodeId, 'rm_cover', coverId]) - .then((response) => { - let data = response; - if(data === null || data === undefined) { - reject('error retrive data from server'); - return; - } - self.bdd.asyncSetInDB(self.serviceName, nodeId, data); - resolve(data); - }).catch((response) => { - reject(response); - }); - }); - } - uploadCover(file:File, - nodeId:number, - progress:any = null) { - const formData = new FormData(); - formData.append('fileName', file.name); - formData.append('nodeId', nodeId.toString()); - formData.append('file', file); - let self = this; - return new Promise((resolve, reject) => { - self.http.uploadMultipart(`${this.serviceName }/${ nodeId }/add_cover/`, formData, progress) - .then((response) => { - let data = response; - if(data === null || data === undefined) { - reject('error retrive data from server'); - return; - } - self.bdd.asyncSetInDB(self.serviceName, nodeId, data); - resolve(data); - }).catch((response) => { - reject(response); - }); - }); - } } diff --git a/front/src/app/service/video.ts b/front/src/app/service/video.ts index e4295f9..1027b77 100644 --- a/front/src/app/service/video.ts +++ b/front/src/app/service/video.ts @@ -5,46 +5,50 @@ */ import { Injectable } from '@angular/core'; +import { isMedia, Media } from 'app/model'; +import { NodeData } from 'common/model'; -import { HttpWrapperService } from '../../common/service/http-wrapper'; -import { BddService } from './bdd'; -import { DataInterface } from 'common/utils/dataInterface'; +import { HttpWrapperService, BddService } from 'common/service'; +import { isArrayOf } from 'common/utils'; +import { GenericInterfaceModelDB } from './GenericInterfaceModelDB'; @Injectable() -export class VideoService { - private serviceName:string = 'video'; +export class VideoService extends GenericInterfaceModelDB { - constructor(private http: HttpWrapperService, - private bdd: BddService) { - console.log('Start VideoService'); + constructor(http: HttpWrapperService, + bdd: BddService) { + super('video', http, bdd); } - - get(id:number):any { - let self = this; + get(id:number): Promise { return new Promise((resolve, reject) => { - self.bdd.get(this.serviceName) - .then((response: DataInterface) => { - let data = response.get(id); - if(data === null || data === undefined) { - reject('Data does not exist in the local BDD'); - return; - } + super.get(id).then((data: NodeData) => { + if (isMedia(data)) { resolve(data); - }).catch((response) => { - reject(response); - }); + return; + } + reject("model is wrong !!!") + return + }).catch((reason:any) => { + reject(reason); + }); }); } - put(id:number, data:any):any { - let ret = this.http.putSpecific([this.serviceName, id], data); - return this.bdd.setAfterPut(this.serviceName, id, ret); + getData(): Promise { + return new Promise((resolve, reject) => { + super.getData().then((data: NodeData[]) => { + if (isArrayOf(data, isMedia)) { + resolve(data); + return; + } + reject("model is wrong !!!") + return + }).catch((reason:any) => { + reject(reason); + }); + }); } - delete(id:number):any { - let ret = this.http.deleteSpecific([this.serviceName, id]); - return this.bdd.delete(this.serviceName, id, ret); - } - + uploadFile(file:File, universe?:string, series?:string, @@ -86,47 +90,6 @@ export class VideoService { return this.http.uploadMultipart(`${this.serviceName }/upload/`, formData, progress); } - deleteCover(mediaId:number, - coverId:number) { - let self = this; - return new Promise((resolve, reject) => { - self.http.getSpecific([this.serviceName, mediaId, 'rm_cover', coverId]) - .then((response) => { - let data = response; - if(data === null || data === undefined) { - reject('error retrive data from server'); - return; - } - self.bdd.asyncSetInDB(self.serviceName, mediaId, data); - resolve(data); - }).catch((response) => { - reject(response); - }); - }); - } - uploadCover(file:File, - mediaId:number, - progress:any = null) { - const formData = new FormData(); - formData.append('fileName', file.name); - formData.append('id', mediaId.toString()); - formData.append('file', file); - let self = this; - return new Promise((resolve, reject) => { - self.http.uploadMultipart(`${this.serviceName }/${ mediaId }/add_cover/`, formData, progress) - .then((response) => { - let data = response; - if(data === null || data === undefined) { - reject('error retrive data from server'); - return; - } - self.bdd.asyncSetInDB(self.serviceName, mediaId, data); - resolve(data); - }).catch((response) => { - reject(response); - }); - }); - } uploadCoverBlob(blob:Blob, mediaId:number, progress:any = null) { diff --git a/front/src/app/service/bdd.ts b/front/src/common/service/bdd.ts similarity index 83% rename from front/src/app/service/bdd.ts rename to front/src/common/service/bdd.ts index dc2d8f9..78a3f3f 100644 --- a/front/src/app/service/bdd.ts +++ b/front/src/common/service/bdd.ts @@ -6,26 +6,15 @@ import { Injectable } from '@angular/core'; import { HttpWrapperService } from 'common/service/http-wrapper'; +import { isNullOrUndefined } from 'common/utils'; import { DataInterface } from 'common/utils/dataInterface'; @Injectable() export class BddService { - private bdd = { - type: null, - series: null, - season: null, - universe: null, - video: null - }; - private bddPomise = { - type: null, - series: null, - season: null, - universe: null, - video: null - }; - private baseLocalStorageName = 'yota_karideo_bdd_'; + private bdd = {}; + private bddPomise = {}; + private baseLocalStorageName = 'bdd_'; private useLocalStorage = false; // we exceed the limit of 5MB constructor(private http: HttpWrapperService) { @@ -80,17 +69,13 @@ export class BddService { get(name: string): Promise { let self = this; console.log(`Try to get DB '${ name }'`); - if(this.bdd[name] === undefined) { - console.log(`Try to get a non existant DB ... '${ name }'`); - return; - } - if(this.bdd[name] !== null) { + if( !isNullOrUndefined(this.bdd[name]) ) { return new Promise((resolve, reject) => { resolve(self.bdd[name]); }); } console.log(`get DB: ?? ${ name } ??`); - if(this.bddPomise[name] === null) { + if(isNullOrUndefined(this.bddPomise[name])) { this.bddPomise[name] = []; // Try to load Local Data (storage) let retriveBDDString = null; @@ -142,32 +127,12 @@ export class BddService { } } return new Promise((resolve, reject) => { - if(self.bdd[name] !== null) { + if(!isNullOrUndefined(self.bdd[name])) { resolve(self.bdd[name]); return; } self.bddPomise[name].push({ resolve: resolve, reject: reject }); }); } - - getType(): Promise { - return this.get('type'); - } - - getSeries(): Promise { - return this.get('series'); - } - - getSeason(): Promise { - return this.get('season'); - } - - getUniverse(): Promise { - return this.get('universe'); - } - - getVideo(): Promise { - return this.get('video'); - } } diff --git a/front/src/common/service/http-wrapper.ts b/front/src/common/service/http-wrapper.ts index 553525c..2701004 100644 --- a/front/src/common/service/http-wrapper.ts +++ b/front/src/common/service/http-wrapper.ts @@ -10,7 +10,7 @@ import { HttpClient, HttpHeaders, HttpEventType } from '@angular/common/http'; import { environment } from 'environments/environment'; import { SessionService } from './session'; -import { isArrayOf, isArrayOfs, isBoolean, isNullOrUndefined, isNumber, isString } from 'common/utils'; +import { isArrayOfs, isBoolean, isNullOrUndefined, isNumber, isString } from 'common/utils'; export enum HTTPRequestModel { POST = "POST", @@ -230,7 +230,7 @@ export class HttpWrapperService { } return out; } - + // Deprecated ... old model stream post(uriRest:string, headerOption:any, data:any, progress:ProgressCallback = null) { console.log(`-------------------------------------------------------\nHTTP-wrapper POST '${ uriRest }'\n\t\theaderOption=${ JSON.stringify(headerOption, null, 2)}\n\t\tdata=${ JSON.stringify(data, null, 2)}`); this.addTokenIfNeeded(headerOption); @@ -286,265 +286,9 @@ export class HttpWrapperService { } reject({ status:error.status, data:error.error }); }); - - /* - - switch (event.type) { - - case HttpEventType.UploadProgress: - const progress = Math.round(100 * event.loaded / event.total); - return { status: 'progress', message: progress }; - - case HttpEventType.Response: - return event.body; - default: - return `Unhandled event: ${event.type}`; - } - }) - ); - */ - }); - } - - putwwww(uriRest:string, headerOption:any, data:any) { - console.log(`-------------------------------------------------------\nHTTP-wrapper PUT '${ uriRest }'\n\t\theaderOption=${ JSON.stringify(headerOption, null, 2)}\n\t\tdata=${ JSON.stringify(data, null, 2)}`); - this.addTokenIfNeeded(headerOption); - let connectionAdresse = this.createRESTCall(uriRest, {}); - const httpOption = { - headers: new HttpHeaders(headerOption) - }; - return new Promise((resolve, reject) => { - if(this.displayReturn === true) { - console.log(`call POST ${ connectionAdresse } data=${ JSON.stringify(data, null, 2)}`); - } - let request = this.http.put(connectionAdresse, data, httpOption); - let self = this; - request.subscribe((res: any) => { - if(self.displayReturn === true) { - console.log(`!! data ${ JSON.stringify(res, null, 2)}`); - } - if(res) { - if(res.httpCode) { - resolve({ status:res.httpCode, data:res }); - } else { - resolve({ status:200, data:res }); - } - } else { - resolve({ status:200, data:'' }); - } - }, - (error) => { - if(self.displayReturn === true) { - console.log(`an error occured status: ${ error.status}`); - console.log(`answer: ${ JSON.stringify(error, null, 2)}`); - } - reject({ status:error.status, data:error.error }); - }); - }); - } - - delete(uriRest:string, headerOption:any) { - this.addTokenIfNeeded(headerOption); - let connectionAdresse = this.createRESTCall(uriRest, {}); - const httpOption = { - headers: new HttpHeaders(headerOption) - }; - return new Promise((resolve, reject) => { - if(this.displayReturn === true) { - console.log(`call POST ${ connectionAdresse}`); - } - let request = this.http.delete(connectionAdresse, httpOption); - let self = this; - request.subscribe((res: any) => { - if(self.displayReturn === true) { - console.log(`!! data ${ JSON.stringify(res, null, 2)}`); - } - if(res) { - if(res.httpCode) { - resolve({ status:res.httpCode, data:res }); - } else { - resolve({ status:200, data:res }); - } - } else { - resolve({ status:200, data:'' }); - } - }, - (error) => { - if(self.displayReturn === true) { - console.log(`an error occured status: ${ error.status}`); - console.log(`answer: ${ JSON.stringify(error, null, 2)}`); - } - reject({ status:error.status, data:error.error }); - }); - }); - } - - uploadFileMultipart(base:string, id:number, file:File): any { - console.log(`Upload file to ${ base}`); - - let url = base; - if(id !== null) { - url = `${url }/${ id}`; - } - let formData = new FormData(); - formData.append('upload', file); - let headers = new Headers(); - console.log(`upload filename : ${ file.name}`); - let extention = file.name.split('.').pop(); - if(extention === 'jpg') { - headers.append('Content-Type', 'image/jpeg'); - } else if(extention === 'png') { - headers.append('Content-Type', 'image/png'); - } else if(extention === 'mkv') { - headers.append('Content-Type', 'video/x-matroska'); - } else if(extention === 'webm') { - headers.append('Content-Type', 'video/webm'); - } else { - return null; - } - headers.append('filename', file.name); - - const httpOption = { - headers: headers, - reportProgress: true, - }; - return new Promise((resolve, reject) => { - this.post(url, httpOption, formData) - .then((response: any) => { - console.log(`URL: ${ url }\nRespond(${ response.status }): ${ JSON.stringify(response.data, null, 2)}`); - if(response.status === 200) { - resolve(response.data); - return; - } - reject('An error occured'); - }, (response: any) => { - if(typeof response.data === 'undefined') { - reject('return ERROR undefined'); - } else { - reject(`return ERROR ${ JSON.stringify(response.data, null, 2)}`); - } - }); - }); - } - uploadFileBase64(base:string, id:number, file:File): any { - console.log(`Upload file to ${ base}`); - - let url = base; - if(id !== null) { - url = `${url }/${ id}`; - } - let self = this; - let reader = new FileReader(); - reader.readAsArrayBuffer(file); - return new Promise((resolve, reject) => { - reader.onload = () => { - let headers:any = {};// new Headers(); - console.log(`upload filename : ${ file.name}`); - let extention = file.name.split('.').pop(); - if(extention === 'jpg') { - // headers.append('Content-Type', "image/jpeg"); - headers['Content-Type'] = 'image/jpeg'; - headers['mime-type'] = 'image/jpeg'; - } else if(extention === 'jpeg') { - // headers.append('Content-Type', "image/jpeg"); - headers['Content-Type'] = 'image/jpeg'; - headers['mime-type'] = 'image/jpeg'; - } else if(extention === 'webp') { - // headers.append('Content-Type', "image/webp"); - headers['Content-Type'] = 'image/webp'; - headers['mime-type'] = 'image/webp'; - } else if(extention === 'png') { - // headers.append('Content-Type', "image/png"); - headers['Content-Type'] = 'image/png'; - headers['mime-type'] = 'image/png'; - } else if(extention === 'mkv') { - headers['Content-Type'] = 'video/x-matroska'; - headers['mime-type'] = 'video/x-matroska'; - } else if(extention === 'webm') { - headers['Content-Type'] = 'video/webm'; - headers['mime-type'] = 'video/webm'; - } else { - return null; - } - // headers.append('filename', file.name); - headers.filename = file.name; - - self.post(url, headers, reader.result) - .then((response: any) => { - console.log(`URL: ${ url }\nRespond(${ response.status }): ${ JSON.stringify(response.data, null, 2)}`); - if(response.status === 200) { - resolve(response.data); - return; - } - reject('An error occured'); - }, (response: any) => { - if(typeof response.data === 'undefined') { - reject('return ERROR undefined'); - } else { - reject('return ERROR ...');// + JSON.stringify(response, null, 2)); - } - }); - }; - }); - } - uploadFile(base:string, file:File): any { - console.log(`Upload file to ${ base}`); - - let url = base; - let self = this; - let reader = new FileReader(); - reader.readAsArrayBuffer(file); - return new Promise((resolve, reject) => { - reader.onload = () => { - let headers: any = {};// new Headers(); - console.log(`upload filename : ${ file.name}`); - let extention = file.name.split('.').pop(); - if(extention === 'jpg') { - // headers.append('Content-Type', "image/jpeg"); - headers['Content-Type'] = 'image/jpeg'; - headers['mime-type'] = 'image/jpeg'; - } else if(extention === 'jpeg') { - // headers.append('Content-Type', "image/jpeg"); - headers['Content-Type'] = 'image/jpeg'; - headers['mime-type'] = 'image/jpeg'; - } else if(extention === 'webp') { - // headers.append('Content-Type', "image/webp"); - headers['Content-Type'] = 'image/webp'; - headers['mime-type'] = 'image/webp'; - } else if(extention === 'png') { - // headers.append('Content-Type', "image/png"); - headers['Content-Type'] = 'image/png'; - headers['mime-type'] = 'image/png'; - } else if(extention === 'mkv') { - headers['Content-Type'] = 'video/x-matroska'; - headers['mime-type'] = 'video/x-matroska'; - } else if(extention === 'webm') { - headers['Content-Type'] = 'video/webm'; - headers['mime-type'] = 'video/webm'; - } else { - return null; - } - // headers.append('filename', file.name); - headers.filename = file.name; - - self.post(url, headers, reader.result) - .then((response: any) => { - console.log(`URL: ${ url }\nRespond(${ response.status }): ${ JSON.stringify(response.data, null, 2)}`); - if(response.status === 200) { - resolve(response.data); - return; - } - reject('An error occured'); - }, (response: any) => { - if(typeof response.data === 'undefined') { - reject('return ERROR undefined'); - } else { - reject('return ERROR ...');// + JSON.stringify(response, null, 2)); - } - }); - }; }); } + // Deprecated ... old model stream uploadMultipart(base:string, multipart:FormData, progress: ProgressCallback): any { console.log(`Upload multipart to ${ base}`); @@ -599,8 +343,7 @@ export class HttpWrapperService { }); } - // Complex wrapper to simplify interaction: - //// TODO change thje base and ID to manage a single [] ... + // Complex wrapper to simplify interaction:s putSpecific(urlPath: UrlPath, data: object):Promise { return new Promise((resolve, reject) => { this.request({ diff --git a/front/src/common/service/index.ts b/front/src/common/service/index.ts index 9be6424..14c9d04 100644 --- a/front/src/common/service/index.ts +++ b/front/src/common/service/index.ts @@ -1,3 +1,4 @@ +import { BddService } from "./bdd"; import { CookiesService } from "./cookies"; import { HttpWrapperService, ModelResponseHttp, HTTPRequest, HTTPMimeType, HTTPRequestModel } from "./http-wrapper"; import { StorageService } from "./local-storage"; @@ -7,6 +8,7 @@ import { SSOService } from "./sso"; import { UserService } from "./user"; export { + BddService, CookiesService, StorageService, HttpWrapperService, diff --git a/front/src/common/utils/index.ts b/front/src/common/utils/index.ts index c98e482..68201fc 100644 --- a/front/src/common/utils/index.ts +++ b/front/src/common/utils/index.ts @@ -1,6 +1,6 @@ -import { DataInterface } from "./dataInterface"; +import { DataInterface, TypeCheck } from "./dataInterface"; import { isArray, isArrayOf, isBoolean, isNull, isNullOrUndefined, isNumber, isNumberFinite, isObject, isOptionalOf, isOptionalArrayOf, isString, isUndefined, isArrayOfs } from "./validator"; export { @@ -18,5 +18,6 @@ export { isOptionalOf, isOptionalArrayOf, DataInterface, + TypeCheck, }