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

75 lines
2.2 KiB
TypeScript
Raw Normal View History

2018-08-22 16:03:49 +02:00
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
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
/**
* Save motion in the server
*
* @return Observable from
*/
2018-08-29 13:21:25 +02:00
public saveModel(model: BaseModel): Observable<BaseModel> {
2018-08-22 16:03:49 +02:00
if (!model.id) {
return this.http.post<BaseModel>('rest/' + model.collectionString + '/', model).pipe(
tap(
response => {
// TODO: Message, Notify, Etc
console.log('New Model added. Response : ', response);
},
error => console.log('error. ', error)
)
);
} else {
return this.http.patch<BaseModel>('rest/' + model.collectionString + '/' + model.id, model).pipe(
2018-08-22 16:03:49 +02:00
tap(
response => {
console.log('Update model. Response : ', response);
},
error => console.log('error. ', error)
)
);
}
}
/**
* 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');
}
}
}