daily pastebin goal
58%
SHARE
TWEET

CategoryService

a guest Aug 13th, 2016 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { Category } from '../model/category';
  3. import { Http, Response, Headers, RequestOptions } from '@angular/http';
  4. import { Observable } from 'rxjs/Observable';
  5.  
  6. @Injectable()
  7. export class CategoryService {
  8.     private _categoryUrl = 'http://localhost:8080/rest/category';
  9.  
  10.     constructor(private _http : Http) { }
  11.  
  12.     createNewCategory(categoryName:string,isMain:boolean,supCategory:Category){
  13.  
  14.         let body = JSON.stringify({ "name":categoryName,"mainCategory":isMain,"parentCategory":supCategory});
  15.         //console.log(body);
  16.         let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
  17.         headers.append('Access-Control-Allow-Origin', '*');
  18.         let options = new RequestOptions({ headers: headers, method: "post" });
  19.  
  20.         return this._http.post(this._categoryUrl, body,options);
  21.     }
  22.     private handleError (error: Response) {
  23.         console.error(error);
  24.         return Observable.throw(error.json().error || ' error');
  25.     }
  26.  
  27.     logIn() {
  28.         let loginUrl = 'http://localhost:8080/j_spring_security_check';
  29.         let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
  30.         let body = 'j_username=admin&j_password=admin';
  31.         return this._http.post(loginUrl, body, { headers: headers });
  32.     }
  33.    
  34.  
  35.     getAllCategories(): Observable<Category[]> {
  36.         return this._http.get(this._categoryUrl)
  37.             .map((response:Response) => <Category[]>response.json())
  38.             .do(data => console.log("All categories: " + JSON.stringify(data)))
  39.             .catch(this.handleError);
  40.            
  41.     }
  42.  
  43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top