opt-cc/static/src/app/services/logs/campaign.service.ts

70 lines
2.0 KiB
TypeScript
Raw Normal View History

2018-03-07 11:56:50 +01:00
import {Injectable} from '@angular/core';
import {Campaign} from '../../models/model-interfaces';
import {AppConfig} from '../../app.config';
import {HttpClient} from '../http-client';
import {ADD, EDIT, LOAD, REMOVE, Store} from '../stores/generic-store';
import {Observable} from 'rxjs';
import {RequestMethod, RequestOptions} from '@angular/http';
2017-09-12 15:02:35 +02:00
@Injectable()
export class CampaignService {
campaigns$: Observable<Campaign[]>;
campaignStore = new Store<Campaign>();
2017-09-12 15:02:35 +02:00
constructor(private http: HttpClient,
private config: AppConfig) {
this.campaigns$ = this.campaignStore.items$;
2017-09-12 15:02:35 +02:00
}
getAllCampaigns() {
return this.http.get(this.config.apiCampaignPath)
.map(res => res.json());
}
getAllCampaignsWithWars() {
2018-10-08 10:53:22 +02:00
return this.http.get(this.config.apiWarPath)
.map(res => res.json())
2018-10-08 10:53:22 +02:00
.do((ranks) => this.campaignStore.dispatch({type: LOAD, data: ranks}))
}
getCampaign(id: string) {
return this.http.get(this.config.apiCampaignPath + '/' + id)
.map(res => res.json());
2017-09-12 15:02:35 +02:00
}
submitCampaign(campaign: Campaign) {
let requestUrl: string;
let requestMethod: RequestMethod
let accessType;
2018-04-28 10:44:52 +02:00
if (campaign._id) {
requestUrl = this.config.apiCampaignPath + '/' + campaign._id;
requestMethod = RequestMethod.Patch;
accessType = EDIT;
2018-04-28 10:44:52 +02:00
} else {
requestUrl = this.config.apiCampaignPath;
requestMethod = RequestMethod.Post;
accessType = ADD;
2018-04-28 10:44:52 +02:00
}
const options = new RequestOptions({
body: campaign,
method: requestMethod
});
return this.http.request(requestUrl, options)
.map(res => res.json())
.do(savedCampaign => {
const action = {type: accessType, data: savedCampaign};
this.campaignStore.dispatch(action);
});
2017-09-12 15:02:35 +02:00
}
deleteCampaign(campaign: Campaign) {
return this.http.delete(this.config.apiCampaignPath + '/' + campaign._id)
2018-10-08 10:53:22 +02:00
.do(res => this.campaignStore.dispatch({type: REMOVE, data: campaign}));
2018-04-28 10:44:52 +02:00
}
2017-09-12 15:02:35 +02:00
}