daily pastebin goal
1%
SHARE
TWEET

Untitled

a guest Sep 3rd, 2018 90 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 { Injectable } from '@angular/core';
  66. import { SharedService } from './shared.service';
  67. import { HttpClient, HttpHeaders } from '@angular/common/http';
  68. import { User } from '../models/user/user.model';
  69. import { AuthenticationService } from 'app/service/authentication.service';
  70. import { UserSerializer } from '../serializer/user-serializer';
  71. import { NgForm } from '@angular/forms';
  72.  
  73. @Injectable()
  74. export class UserService extends SharedService<User>{
  75.  
  76.   constructor(httpClient: HttpClient, authenticate: AuthenticationService) {
  77.     super(httpClient,
  78.       'http://localhost:8084/SuperCloud/webresources',
  79.       'user',
  80.       authenticate,
  81.       new UserSerializer()
  82.     );
  83.   }
  84.    
  85. import { User } from "../models/user/user.model";
  86. import { Serializer } from "./serializer";
  87. import { Resource } from "../models/resource.model";
  88.  
  89. export class UserSerializer extends Serializer {
  90.     fromJson(json: any): Resource {
  91.         const user = new User();
  92.         user.id = json.id;
  93.         user.name = json.name;
  94.         user.surname = json.surname;
  95.         user.email = json.email;
  96.         user.phoneNumber = json.phoneNumber;
  97.         user.password = json.password;
  98.         user.username = json.username;
  99.         user.active = json.active;
  100.         console.log('serializer');
  101.         console.log(user);
  102.         return user;
  103.     }
  104. }
  105.    
  106. import { Resource } from "../resource.model";
  107.  
  108. export class User extends Resource{
  109.     username: string;
  110.     email: string;
  111.     name: string;
  112.     surname: string;
  113.     phoneNumber: string;
  114.     password?: string;
  115.     active : boolean;
  116. }
  117.    
  118. ngOnInit() {
  119.         this.userService.list().subscribe(
  120.             (data) => console.log(data)
  121.        );
  122.  
  123.     }
  124.    
  125. return this.httpClient
  126.   .put<T>(`${this.url}/${this.endpoint}`, JSON.stringify(item), {
  127.     headers: this.addHeaders()
  128.   })
  129.   .subscribe(data => this.serializer.fromJson(data) as T);
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