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

60 lines
1.8 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Campaign} from '../../models/model-interfaces';
import {AppConfig} from '../../app.config';
import {ADD, EDIT, LOAD, REMOVE, Store} from '../stores/generic-store';
import {Observable} from 'rxjs';
import {HttpGateway, HttpMethod} from '../http-gateway';
@Injectable()
export class CampaignService {
campaigns$: Observable<Campaign[]>;
campaignStore = new Store<Campaign>();
constructor(private httpGateway: HttpGateway,
private config: AppConfig) {
this.campaigns$ = this.campaignStore.items$;
}
getAllCampaigns() {
return this.httpGateway.get<Campaign[]>(this.config.apiCampaignPath)
.do((ranks) => this.campaignStore.dispatch({type: LOAD, data: ranks}));
}
getCampaign(id: string) {
return this.httpGateway.get(`${this.config.apiCampaignPath}/${id}`);
}
getCampaignByWarId(warId) {
return this.httpGateway.get(`${this.config.apiCampaignPath}/with/war/${warId}`);
}
submitCampaign(campaign: Campaign) {
let requestUrl: string;
let requestMethod: HttpMethod;
let accessType;
if (campaign._id) {
requestUrl = this.config.apiCampaignPath + '/' + campaign._id;
requestMethod = 'PATCH';
accessType = EDIT;
} else {
requestUrl = this.config.apiCampaignPath;
requestMethod = 'POST';
accessType = ADD;
}
return this.httpGateway.request<Campaign>(requestUrl, campaign, requestMethod)
.do(savedCampaign => {
const action = {type: accessType, data: savedCampaign};
this.campaignStore.dispatch(action);
});
}
deleteCampaign(campaign: Campaign) {
return this.httpGateway.delete(this.config.apiCampaignPath + '/' + campaign._id)
.do(res => this.campaignStore.dispatch({type: REMOVE, data: campaign}));
}
}