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

62 lines
1.8 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';
import { HttpService } from './http.service';
2018-10-26 11:19:05 +02:00
import { Identifiable } from '../../shared/models/base/identifiable';
2018-08-22 16:03:49 +02:00
/**
2018-10-26 11:19:05 +02:00
* Send data back to server. Cares about the right REST routes.
2018-08-22 16:03:49 +02:00
*
* Contrast to dataStore service
*/
@Injectable({
providedIn: 'root'
})
export class DataSendService {
/**
* Construct a DataSendService
*
2018-10-26 11:19:05 +02:00
* @param httpService The HTTP Service
2018-08-22 16:03:49 +02:00
*/
public constructor(private httpService: HttpService) {}
2018-08-22 16:03:49 +02:00
/**
2018-10-26 11:19:05 +02:00
* Sends a post request with the model to the server to create it.
*
* @param model The model to create.
2018-09-18 18:27:14 +02:00
*/
2018-10-26 11:19:05 +02:00
public async createModel(model: BaseModel): Promise<Identifiable> {
const restPath = `rest/${model.collectionString}/`;
2018-10-26 11:19:05 +02:00
return await this.httpService.post<Identifiable>(restPath, model);
}
/**
* Function to fully update a model on the server.
*
* @param model The model that is meant to be changed.
*/
public async updateModel(model: BaseModel): Promise<void> {
const restPath = `rest/${model.collectionString}/${model.id}/`;
await this.httpService.put(restPath, model);
2018-09-18 18:27:14 +02:00
}
/**
2018-10-26 11:19:05 +02:00
* Updates a model partially on the server.
2018-08-22 16:03:49 +02:00
*
2018-10-26 11:19:05 +02:00
* @param model The model to partially update.
2018-08-22 16:03:49 +02:00
*/
2018-10-26 11:19:05 +02:00
public async partialUpdateModel(model: BaseModel): Promise<void> {
const restPath = `rest/${model.collectionString}/${model.id}/`;
await this.httpService.patch(restPath, model);
2018-08-22 16:03:49 +02:00
}
/**
2018-10-26 11:19:05 +02:00
* Deletes the given model on the server.
2018-08-22 16:03:49 +02:00
*
2018-10-26 11:19:05 +02:00
* @param model the model that shall be deleted.
2018-08-22 16:03:49 +02:00
*/
2018-10-26 11:19:05 +02:00
public async deleteModel(model: BaseModel): Promise<void> {
const restPath = `rest/${model.collectionString}/${model.id}/`;
await this.httpService.delete(restPath);
2018-08-22 16:03:49 +02:00
}
}