Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 19 | import {IXosModelDefsField} from '../../core/services/helpers/config.helpers'; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 20 | import {IXosAppConfig} from '../../../index'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 21 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 22 | // Models interfaces |
| 23 | export interface IXosModelDefsField { |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 24 | name: string; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 25 | type: string; |
| 26 | validators?: any; |
| 27 | hint?: string; |
| 28 | relation?: { |
| 29 | model: string; |
| 30 | type: string; |
| 31 | }; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 34 | export interface IXosModelDefsRelation { |
| 35 | model: string; // model name |
| 36 | type: string; // relation type |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 37 | on_field: string; // the field that is containing the relation |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 40 | export interface IXosModeldef { |
| 41 | fields: IXosModelDefsField[]; |
| 42 | relations?: IXosModelDefsRelation[]; |
| 43 | name: string; |
| 44 | app: string; |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 45 | description: string; |
| 46 | verbose_name: string; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | export interface IXosModeldefsService { |
| 50 | get(): Promise<IXosModeldef[]>; |
| 51 | } |
| 52 | |
| 53 | export class XosModeldefsService implements IXosModeldefsService { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 54 | |
| 55 | static $inject = ['$http', '$q', 'AppConfig']; |
| 56 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 57 | constructor( |
| 58 | private $http: angular.IHttpService, |
| 59 | private $q: angular.IQService, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 60 | private AppConfig: IXosAppConfig |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 61 | ) { |
| 62 | } |
| 63 | |
| 64 | public get(): Promise<any> { |
| 65 | const d = this.$q.defer(); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 66 | this.$http.get(`${this.AppConfig.apiEndpoint}/modeldefs`) |
| 67 | .then((res: any) => { |
| 68 | d.resolve(res.data.items); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 69 | }) |
| 70 | .catch(e => { |
| 71 | d.reject(e); |
| 72 | }); |
| 73 | return d.promise; |
| 74 | } |
| 75 | } |