2018-08-22 16:03:49 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
2018-09-13 09:23:57 +02:00
|
|
|
import { BaseModel } from '../../shared/models/base/base-model';
|
2018-08-22 16:03:49 +02:00
|
|
|
import { Observable } from 'rxjs';
|
2018-10-15 11:52:57 +02:00
|
|
|
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
|
|
|
|
*
|
2018-10-15 11:52:57 +02:00
|
|
|
* @param httpService The HTTP Client
|
2018-08-22 16:03:49 +02:00
|
|
|
*/
|
2018-10-15 11:52:57 +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> {
|
2018-10-19 17:27:41 +02:00
|
|
|
const restPath = `rest/${model.collectionString}/`;
|
|
|
|
return this.httpService.create(restPath, 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
|
|
|
*/
|
2018-10-15 11:52:57 +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}`;
|
2018-10-19 17:27:41 +02:00
|
|
|
return this.httpService.update(restPath, 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
|
|
|
|
*/
|
2018-10-15 11:52:57 +02:00
|
|
|
public deleteModel(model: BaseModel): Observable<BaseModel> {
|
2018-10-19 17:27:41 +02:00
|
|
|
const restPath = `rest/${model.collectionString}/${model.id}`;
|
|
|
|
return this.httpService.delete(restPath) as Observable<BaseModel>;
|
2018-08-22 16:03:49 +02:00
|
|
|
}
|
|
|
|
}
|