aboutsummaryrefslogtreecommitdiff
path: root/src/app/services/technology.service.ts
blob: dbdc039de1b61e33a24ac7a8f3148cb5cffc6d7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Guid } from 'guid-typescript';
import { Observable } from 'rxjs';
import { Technology } from 'src/models/technology';
import { AppConstants } from '../app-constants.module';
import { TokenService } from './token.service';

@Injectable({
  providedIn: 'root'
})
export class TechnologyService {
  constructor(private _http: HttpClient, private _tokenService: TokenService)
  { }

  /* Requests from session storage */

   createTechnologyWithSessionStorageRequest(name: string): Observable<object> {
    const token = this._tokenService.getTokenFromSessionStorage();

    return this.createtTechnologyRequest(name, token);
  }

  getAllTechnologiesWithSessionStorageRequest(): Observable<object> {
    const token = this._tokenService.getTokenFromSessionStorage();

    return this.getAllTechnologiesRequest(token);
  }

  putTechnologyWithSessionStorageRequest(langId: Guid, newName: string): Observable<object> {
    const token = this._tokenService.getTokenFromSessionStorage();

    return this.putTechnologyRequest(token, langId, newName);
  }

  deleteTechnologyWithSessionStorageRequest(langId: Guid): Observable<object> {
    const token = this._tokenService.getTokenFromSessionStorage();

    return this.deleteTechnologyRequest(token, langId);
  }

  /* Technology requests */

  createtTechnologyRequest(name: string, authToken: string): Observable<object> {
    const body = {
      name: name
    };
    const options = {
      headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
    };
    return this._http.post(AppConstants.API_TECHNOLOGY_URL, body, options);
  }

  getTechnologyRequest(techId: Guid): Observable<object> {
    const options = {
      params: new HttpParams().set('Id', techId.toString())
    };
    return this._http.get(AppConstants.API_TECHNOLOGY_URL, options);
  }

  getAllTechnologiesRequest(authToken: string): Observable<object> {
    const options = {
      headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
    };
    return this._http.get(AppConstants.API_TECHNOLOGY_URL + '/GetTechnologies', options);
  }

  getFullTechnologiesFromIncomplete(givenTechnologies: Technology[]): Promise<Technology[]> {
    if (givenTechnologies.length === 0) {
      return new Promise(resolve => resolve(givenTechnologies));
    }

    // This accepts language array with incomplete languages, meaning
    // languages that only have an id, but no name
    return new Promise(resolve => {
      const lastGuid = givenTechnologies[givenTechnologies.length - 1].id;

      // For each language, request his name and assign it
      for (const tech of givenTechnologies) {
        this.getTechnologyRequest(tech.id).subscribe(
          (result: object) => {
            // this only assigns the "name" property to the language,
            // because only the name is returned from the request
            Object.assign(tech, result);

            if (lastGuid === tech.id) {
              resolve(givenTechnologies);
            }
          }
        );
      }
    });
  }

  putTechnologyRequest(authToken: string, langId: Guid, newName: string): Observable<object> {
    const body = {
      name: newName
    };
    const options = {
      params: new HttpParams().set('Id', langId.toString()),
      headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
    };
    return this._http.put(AppConstants.API_TECHNOLOGY_URL, body, options);
  }

  deleteTechnologyRequest(authToken: string, langId: Guid): Observable<object> {
    const options = {
      params: new HttpParams().set('Id', langId.toString()),
      headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
    };
    return this._http.delete(AppConstants.API_TECHNOLOGY_URL, options);
  }
}