import { EventEmitter, Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { Game } from '../../models/game'; import { ApiService, RestParameters } from '../api/api.service'; import { EnvironmentService } from '../environment/environment.service'; export interface GameServiceUploadOptions { background?: Blob; icon?: Blob; } @Injectable({ providedIn: 'root' }) export class GameService { public basePath: string; public onCreate = new EventEmitter(); public onDelete = new EventEmitter(); public onRead = new EventEmitter(); public onUpdate = new EventEmitter(); constructor(private apiService: ApiService, private environmentService: EnvironmentService) { this.basePath = this.environmentService.gameApiBaseUrl; } public async count(parameters: RestParameters): Promise { const response = await this.apiService.request('get', `${this.basePath}/count`, parameters); return response.count; } public async create(parameters: Partial): Promise { const response = await this.apiService.request('post', this.basePath, parameters); const record = new Game(response.record); this.onCreate.emit(record); return record; } public async delete(_id: string): Promise { const response = await this.apiService.request('delete', `${this.basePath}/${_id}`, null); const record = new Game(response.record); this.onDelete.emit(record); return record; } public async find(parameters: RestParameters): Promise { const response = await this.apiService.request('get', this.basePath, parameters); const records = response.records.map(record => new Game(record)); this.onRead.emit(records); return records; } public async findOne(_id: string): Promise { const response = await this.apiService.request('get', `${this.basePath}/${_id}`, null); const record = new Game(response.record); this.onRead.emit([record]); return record; } public async update(parameters: Partial): Promise { const response = await this.apiService.request( 'put', `${this.basePath}/${parameters._id}`, parameters, ); const record = new Game(response.record); this.onUpdate.emit(record); return record; } public upload(_id: string, key: string, blobs: Blob[]) { const formData = new FormData(); for (const blob of blobs) { formData.append(key, blob); } return this.apiService.request('post', `${this.basePath}/${_id}/${key}`, formData, { observe: 'events', reportProgress: true, }) as Observable; } }