2019-02-15 07:25:55 +08:00
|
|
|
import {Injectable} from '@angular/core';
|
2019-03-03 17:30:12 +08:00
|
|
|
import {NetworkService} from '../../model/network/network.service';
|
2019-02-15 07:25:55 +08:00
|
|
|
import {BehaviorSubject} from 'rxjs';
|
2019-03-03 17:30:12 +08:00
|
|
|
import {PersonDTO} from '../../../../common/entities/PersonDTO';
|
2019-02-15 07:25:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class FacesService {
|
|
|
|
public persons: BehaviorSubject<PersonDTO[]>;
|
|
|
|
|
|
|
|
constructor(private networkService: NetworkService) {
|
2019-03-04 04:17:42 +08:00
|
|
|
this.persons = new BehaviorSubject<PersonDTO[]>([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async setFavourite(person: PersonDTO, isFavourite: boolean): Promise<void> {
|
|
|
|
const updated = await this.networkService.postJson<PersonDTO>('/person/' + person.name, {isFavourite: isFavourite});
|
|
|
|
const updatesList = this.persons.getValue();
|
|
|
|
for (let i = 0; i < updatesList.length; i++) {
|
|
|
|
if (updatesList[i].id === updated.id) {
|
|
|
|
updatesList[i] = updated;
|
|
|
|
this.persons.next(updatesList);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-02-15 07:25:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public async getPersons() {
|
|
|
|
this.persons.next((await this.networkService.getJson<PersonDTO[]>('/person')).sort((a, b) => a.name.localeCompare(b.name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|