daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Sep 3rd, 2018 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { Http, Headers } from '@angular/http';
  3. import 'rxjs/add/operator/map';
  4. import { HttpClient, HttpHeaders } from '@angular/common/http';
  5. import { Resource } from '../models/resource.model';
  6. import { Observable } from 'rxjs/Observable';
  7. import { Serializer } from '../serializer/serializer';
  8. import { AuthenticationService } from './authentication.service';
  9.  
  10. @Injectable()
  11. export class SharedService<T extends Resource> {
  12.  
  13.   constructor(
  14.     private httpClient: HttpClient,
  15.     private url: string,
  16.     private endpoint: string,
  17.     private authentication: AuthenticationService,
  18.     private serializer: Serializer
  19.   ) { }
  20.  
  21.  
  22.   create(resource: T) {
  23.     let headers = new HttpHeaders();
  24.     headers = headers.set('Content-Type', 'application/json; charset=utf-8');
  25.     return this.httpClient.post(`${this.url}/${this.endpoint}`, JSON.stringify(resource), { headers: headers });
  26.   }
  27.  
  28.  
  29.  
  30.   //PUT
  31.   update(item: T): Observable<T> {
  32.     return this.httpClient.put<T>(`${this.url}/${this.endpoint}`, JSON.stringify(item), { headers: this.addHeaders() })
  33.       .map(data => this.serializer.fromJson(data) as T);
  34.   }
  35.  
  36.  
  37.  
  38.   //GET
  39.   read(id: number): Observable<T> {
  40.     return this.httpClient.get(`${this.url}/${this.endpoint}/${id}`, { headers: this.addHeaders() })
  41.       .map((data: any) => this.serializer.fromJson(data) as T);
  42.   }
  43.  
  44.   //GET ALL
  45.   list(): Observable<T[]> {
  46.     return this.httpClient.get<T>(`${this.url}/${this.endpoint}` , {headers : this.addHeaders()})
  47.       .map((data: any) =>
  48.         this.convertData(data.items));
  49.   }
  50.  
  51.   protected convertData(data: any): T[] {
  52.  
  53.     return data.map(item => {this.serializer.fromJson(item)});
  54.   }
  55.  
  56.   protected addHeaders() {
  57.     let token = ('Bearer ' + this.authentication.getToken()).valueOf();
  58.     let headers = new HttpHeaders();
  59.     headers = headers.set('Content-Type', 'application/json; charset=utf-8').set('Authorization', token);
  60.     return headers;
  61.   }
  62.  
  63. }
  64.    
  65. import { User } from "../models/user/user.model";
  66. import { Serializer } from "./serializer";
  67. import { Resource } from "../models/resource.model";
  68.  
  69. export class UserSerializer extends Serializer {
  70.     fromJson(json: any): Resource {
  71.         const user = new User();
  72.         user.id = json.id;
  73.         user.name = json.name;
  74.         user.surname = json.surname;
  75.         user.email = json.email;
  76.         user.phoneNumber = json.phoneNumber;
  77.         user.password = json.password;
  78.         user.username = json.username;
  79.         user.active = json.active;
  80.         console.log('serializer');
  81.         console.log(user);
  82.         return user;
  83.     }
  84. }
  85.    
  86. import { Resource } from "../resource.model";
  87.  
  88. export class User extends Resource{
  89.     username: string;
  90.     email: string;
  91.     name: string;
  92.     surname: string;
  93.     phoneNumber: string;
  94.     password?: string;
  95.     active : boolean;
  96. }
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