All files / src/services/friend friend.service.ts

87.5% Statements 21/24
100% Branches 0/0
100% Functions 6/6
85.71% Lines 18/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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              1x     4x 4x 4x 4x   4x 4x       1x   1x       1x   1x 1x   1x       1x                 1x   1x 1x   1x      
import { EventEmitter, Injectable } from '@angular/core';
 
import { Friend } from '../../models/friend';
import { ApiService, RestParameters } from '../api/api.service';
import { EnvironmentService } from '../environment/environment.service';
 
@Injectable({ providedIn: 'root' })
export class FriendService {
  public basePath: string;
 
  public onCreate = new EventEmitter<Friend>();
  public onDelete = new EventEmitter<Friend>();
  public onRead = new EventEmitter<Friend[]>();
  public onUpdate = new EventEmitter<Friend>();
 
  constructor(private apiService: ApiService, private environmentService: EnvironmentService) {
    this.basePath = this.environmentService.friendApiBaseUrl;
  }
 
  public async count(parameters: RestParameters): Promise<number> {
    const response = await this.apiService.request('get', `${this.basePath}/count`, parameters);
 
    return response.count;
  }
 
  public async create(parameters: Partial<Friend>): Promise<Friend> {
    const response = await this.apiService.request('post', this.basePath, parameters);
 
    const record = new Friend(response.record);
    this.onCreate.emit(record);
 
    return record;
  }
 
  public async delete(_id: string): Promise<Friend> {
    const response = await this.apiService.request('delete', `${this.basePath}/${_id}`, null);
 
    const record = new Friend(response.record);
    this.onDelete.emit(record);
 
    return record;
  }
 
  public async find(parameters: RestParameters): Promise<Friend[]> {
    const response = await this.apiService.request('get', this.basePath, parameters);
 
    const records = response.records.map(record => new Friend(record));
    this.onRead.emit(records);
 
    return records;
  }
}