2018-03-07 11:56:50 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
2018-10-07 17:46:29 +02:00
|
|
|
import {Rank} from '../../models/model-interfaces';
|
2018-03-07 11:56:50 +01:00
|
|
|
import {Observable} from 'rxjs/Observable';
|
2018-10-07 17:46:29 +02:00
|
|
|
import {ADD, EDIT, LOAD, REMOVE, Store} from '../stores/generic-store';
|
2018-03-07 11:56:50 +01:00
|
|
|
import {AppConfig} from '../../app.config';
|
2018-10-20 22:40:18 +02:00
|
|
|
import {HttpGateway, HttpMethod} from '../http-gateway';
|
2018-10-14 15:56:52 +02:00
|
|
|
import {HttpParams} from '@angular/common/http';
|
2017-05-10 11:04:06 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class RankService {
|
|
|
|
|
|
|
|
ranks$: Observable<Rank[]>;
|
|
|
|
|
2018-10-07 17:46:29 +02:00
|
|
|
rankStore = new Store<Rank>();
|
|
|
|
|
2018-10-09 23:23:41 +02:00
|
|
|
constructor(private httpGateway: HttpGateway,
|
2017-05-10 11:04:06 +02:00
|
|
|
private config: AppConfig) {
|
2018-10-07 17:46:29 +02:00
|
|
|
this.ranks$ = this.rankStore.items$;
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
|
2018-10-14 15:56:52 +02:00
|
|
|
findRanks(query = '', fractionFilter?): Observable<Rank[]> {
|
|
|
|
let searchParams = new HttpParams().append('q', query);
|
2017-05-10 11:04:06 +02:00
|
|
|
if (fractionFilter) {
|
2018-10-14 15:56:52 +02:00
|
|
|
searchParams = searchParams.append('fractFilter', fractionFilter);
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
|
2018-10-14 15:56:52 +02:00
|
|
|
this.httpGateway.get<Rank[]>(this.config.apiRankPath, searchParams)
|
2018-02-26 09:04:27 +01:00
|
|
|
.do((ranks) => {
|
|
|
|
this.rankStore.dispatch({type: LOAD, data: ranks});
|
|
|
|
}).subscribe(_ => {
|
2017-05-10 11:04:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return this.ranks$;
|
|
|
|
}
|
|
|
|
|
2018-07-22 13:09:51 +02:00
|
|
|
getRank(id: number | string): Observable<Rank> {
|
2018-10-14 15:56:52 +02:00
|
|
|
return this.httpGateway.get<Rank>(this.config.apiRankPath + id);
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
|
2017-05-15 15:32:36 +02:00
|
|
|
/**
|
|
|
|
* For creating new data with POST or
|
|
|
|
* update existing with patch PATCH
|
|
|
|
*/
|
2018-10-14 15:56:52 +02:00
|
|
|
submitRank(rank: Rank, imageFile?): Observable<Rank> {
|
2017-08-01 23:52:10 +02:00
|
|
|
let requestUrl = this.config.apiRankPath;
|
2018-10-20 22:40:18 +02:00
|
|
|
let requestMethod: HttpMethod;
|
2017-05-15 15:32:36 +02:00
|
|
|
let accessType;
|
|
|
|
let body;
|
|
|
|
|
|
|
|
if (rank._id) {
|
|
|
|
requestUrl += rank._id;
|
2018-10-20 22:40:18 +02:00
|
|
|
requestMethod = 'PATCH';
|
2017-05-15 15:32:36 +02:00
|
|
|
accessType = EDIT;
|
|
|
|
} else {
|
2018-10-20 22:40:18 +02:00
|
|
|
requestMethod = 'POST';
|
2017-05-15 15:32:36 +02:00
|
|
|
accessType = ADD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (imageFile) {
|
|
|
|
body = new FormData();
|
|
|
|
Object.keys(rank).map((objectKey) => {
|
|
|
|
if (rank[objectKey] !== undefined) {
|
|
|
|
body.append(objectKey, rank[objectKey]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
body.append('image', imageFile, imageFile.name);
|
|
|
|
} else {
|
|
|
|
body = rank;
|
|
|
|
}
|
|
|
|
|
2018-10-20 22:40:18 +02:00
|
|
|
return this.httpGateway.request<Rank>(requestUrl, body, requestMethod)
|
2018-02-26 09:04:27 +01:00
|
|
|
.do(savedRank => {
|
|
|
|
const action = {type: accessType, data: savedRank};
|
2018-10-14 15:56:52 +02:00
|
|
|
// leave some time to save image file before accessing it through list view
|
2018-02-26 09:04:27 +01:00
|
|
|
setTimeout(() => {
|
|
|
|
this.rankStore.dispatch(action);
|
|
|
|
}, 300);
|
|
|
|
});
|
2017-05-15 15:32:36 +02:00
|
|
|
}
|
|
|
|
|
2017-05-15 13:44:37 +02:00
|
|
|
deleteRank(rank: Rank) {
|
2018-10-09 23:23:41 +02:00
|
|
|
return this.httpGateway.delete(this.config.apiRankPath + rank._id)
|
2018-02-26 09:04:27 +01:00
|
|
|
.do(res => {
|
|
|
|
this.rankStore.dispatch({type: REMOVE, data: rank});
|
|
|
|
});
|
2017-05-15 13:44:37 +02:00
|
|
|
}
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|