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

90.32% Statements 28/31
100% Branches 0/0
100% Functions 7/7
89.28% Lines 25/28

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