Initial commit
This commit is contained in:
191
src/models/collection.ts
Normal file
191
src/models/collection.ts
Normal file
@@ -0,0 +1,191 @@
|
||||
/**
|
||||
* Class model for Collection Interface
|
||||
*/
|
||||
|
||||
import type { CollectionInterface, CollectionPropertiesInterface } from "@/types/collection";
|
||||
|
||||
export class CollectionObject implements CollectionInterface {
|
||||
|
||||
_data!: CollectionInterface;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
provider: '',
|
||||
service: '',
|
||||
collection: null,
|
||||
identifier: '',
|
||||
signature: null,
|
||||
created: null,
|
||||
modified: null,
|
||||
properties: {
|
||||
'@type': 'mail.collection',
|
||||
version: 1,
|
||||
total: 0,
|
||||
unread: 0,
|
||||
label: '',
|
||||
role: null,
|
||||
rank: 0,
|
||||
subscribed: true,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: CollectionInterface): CollectionObject {
|
||||
this._data = data;
|
||||
if (data.properties) {
|
||||
this._data.properties = new CollectionPropertiesObject().fromJson(data.properties as CollectionPropertiesInterface);
|
||||
} else {
|
||||
this._data.properties = new CollectionPropertiesObject();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): CollectionInterface {
|
||||
const json = { ...this._data };
|
||||
if (this._data.properties instanceof CollectionPropertiesObject) {
|
||||
json.properties = this._data.properties.toJson();
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
clone(): CollectionObject {
|
||||
const cloned = new CollectionObject();
|
||||
cloned._data = { ...this._data };
|
||||
cloned._data.properties = this.properties.clone();
|
||||
return cloned;
|
||||
}
|
||||
|
||||
/** Immutable Properties */
|
||||
|
||||
get provider(): string {
|
||||
return this._data.provider;
|
||||
}
|
||||
|
||||
get service(): string | number {
|
||||
return this._data.service;
|
||||
}
|
||||
|
||||
get collection(): string | number | null {
|
||||
return this._data.collection;
|
||||
}
|
||||
|
||||
get identifier(): string | number {
|
||||
return this._data.identifier;
|
||||
}
|
||||
|
||||
get signature(): string | null | undefined {
|
||||
return this._data.signature;
|
||||
}
|
||||
|
||||
get created(): string | null | undefined {
|
||||
return this._data.created;
|
||||
}
|
||||
|
||||
get modified(): string | null | undefined {
|
||||
return this._data.modified;
|
||||
}
|
||||
|
||||
get properties(): CollectionPropertiesObject {
|
||||
if (this._data.properties instanceof CollectionPropertiesObject) {
|
||||
return this._data.properties;
|
||||
}
|
||||
|
||||
if (this._data.properties) {
|
||||
const hydrated = new CollectionPropertiesObject().fromJson(this._data.properties as CollectionPropertiesInterface);
|
||||
this._data.properties = hydrated;
|
||||
return hydrated;
|
||||
}
|
||||
|
||||
return new CollectionPropertiesObject();
|
||||
}
|
||||
|
||||
set properties(value: CollectionPropertiesObject) {
|
||||
if (value instanceof CollectionPropertiesObject) {
|
||||
this._data.properties = value as any;
|
||||
} else {
|
||||
this._data.properties = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class CollectionPropertiesObject implements CollectionPropertiesInterface {
|
||||
|
||||
_data!: CollectionPropertiesInterface;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'mail.collection',
|
||||
version: 1,
|
||||
total: 0,
|
||||
unread: 0,
|
||||
label: '',
|
||||
role: null,
|
||||
rank: 0,
|
||||
subscribed: true,
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: CollectionPropertiesInterface): CollectionPropertiesObject {
|
||||
this._data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): CollectionPropertiesInterface {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
clone(): CollectionPropertiesObject {
|
||||
const cloned = new CollectionPropertiesObject();
|
||||
cloned._data = { ...this._data };
|
||||
return cloned;
|
||||
}
|
||||
|
||||
/** Immutable Properties */
|
||||
|
||||
get '@type'(): string {
|
||||
return this._data['@type'];
|
||||
}
|
||||
|
||||
get version(): number {
|
||||
return this._data.version;
|
||||
}
|
||||
|
||||
get role(): string | null | undefined {
|
||||
return this._data.role;
|
||||
}
|
||||
|
||||
get total(): number | undefined {
|
||||
return this._data.total;
|
||||
}
|
||||
|
||||
get unread(): number | undefined {
|
||||
return this._data.unread;
|
||||
}
|
||||
|
||||
/** Mutable Properties */
|
||||
|
||||
get label(): string {
|
||||
return this._data.label || '';
|
||||
}
|
||||
|
||||
set label(value: string) {
|
||||
this._data.label = value;
|
||||
}
|
||||
|
||||
get rank(): number | undefined {
|
||||
return this._data.rank;
|
||||
}
|
||||
|
||||
set rank(value: number) {
|
||||
this._data.rank = value;
|
||||
}
|
||||
|
||||
get subscribed(): boolean | undefined {
|
||||
return this._data.subscribed;
|
||||
}
|
||||
|
||||
set subscribed(value: boolean) {
|
||||
this._data.subscribed = value;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user