SHARE
TWEET

Untitled

a guest Apr 21st, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient } from '@angular/common/http';
  3. import { BehaviorSubject, Observable, Subject } from 'rxjs';
  4. import { map } from 'rxjs/operators';
  5. import { environment } from '../../environments/environment';
  6. import { Match } from '../models/match';
  7. import { first } from 'rxjs/operators';
  8.  
  9. @Injectable({
  10.   providedIn: 'root'
  11. })
  12. export class MatchService {
  13.  
  14.   private matchSource = new BehaviorSubject(new Match);
  15.   public selectedMatchAsObservable = this.matchSource.asObservable();
  16.   private selectedMatch : Match;
  17.  
  18.   private matchesSource = new BehaviorSubject([]);
  19.   public matches = this.matchesSource.asObservable();
  20.   private localMatches : Match[] = [];
  21.  
  22.   constructor(private http: HttpClient){}
  23.        
  24.   setSelectedMatch(post: Match){
  25.       this.matchSource.next(post);
  26.       this.selectedMatch = post;
  27.       console.log("current selected match: " + post.title);
  28.   }
  29.  
  30.   getSelectedMatch(){
  31.     return this.selectedMatch;
  32.   }
  33.  
  34.   getAll() {
  35.     this.http.get<Match[]>(`${environment.apiUrl}/matches`).pipe(first()).subscribe(matches => {
  36.         this.matchesSource.next(matches);
  37.         this.localMatches = matches;
  38.     });
  39.   }
  40.  
  41.   getById(id: number) {
  42.       return this.http.get(`${environment.apiUrl}/matches/` + id);
  43.   }
  44.  
  45.   update(post: Match) {
  46.     let postAsFormData = new FormData();
  47.     postAsFormData.append('title', post.title);
  48.     postAsFormData.append('swimmingPool', post.swimmingPool);
  49.     postAsFormData.append('organiser', post.organiser);
  50.  
  51.     this.http.put<Match>(`${environment.apiUrl}/matches/` + post._id, postAsFormData).pipe(first()).subscribe(updatedPost => {
  52.       const index = this.localMatches.indexOf(<Match> post, 0);
  53.       if (index > -1) {
  54.           this.localMatches[index] = updatedPost;
  55.       }  
  56.       this.matchesSource.next(this.localMatches);
  57.     })
  58.   }
  59.  
  60.   delete(post: Match) {
  61.       this.http.delete(`${environment.apiUrl}/matches/` + post._id).pipe(first()).subscribe(x => {
  62.         const index = this.localMatches.indexOf(<Match> post, 0);
  63.         if (index > -1) {
  64.            this.localMatches.splice(index, 1);
  65.         }  
  66.         this.matchesSource.next(this.localMatches);
  67.     });
  68.   }
  69.  
  70.   create(post: Match) {
  71.     let postAsFormData = new FormData();
  72.     postAsFormData.append('title', post.title);
  73.     postAsFormData.append('swimmingPool', post.swimmingPool);
  74.     postAsFormData.append('organiser', post.organiser);
  75.  
  76.     this.http.post(`${environment.apiUrl}/matches/`, postAsFormData).pipe(first()).subscribe(post => {
  77.         this.localMatches.push(<Match> post);
  78.         this.matchesSource.next(this.localMatches);
  79.     });
  80.   }}
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top