collect shared logic in class for temp faked database

This commit is contained in:
Mads Rasmussen
2022-08-05 10:12:56 +02:00
parent ed5cfa2dbc
commit 7ce5ce6cbe
5 changed files with 47 additions and 76 deletions

View File

@@ -1,3 +1,5 @@
import { UmbData } from './data';
export interface DocumentNode {
id: number;
key: string;
@@ -133,30 +135,9 @@ export const data: Array<DocumentNode> = [
];
// Temp mocked database
class UmbContentData {
private _data: Array<DocumentNode> = [];
class UmbContentData extends UmbData<DocumentNode> {
constructor() {
this._data = data;
}
getById(id: number) {
return this._data.find((item) => item.id === id);
}
save(nodes: DocumentNode[]) {
nodes.forEach((node) => {
const foundIndex = this._data.findIndex((item) => item.id === node.id);
if (foundIndex !== -1) {
// replace
this._data[foundIndex] = node;
} else {
// new
this._data.push(node);
}
});
//console.log('save:', nodes);
return nodes;
super(data);
}
}

View File

@@ -1,3 +1,5 @@
import { UmbData } from './data';
export interface DataTypeEntity {
id: number;
key: string;
@@ -34,34 +36,9 @@ export const data: Array<DataTypeEntity> = [
];
// Temp mocked database
class UmbDataTypeData {
private _data: Array<DataTypeEntity> = [];
class UmbDataTypeData extends UmbData<DataTypeEntity> {
constructor() {
this._data = data;
}
getById(id: number) {
return this._data.find((item) => item.id === id);
}
getByKey(key: string) {
return this._data.find((item) => item.key === key);
}
save(nodes: DataTypeEntity[]) {
nodes.forEach((node) => {
const foundIndex = this._data.findIndex((item) => item.id === node.id);
if (foundIndex !== -1) {
// replace
this._data[foundIndex] = node;
} else {
// new
this._data.push(node);
}
});
//console.log('save:', nodes);
return nodes;
super(data);
}
}

View File

@@ -0,0 +1,31 @@
// Temp mocked database
export class UmbData<T extends { id: number; key: string }> {
private _data: Array<T> = [];
constructor(data: Array<T>) {
this._data = data;
}
getById(id: number) {
return this._data.find((item) => item.id === id);
}
getByKey(key: string) {
return this._data.find((item) => item.key === key);
}
save(data: Array<T>) {
data.forEach((storedItem) => {
const foundIndex = this._data.findIndex((item) => item.id === storedItem.id);
if (foundIndex !== -1) {
// replace
this._data[foundIndex] = storedItem;
} else {
// new
this._data.push(storedItem);
}
});
return data;
}
}

View File

@@ -1,3 +1,5 @@
import { UmbData } from './data';
export interface DocumentTypeEntity {
id: number;
key: string;
@@ -21,29 +23,9 @@ export const data: Array<DocumentTypeEntity> = [
];
// Temp mocked database
class UmbDocumentTypeData {
private _data: Array<DocumentTypeEntity> = [];
class UmbDocumentTypeData extends UmbData<DocumentTypeEntity> {
constructor() {
this._data = data;
}
getById(id: number) {
return this._data.find((item) => item.id === id);
}
save(nodes: DocumentTypeEntity[]) {
nodes.forEach((node) => {
const foundIndex = this._data.findIndex((item) => item.id === node.id);
if (foundIndex !== -1) {
// replace
this._data[foundIndex] = node;
} else {
// new
this._data.push(node);
}
});
return nodes;
super(data);
}
}

View File

@@ -9,18 +9,18 @@ export const handlers = [
if (!id) return;
const int = parseInt(id);
const document = umbDataTypeData.getById(int);
const dataType = umbDataTypeData.getById(int);
return res(ctx.status(200), ctx.json([document]));
return res(ctx.status(200), ctx.json([dataType]));
}),
rest.get('/umbraco/backoffice/data-type/by-key/:key', (req, res, ctx) => {
const key = req.params.key as string;
if (!key) return;
const document = umbDataTypeData.getByKey(key);
const dataType = umbDataTypeData.getByKey(key);
return res(ctx.status(200), ctx.json([document]));
return res(ctx.status(200), ctx.json([dataType]));
}),
rest.post<DataTypeEntity[]>('/umbraco/backoffice/data-type/save', (req, res, ctx) => {