[sdc] rebase update
[sdc.git] / catalog-ui / src / app / services / category-resource-service.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 'use strict';
22 // import 'angular-resource';
23 import {IAppConfigurtaion, IMainCategory, IUserProperties} from "../models";
24 import {Categories} from "../models/categories";
25
26 // Define an interface of the object you want to use, providing it's properties
27 export interface ICategoryResource extends IUserProperties,ng.resource.IResource<ICategoryResource> {
28     name:string;
29     uniqueId:string;
30     subcategories:Array<ICategoryResource>;
31
32     getAllCategories(params?:Object, success?:Function, error?:Function):Categories;
33     $saveSubCategory(params?:Object, success?:Function, error?:Function):any;
34     $deleteSubCategory(params?:Object, success?:Function, error?:Function):any;
35 }
36
37 // Define your resource, adding the signature of the custom actions
38 export interface ICategoryResourceClass extends ng.resource.IResourceClass<ICategoryResource> {
39     getAllCategories(params?:Object, success?:Function, error?:Function):Categories;
40     saveSubCategory(params?:Object, success?:Function, error?:Function):any;
41     deleteSubCategory(params?:Object, success?:Function, error?:Function):any;
42 }
43
44 export class CategoryResourceService {
45
46     public static getResource = ($resource:ng.resource.IResourceService,
47                                  sdcConfig:IAppConfigurtaion):ICategoryResourceClass => {
48
49         // Define your custom actions here as IActionDescriptor
50         let getAllCategoriesAction:ng.resource.IActionDescriptor = {
51             method: 'GET',
52             isArray: false,
53             url: sdcConfig.api.root + sdcConfig.api.GET_categories
54         };
55         let saveSubCategory:ng.resource.IActionDescriptor = {
56             method: 'POST',
57             isArray: false,
58             url: sdcConfig.api.root + sdcConfig.api.POST_subcategory
59         };
60         let deleteSubCategory:ng.resource.IActionDescriptor = {
61             method: 'DELETE',
62             isArray: false,
63             url: sdcConfig.api.root + sdcConfig.api.POST_subcategory
64         };
65
66
67         let url:string = sdcConfig.api.root + sdcConfig.api.POST_category;
68         let categoryResource:ICategoryResourceClass = <ICategoryResourceClass>$resource(
69             url,
70             {types: '@types', categoryId: '@categoryId'},
71             {
72                 getAllCategories: getAllCategoriesAction,
73                 saveSubCategory: saveSubCategory,
74                 deleteSubCategory: deleteSubCategory
75             }
76         );
77
78         return categoryResource;
79     }
80 }
81 CategoryResourceService.getResource.$inject = ['$resource', 'sdcConfig'];