Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / demoApp / src / services / authorService.ts
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 import { IAuthor } from '../models/author';
19
20 import * as $ from 'jquery';
21
22 const base_url = 'https://api.mfico.de/v1/authors';
23
24 /** 
25  * Represents a web api accessor service for all author related actions.
26  */
27 class AuthorService {
28
29   /**
30    * Gets all known authors from the backend.
31    * @returns A promise of the type array of @see {@link IAuthor} containing all known authors.
32    */
33   public getAllAuthors(): Promise<IAuthor[]> {
34     return new Promise((resolve: (value: IAuthor[]) => void, reject: (err: any) => void) => {
35       $.ajax({ method: 'GET', url: base_url })
36         .then((data) => { resolve(data); }, (err) => { reject(err); });
37     });
38   }
39
40   /**
41    * Gets an author by its id from the backend.
42    * @returns A promise of the type @see {@link IAuthor} containing the author to get.
43    */
44   public getAuthorById(id: string | number): Promise<IAuthor> {
45     return new Promise((resolve: (value: IAuthor) => void, reject: (err: any) => void) => {
46       $.ajax({ method: 'GET', url: base_url + '/' + id })
47         .then((data) => { resolve(data); }, (err) => { reject(err); });
48     });
49   }
50
51
52   /**
53  * Saves the given author to the backend api.
54  * @returns A promise of the type @see {@link IAuthor} containing the autor returned by the backend api.
55  */
56   public saveAuthor(author: IAuthor): Promise<IAuthor> {
57     return new Promise((resolve: (value: IAuthor) => void, reject: (err: any) => void) => {
58       // simulate server save
59       window.setTimeout(() => {
60         if (Math.random() > 0.8) {
61           reject('Could not save author.');
62         } else {
63           resolve(author);
64         }
65       }, 800); // simulate a short network delay
66     });
67   }
68 }
69
70 // return as a singleton
71 export const authorService = new AuthorService();
72 export default authorService;