OpenSlides/client/src/app/core/services/data-send.service.ts

54 lines
1.6 KiB
TypeScript
Raw Normal View History

2018-08-22 16:03:49 +02:00
import { Injectable } from '@angular/core';
import { BaseModel } from '../../shared/models/base/base-model';
2018-08-22 16:03:49 +02:00
import { Observable } from 'rxjs';
import { HttpService } from './http.service';
import { HTTPMethod } from './http.service';
2018-08-22 16:03:49 +02:00
/**
* Send data back to server
*
* Contrast to dataStore service
*/
@Injectable({
providedIn: 'root'
})
export class DataSendService {
/**
* Construct a DataSendService
*
* @param httpService The HTTP Client
2018-08-22 16:03:49 +02:00
*/
public constructor(private httpService: HttpService) {}
2018-08-22 16:03:49 +02:00
/**
2018-09-18 18:27:14 +02:00
* Sends a post request with the model to the server.
* Usually for new Models
*/
public createModel(model: BaseModel): Observable<BaseModel> {
return this.httpService.create('rest/' + model.collectionString + '/', model) as Observable<BaseModel>;
2018-09-18 18:27:14 +02:00
}
/**
* Function to change a model on the server.
2018-08-22 16:03:49 +02:00
*
2018-09-18 18:27:14 +02:00
* @param model the base model that is meant to be changed
* @param method the required http method. might be put or patch
2018-08-22 16:03:49 +02:00
*/
public updateModel(model: BaseModel, method: HTTPMethod): Observable<BaseModel> {
2018-09-18 18:27:14 +02:00
const restPath = `rest/${model.collectionString}/${model.id}`;
return this.httpService.update(restPath + '/' + model.id, model, method) as Observable<BaseModel>;
2018-08-22 16:03:49 +02:00
}
/**
* Deletes the given model on the server
*
* @param model the BaseModel that shall be removed
* @return Observable of BaseModel
*
* TODO Not tested
*/
public deleteModel(model: BaseModel): Observable<BaseModel> {
return this.httpService.delete(model.collectionString + '/' + model.id) as Observable<BaseModel>;
2018-08-22 16:03:49 +02:00
}
}