2018-08-22 16:03:49 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { HttpClient } from '@angular/common/http';
|
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';
|
|
|
|
import { tap } from 'rxjs/operators';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send data back to server
|
|
|
|
*
|
|
|
|
* Contrast to dataStore service
|
|
|
|
*/
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class DataSendService {
|
|
|
|
/**
|
|
|
|
* Construct a DataSendService
|
|
|
|
*
|
|
|
|
* @param http The HTTP Client
|
|
|
|
*/
|
2018-08-29 13:21:25 +02:00
|
|
|
public constructor(private http: HttpClient) {}
|
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.http.post<BaseModel>('rest/' + model.collectionString + '/', model).pipe(
|
|
|
|
tap(
|
|
|
|
response => {
|
|
|
|
// TODO: Message, Notify, Etc
|
|
|
|
console.log('New Model added. Response :\n', response);
|
|
|
|
},
|
|
|
|
error => console.error('createModel has returned an Error:\n', error)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-09-18 18:27:14 +02:00
|
|
|
public updateModel(model: BaseModel, method: 'put' | 'patch'): Observable<BaseModel> {
|
|
|
|
const restPath = `rest/${model.collectionString}/${model.id}`;
|
|
|
|
let httpMethod;
|
|
|
|
|
|
|
|
if (method === 'patch') {
|
|
|
|
httpMethod = this.http.patch<BaseModel>(restPath, model);
|
|
|
|
} else if (method === 'put') {
|
|
|
|
httpMethod = this.http.put<BaseModel>(restPath, model);
|
2018-08-22 16:03:49 +02:00
|
|
|
}
|
2018-09-18 18:27:14 +02:00
|
|
|
|
|
|
|
return httpMethod.pipe(
|
|
|
|
tap(
|
|
|
|
response => {
|
|
|
|
// TODO: Message, Notify, Etc
|
|
|
|
console.log('Update model. Response :\n', response);
|
|
|
|
},
|
|
|
|
error => console.error('updateModel has returned an Error:\n', error)
|
|
|
|
)
|
|
|
|
);
|
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
|
|
|
|
*/
|
2018-08-29 13:21:25 +02:00
|
|
|
public delete(model: BaseModel): Observable<BaseModel> {
|
2018-08-22 16:03:49 +02:00
|
|
|
if (model.id) {
|
|
|
|
return this.http.delete<BaseModel>('rest/' + model.collectionString + '/' + model.id).pipe(
|
|
|
|
tap(
|
|
|
|
response => {
|
|
|
|
// TODO: Message, Notify, Etc
|
|
|
|
console.log('the response: ', response);
|
|
|
|
},
|
|
|
|
error => console.error('error during delete: ', error)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
console.error('No model ID to delete');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|