HEX
Server: nginx/1.24.0
System: Linux nowruzgan 6.8.0-57-generic #59-Ubuntu SMP PREEMPT_DYNAMIC Sat Mar 15 17:40:59 UTC 2025 x86_64
User: babak (1000)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: /var/dev/nowruzgan/admin/src/app/services/entry.service.ts
import { Injectable } from '@angular/core';
import { GenericService } from './generic.service';
import { Entry, Batch } from '../data.types';
import { environment } from '../../environments/environment';
import { Observable, of, map, catchError } from 'rxjs';

@Injectable()
export class EntryService extends GenericService {
  async list(entriesBatch: Batch<Entry>) {
    entriesBatch.meta.busy = true;
    let skip: string = `skip=${entriesBatch.offset || 0}`;
    let limit: string = `limit=${entriesBatch.limit || 30}`;
    let sort: string = `sort=${entriesBatch.sort}`;
    let filter: string = entriesBatch.filter ? `filter=${entriesBatch.filter}` : '';
    let response = await this.http
      .get(`${environment.apiBase}/encyc/entry?${skip}&${limit}&${sort}&${filter}`, this.headers)
      .toPromise().catch(e => {
        this.handleError(e);
        return {sum: 0, limit: 0, list: []};
      });
    entriesBatch.absorb(response);
    entriesBatch.meta.busy = false;
  }

  load(entry: Entry): Observable<boolean> {
    if(!entry.id) return of(false);
    entry.meta.busyLoading = true;
    entry.meta.errorLoading = false;

    return this.http
      .get(`${environment.apiBase}/encyc/entry/${entry.id}`, this.headers)
      .pipe(
        map((data: any) => {
          entry.meta.busyLoading = false;
          entry.meta.errorLoading = false;
          if(!data?.id) {
            entry.meta.errorLoading = true;
            return false;
          }
          entry.absorb(data);
          return true;
        }),        
        catchError(error => {
          entry.meta.busyLoading = false;
          entry.meta.errorLoading = true;
          console.log(error);
          return of(false)
        }
      )
    );
  }

  save(entry: Entry): Observable<boolean> {
    entry.meta.busySave = true;
    entry.meta.errorSave = false;

    return this.http
      .patch(`${environment.apiBase}/encyc/entry/${entry.id}`, entry.trans, this.headers)
      .pipe(
        map((result: any) => {
          entry.meta.busySave = false;
          entry.meta.errorSave = !result;
          return !!result;
        }),        
        catchError(error => {
          entry.meta.busySave = false;
          entry.meta.errorSave = true;
          console.log(error);
          return of(false)
        })
      );
  }

  async create(entry: Entry): Promise<Entry | null> {
    try {
      return new Entry(await this.http
        .post(`${environment.apiBase}/encyc/entry`, entry.trans, this.headers)
        .toPromise());
    }catch(e) {
      this.handleError(e);
      return null;
    }
  }

  async remove(entry: Entry): Promise<boolean> {
    entry.meta.busyDelete = true;
    entry.meta.errorDelete = false;
    let result: any = await this.http
      .delete(`${environment.apiBase}/encyc/entry/${entry.id}`, this.headers)
      .toPromise()
      .catch(error => this.handleError(error))
    entry.meta.busyDelete = false;
    entry.meta.errorDelete = !result;
    return !!result;
  }

  async search(value: string): Promise<Entry[]> {
    let response: any = await this.http
      .get(`${environment.apiBase}/encyc/entry?filter=${value}`, this.headers)
      .toPromise().catch(e => {this.handleError(e); return{list: []};});
    return response.list.map((record: any) => new Entry(record));
  }
}