-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame.service.ts
120 lines (107 loc) · 4.3 KB
/
game.service.ts
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
114
115
116
117
118
119
120
import { Game } from './../models/game.model';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
const BASE_URL = '/api/games/';
@Injectable({ providedIn: 'root' })
export class GameService {
constructor(private http: HttpClient) { }
getIndexGames(): Observable<any> {
return this.http.get(BASE_URL
) as Observable<any>;}
getGameCoverImage(game: Game) {
return game.titleImage ? BASE_URL + game.id + '/coverImage' : '/assets/images/no_image.png';
}
getGameplayImage(game: Game, index: number) {
return game.gameplayImages[index - 1] ? BASE_URL + game.id + '/gameplayImage/' + index : '/assets/images/no_image.png';
}
getMoreGames(indexGames: number): Observable<any> {
return this.http.get(BASE_URL + 'moreIndexGames/' + indexGames
) as Observable<any>;
}
getGameById(id: number): Observable<any> {
return this.http.get(BASE_URL + id).pipe(
catchError((error) => {
return throwError(error);
})
) as Observable<any>;
}
deleteGame(id: number): Observable<any> {
return this.http.delete(BASE_URL + id).pipe(
catchError((error) => {
return throwError(error);
})
) as Observable<any>;
}
searchGames(category: String, name: String): Observable<any> {
return this.http.get(BASE_URL + 'search/?name=' + name + '&category=' + category).pipe(
catchError((error) => {
return throwError(error);
})
) as Observable<any>;
}
moreFoundGames(category: String, name: String, indexGames: number): Observable<any> {
return this.http.get(BASE_URL + 'moreFoundGames/' + indexGames + '/?name=' + name + '&category=' + category).pipe(
catchError((error) => {
return throwError(error);
})
) as Observable<any>;
}
addGame(name: string, category: string, price: number, os: string, processor: string, memory: string, directX: string, network: string, hardDrive: string, soundcard: string, graphics: string, imageField: File, imageFields: File[], description: string) {
const formData = new FormData();
formData.append('name', name);
formData.append('category', category);
formData.append('price', price.toString());
formData.append('os', os);
formData.append('processor', processor);
formData.append('memory', memory);
formData.append('directX', directX);
formData.append('network', network);
formData.append('hardDrive', hardDrive);
formData.append('soundcard', soundcard);
formData.append('graphics', graphics);
formData.append('imageField', imageField);
for (let i = 0; i < imageFields.length; i++) {
formData.append('imageFields', imageFields[i]);
}
formData.append('description', description);
return this.http.post(BASE_URL, formData).pipe( map((response: any) => {
return response;
}),
catchError((error: any) => {
return throwError('Something went wrong');
})
) as Observable<any>;
}
editGame(game: Game, imageField: File, imageFields: File[]) {
const formData = new FormData();
formData.append('name', game.name);
formData.append('category', game.category);
formData.append('price', game.price.toString());
formData.append('os', game.os);
formData.append('processor', game.processor);
formData.append('memory', game.memory);
formData.append('directX', game.directX);
formData.append('network', game.network);
formData.append('hardDrive', game.hardDrive);
formData.append('soundCard', game.soundCard);
formData.append('graphics', game.graphics);
formData.append('description', game.description);
if(imageField){
formData.append('imageField', imageField);
}
if(imageFields){
for (let i = 0; i < imageFields.length; i++) {
formData.append('imageFields', imageFields[i]);
}
}
return this.http.put(BASE_URL + game.id, formData).pipe( map((response: any) => {
return response;
}),
catchError((error: any) => {
return throwError('Something went wrong');
})
) as Observable<any>;
}
}