github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/frontend/manage/app/publication/publication.service.ts (about) 1 import { Injectable } from '@angular/core'; 2 import { Http } from '@angular/http'; 3 4 import 'rxjs/add/operator/toPromise'; 5 6 import { Publication } from './publication'; 7 import { MasterFile } from './master-file'; 8 9 import { CrudService } from '../crud/crud.service'; 10 11 declare var Config: any; // this comes from the autogenerated config.js file 12 13 @Injectable() 14 export class PublicationService extends CrudService<Publication> { 15 private masterFileListUrl: string; 16 17 constructor (http: Http) { 18 super(); 19 this.http = http; 20 this.baseUrl = Config.frontend.url + '/api/v1/publications'; 21 this.masterFileListUrl = Config.frontend.url + 22 '/api/v1/repositories/master-files'; 23 } 24 25 decode(jsonObj: any): Publication { 26 return { 27 id: jsonObj.id, 28 uuid: jsonObj.uuid, 29 title: jsonObj.title, 30 status: jsonObj.status, 31 masterFilename: null 32 } 33 } 34 35 encode(obj: Publication): any { 36 return { 37 id: obj.id, 38 title: obj.title, 39 masterFilename: obj.masterFilename 40 } 41 } 42 43 checkByName(name: string): Promise<number> { 44 var self = this 45 return this.http 46 .get( 47 this.baseUrl + "/check-by-title?title=" + name, 48 { headers: this.defaultHttpHeaders }) 49 .toPromise() 50 .then(function (response) { 51 let jsonObj = response.json(); 52 return jsonObj; 53 }) 54 .catch(this.handleError); 55 } 56 57 getMasterFiles(): Promise<MasterFile[]> { 58 return this.http 59 .get( 60 this.masterFileListUrl, 61 { headers: this.defaultHttpHeaders }) 62 .toPromise() 63 .then(function (response) { 64 if (response.ok) { 65 let items: MasterFile[] = []; 66 67 for (let jsonObj of response.json()) { 68 items.push({ 69 name: jsonObj.name 70 }); 71 } 72 73 return items; 74 } else { 75 throw 'Error creating user ' + response.text; 76 } 77 }) 78 .catch(this.handleError); 79 } 80 81 addPublication(pub: Publication): Promise<number> { 82 return this.http 83 .post( 84 this.baseUrl, 85 this.encode(pub), 86 { headers: this.defaultHttpHeaders }) 87 .toPromise() 88 .then(function (response) { 89 if (response.ok) { 90 return 200; 91 } else { 92 throw 'Error creating publication ' + response.text; 93 } 94 }) 95 .catch(this.handleAddError); 96 } 97 98 protected handleAddError(error: any): any { 99 return error.status; 100 } 101 }