From aec096b2ecf8d5f0497c800d72f91b520939392a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petar=20Mili=C4=8Di=C4=87?= Date: Thu, 9 Dec 2021 15:23:59 +0100 Subject: [PATCH] WebApi data create and update 2sic/2sxc#2619 --- .../$2sxc/src/sxc-instance/data/sxc-data.ts | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/projects/$2sxc/src/sxc-instance/data/sxc-data.ts b/projects/$2sxc/src/sxc-instance/data/sxc-data.ts index 0eb4ef0d9..ac7bd4312 100644 --- a/projects/$2sxc/src/sxc-instance/data/sxc-data.ts +++ b/projects/$2sxc/src/sxc-instance/data/sxc-data.ts @@ -14,14 +14,14 @@ export class SxcData extends SxcDataQueryBase { constructor(sxc: SxcInstance, readonly name: string) { super(sxc, name, 'ContentType'); } - + /** * Get all items of this type. */ getAll(): Promise { return this.getInternal(); } - + /** * Get the specific item with the ID. It will return null if not found */ @@ -33,8 +33,8 @@ export class SxcData extends SxcDataQueryBase { private getMany(criteria: Record, fields: Array): Promise { throw 'not implemented - probably v13.5 or something'; } - - + + /** * Get all or one data entity from the backend * @param id optional id as number or string - if not provided, will get all @@ -46,22 +46,30 @@ export class SxcData extends SxcDataQueryBase { if (id && (typeof id === 'string' || typeof id === 'number')) path += "/" + id; return this.webApi.fetchJson(this.webApi.url(path, params)); } - - // TODO: @SPM create - // - Create a type for the `metadataFor` attribute - // - implement create - // - capture various null-cases - // - if `metadataFor` is specified, add attribute `for` the the object before sending (that should work) create(values: Record): Promise>; + create(values: Record, metadataFor: MetadataFor): Promise>; - create(values: Record, metadataFor?: any): Promise> { - return null; + create(values: Record, metadataFor?: MetadataFor): Promise> { + const path = `app/auto/data/${this.name}`; + if (metadataFor != null) { + try { + values.For = metadataFor; + } catch { } + } + return this.webApi.fetchJson(this.webApi.url(path), values, 'POST'); } - - // TODO: @SPM update + update(id: number, values: Record): Promise> { - return null; + const path = `app/auto/data/${this.name}/${id}`; + return this.webApi.fetchJson(this.webApi.url(path), values, 'POST'); } } - \ No newline at end of file + +export interface MetadataFor { + Target: string; + Number?: number; + String?: string; + Guid?: string; + Singleton?: boolean; +}