2017-05-10 11:04:06 +02:00
|
|
|
import {Injectable} from "@angular/core";
|
|
|
|
import {Decoration} from "../../models/model-interfaces";
|
|
|
|
import {RequestMethod, RequestOptions, URLSearchParams} from "@angular/http";
|
|
|
|
import {Observable} from "rxjs/Observable";
|
|
|
|
import {ADD, DecorationStore, EDIT, LOAD, REMOVE} from "../stores/decoration.store";
|
|
|
|
import {AppConfig} from "../../app.config";
|
|
|
|
import {HttpClient} from "../http-client";
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class DecorationService {
|
|
|
|
|
|
|
|
decorations$: Observable<Decoration[]>;
|
|
|
|
|
|
|
|
constructor(private http: HttpClient,
|
|
|
|
private decorationStore: DecorationStore,
|
|
|
|
private config: AppConfig) {
|
|
|
|
this.decorations$ = decorationStore.items$;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
findDecorations(query = '', fractionFilter?) {
|
|
|
|
const searchParams = new URLSearchParams();
|
|
|
|
searchParams.append('q', query);
|
|
|
|
if (fractionFilter) {
|
|
|
|
searchParams.append('fractFilter', fractionFilter);
|
|
|
|
}
|
|
|
|
|
2017-08-01 23:52:10 +02:00
|
|
|
this.http.get(this.config.apiDecorationPath, searchParams)
|
2018-02-26 09:04:27 +01:00
|
|
|
.map(res => res.json())
|
|
|
|
.do((squads) => {
|
|
|
|
this.decorationStore.dispatch({type: LOAD, data: squads});
|
|
|
|
}).subscribe(_ => {
|
2017-05-10 11:04:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return this.decorations$;
|
|
|
|
}
|
|
|
|
|
|
|
|
getDecoration(id: number | string): Observable<Decoration> {
|
2017-08-01 23:52:10 +02:00
|
|
|
return this.http.get(this.config.apiDecorationPath + id)
|
2018-02-26 09:04:27 +01:00
|
|
|
.map(res => res.json());
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For creating new data with POST or
|
|
|
|
* update existing with patch PATCH
|
|
|
|
*/
|
|
|
|
submitDecoration(decoration: Decoration, imageFile?) {
|
2017-08-01 23:52:10 +02:00
|
|
|
let requestUrl = this.config.apiDecorationPath;
|
2017-05-10 11:04:06 +02:00
|
|
|
let requestMethod: RequestMethod;
|
|
|
|
let accessType;
|
|
|
|
let body;
|
|
|
|
|
|
|
|
if (decoration._id) {
|
|
|
|
requestUrl += decoration._id;
|
|
|
|
requestMethod = RequestMethod.Patch;
|
|
|
|
accessType = EDIT;
|
|
|
|
} else {
|
|
|
|
requestMethod = RequestMethod.Post;
|
|
|
|
accessType = ADD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (imageFile) {
|
|
|
|
body = new FormData();
|
|
|
|
Object.keys(decoration).map((objectKey) => {
|
2017-05-16 20:06:00 +02:00
|
|
|
if (decoration[objectKey] !== undefined) {
|
2017-05-10 11:04:06 +02:00
|
|
|
body.append(objectKey, decoration[objectKey]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
body.append('image', imageFile, imageFile.name);
|
|
|
|
} else {
|
|
|
|
body = decoration;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const options = new RequestOptions({
|
|
|
|
body: body,
|
|
|
|
method: requestMethod,
|
|
|
|
});
|
|
|
|
|
|
|
|
return this.http.request(requestUrl, options)
|
2018-02-26 09:04:27 +01:00
|
|
|
.map(res => res.json())
|
|
|
|
.do(savedDecoration => {
|
|
|
|
const action = {type: accessType, data: savedDecoration};
|
|
|
|
this.decorationStore.dispatch(action);
|
|
|
|
});
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
deleteDecoration(decoration: Decoration) {
|
2017-08-01 23:52:10 +02:00
|
|
|
return this.http.delete(this.config.apiDecorationPath + decoration._id)
|
2018-02-26 09:04:27 +01:00
|
|
|
.do(res => {
|
|
|
|
this.decorationStore.dispatch({type: REMOVE, data: decoration});
|
|
|
|
});
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|