| import { DocumentProvider } from '../../Core/ViewModel/DocumentProvider'; |
| import { Document } from '../../Core/Model/Document'; |
| |
| import { RequestService } from '@ud-viz/utils_browser'; |
| |
| |
| |
| |
| |
| export class ContributeService { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| constructor(requestService, provider, configServer) { |
| |
| |
| |
| |
| |
| this.requestService = requestService; |
| |
| |
| |
| |
| |
| |
| this.provider = provider; |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| |
| |
| this.configServer = configServer; |
| |
| |
| |
| |
| |
| |
| this.documentUrl = this.configServer.url; |
| if (!this.documentUrl.endsWith('/')) { |
| this.documentUrl += '/'; |
| } |
| this.documentUrl += this.configServer.document; |
| } |
| |
| |
| |
| |
| |
| |
| |
| async updateDocument(updatedData) { |
| |
| const currentDoc = this.provider.getDisplayedDocument(); |
| const id = currentDoc.id; |
| |
| const url = this.documentUrl + '/' + id; |
| |
| const response = await this.requestService.request('PUT', url, { |
| body: updatedData, |
| }); |
| |
| if (response.status >= 200 && response.status < 300) { |
| const updated = JSON.parse(response.responseText); |
| await this.provider.refreshDocumentList(); |
| this.provider.setDisplayedDocument(updated); |
| return updated; |
| } |
| throw response.statusText; |
| } |
| |
| |
| |
| |
| |
| |
| |
| async createDocument(creationData) { |
| const response = await this.requestService.request( |
| 'POST', |
| this.documentUrl, |
| { |
| body: creationData, |
| } |
| ); |
| |
| if (response.status >= 200 && response.status < 300) { |
| const created = JSON.parse(response.responseText); |
| await this.provider.refreshDocumentList(); |
| return created; |
| } |
| throw response.statusText; |
| } |
| |
| |
| |
| |
| async deleteDocument() { |
| const currentDoc = this.provider.getDisplayedDocument(); |
| const id = currentDoc.id; |
| |
| const url = this.documentUrl + '/' + id; |
| |
| const response = await this.requestService.request('DELETE', url); |
| |
| if (response.status >= 200 && response.status < 300) { |
| await this.provider.refreshDocumentList(); |
| return; |
| } |
| throw response.statusText; |
| } |
| } |