update script detail data source to match new end points

This commit is contained in:
Mads Rasmussen
2024-01-08 15:44:19 +01:00
parent 0826bbb0c3
commit d369f21bd8

View File

@@ -4,7 +4,7 @@ import { UmbServerPathUniqueSerializer, appendFileExtensionIfNeeded } from '../.
import {
CreateScriptRequestModel,
ScriptResource,
UpdateTextFileViewModelBaseModel,
UpdateScriptRequestModel,
} from '@umbraco-cms/backoffice/backend-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UmbDetailDataSource } from '@umbraco-cms/backoffice/repository';
@@ -63,19 +63,18 @@ export class UmbScriptDetailServerDataSource implements UmbDetailDataSource<UmbS
async read(unique: string) {
if (!unique) throw new Error('Unique is missing');
const serverPath = this.#serverPathUniqueSerializer.toServerPath(unique);
const path = this.#serverPathUniqueSerializer.toServerPath(unique);
const { data, error } = await tryExecuteAndNotify(this.#host, ScriptResource.getScript({ path: serverPath }));
const { data, error } = await tryExecuteAndNotify(this.#host, ScriptResource.getScriptByPath({ path }));
if (error || !data) {
return { error };
}
// TODO: make data mapper to prevent errors
const script: UmbScriptDetailModel = {
entityType: UMB_SCRIPT_ENTITY_TYPE,
unique: this.#serverPathUniqueSerializer.toUnique(data.path),
parentUnique: this.#serverPathUniqueSerializer.toParentUnique(data.path),
parentUnique: data.parent ? this.#serverPathUniqueSerializer.toUnique(data.parent.path) : null,
path: data.path,
name: data.name,
content: data.content,
@@ -87,18 +86,16 @@ export class UmbScriptDetailServerDataSource implements UmbDetailDataSource<UmbS
async update(data: UmbScriptDetailModel) {
if (!data.unique) throw new Error('Unique is missing');
const existingPath = this.#serverPathUniqueSerializer.toServerPath(data.unique);
const path = this.#serverPathUniqueSerializer.toServerPath(data.unique);
// TODO: make data mapper to prevent errors
const requestBody: UpdateTextFileViewModelBaseModel = {
existingPath,
name: data.name,
const requestBody: UpdateScriptRequestModel = {
content: data.content,
};
const { error } = await tryExecuteAndNotify(
this.#host,
ScriptResource.putScript({
ScriptResource.putScriptByPath({
path,
requestBody,
}),
);
@@ -107,12 +104,7 @@ export class UmbScriptDetailServerDataSource implements UmbDetailDataSource<UmbS
return { error };
}
// TODO: should we get this as part of the PUT response?
const parentPath = this.#serverPathUniqueSerializer.toServerPath(data.parentUnique);
const newFilePath = parentPath + '/' + requestBody.name;
const newPathUnique = this.#serverPathUniqueSerializer.toUnique(newFilePath);
// We have to fetch the data type again. The server can have modified the data after update
return this.read(newPathUnique);
return this.read(data.unique);
}
async delete(unique: string) {
@@ -122,7 +114,7 @@ export class UmbScriptDetailServerDataSource implements UmbDetailDataSource<UmbS
return tryExecuteAndNotify(
this.#host,
ScriptResource.deleteScript({
ScriptResource.deleteScriptByPath({
path,
}),
);