Initial commit
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
/**
|
||||
* Class model for Entity Interface
|
||||
*/
|
||||
import type { EntityInterface } from "@/types/entity";
|
||||
import type { EventInterface } from "@/types/event";
|
||||
import type { TaskInterface } from "@/types/task";
|
||||
import type { JournalInterface } from "@/types/journal";
|
||||
import { EventObject } from "./event";
|
||||
import { TaskObject } from "./task";
|
||||
import { JournalObject } from "./journal";
|
||||
|
||||
export class EntityObject implements EntityInterface {
|
||||
|
||||
_data!: EntityInterface;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'chrono:entity',
|
||||
version: 1,
|
||||
in: null,
|
||||
id: null,
|
||||
createdOn: null,
|
||||
createdBy: null,
|
||||
modifiedOn: null,
|
||||
modifiedBy: null,
|
||||
signature: null,
|
||||
data: null,
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: EntityInterface): EntityObject {
|
||||
this._data = data;
|
||||
if (data.data) {
|
||||
const type = data.data.type;
|
||||
if (type === 'task') {
|
||||
this._data.data = new TaskObject().fromJson(data.data as TaskInterface);
|
||||
} else if (type === 'journal') {
|
||||
this._data.data = new JournalObject().fromJson(data.data as JournalInterface);
|
||||
} else {
|
||||
this._data.data = new EventObject().fromJson(data.data as EventInterface);
|
||||
}
|
||||
} else {
|
||||
this._data.data = null;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): EntityInterface {
|
||||
const json = { ...this._data };
|
||||
if (this._data.data instanceof EventObject ||
|
||||
this._data.data instanceof TaskObject ||
|
||||
this._data.data instanceof JournalObject) {
|
||||
json.data = this._data.data.toJson();
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
clone(): EntityObject {
|
||||
const cloned = new EntityObject();
|
||||
cloned._data = JSON.parse(JSON.stringify(this._data));
|
||||
return cloned;
|
||||
}
|
||||
|
||||
/** Properties */
|
||||
|
||||
get '@type'(): string {
|
||||
return this._data['@type'];
|
||||
}
|
||||
|
||||
get version(): number {
|
||||
return this._data.version;
|
||||
}
|
||||
|
||||
set version(value: number) {
|
||||
this._data.version = value;
|
||||
}
|
||||
|
||||
get in(): string | number | null {
|
||||
return this._data.in;
|
||||
}
|
||||
|
||||
set in(value: string | number | null) {
|
||||
this._data.in = value;
|
||||
}
|
||||
|
||||
get id(): string | number | null {
|
||||
return this._data.id;
|
||||
}
|
||||
|
||||
set id(value: string | number | null) {
|
||||
this._data.id = value;
|
||||
}
|
||||
|
||||
get createdOn(): Date | null {
|
||||
return this._data.createdOn;
|
||||
}
|
||||
|
||||
set createdOn(value: Date | null) {
|
||||
this._data.createdOn = value;
|
||||
}
|
||||
|
||||
get createdBy(): string | null {
|
||||
return this._data.createdBy;
|
||||
}
|
||||
|
||||
set createdBy(value: string | null) {
|
||||
this._data.createdBy = value;
|
||||
}
|
||||
|
||||
get modifiedOn(): Date | null {
|
||||
return this._data.modifiedOn;
|
||||
}
|
||||
|
||||
set modifiedOn(value: Date | null) {
|
||||
this._data.modifiedOn = value;
|
||||
}
|
||||
|
||||
get modifiedBy(): string | null {
|
||||
return this._data.modifiedBy;
|
||||
}
|
||||
|
||||
set modifiedBy(value: string | null) {
|
||||
this._data.modifiedBy = value;
|
||||
}
|
||||
|
||||
get signature(): string | null {
|
||||
return this._data.signature;
|
||||
}
|
||||
|
||||
set signature(value: string | null) {
|
||||
this._data.signature = value;
|
||||
}
|
||||
|
||||
get data(): EventObject | TaskObject | JournalObject | null {
|
||||
if (this._data.data instanceof EventObject ||
|
||||
this._data.data instanceof TaskObject ||
|
||||
this._data.data instanceof JournalObject) {
|
||||
return this._data.data;
|
||||
}
|
||||
|
||||
if (this._data.data) {
|
||||
const type = this._data.data.type;
|
||||
let hydrated;
|
||||
if (type === 'task') {
|
||||
hydrated = new TaskObject().fromJson(this._data.data as TaskInterface);
|
||||
} else if (type === 'journal') {
|
||||
hydrated = new JournalObject().fromJson(this._data.data as JournalInterface);
|
||||
} else {
|
||||
hydrated = new EventObject().fromJson(this._data.data as EventInterface);
|
||||
}
|
||||
this._data.data = hydrated;
|
||||
return hydrated;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
set data(value: EventObject | TaskObject | JournalObject | null) {
|
||||
this._data.data = value;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user