Created BaseEntry

This commit is contained in:
Marcel Lorbeer 2023-07-03 21:36:02 +02:00
parent de5bb35cb2
commit 5c02b52e93

View File

@ -1,15 +1,11 @@
export class Entry { export class BaseEntry {
private _accessToken: string;
private _collectionName: string;
private _entryId: string;
private _data: EntryData; private _data: EntryData;
private _collectionName: string;
constructor(accessToken: string, collectionName: string, data: EntryData) { constructor(private data: EntryData, private collectionName: string) {
this._accessToken = accessToken;
this._collectionName = collectionName;
this._entryId = data._id;
this._data = data; this._data = data;
this._collectionName = collectionName;
} }
/** /**
@ -38,7 +34,7 @@ export class Entry {
getValues(): EntryData { getValues(): EntryData {
return this._data; return this._data;
} }
/** /**
* *
* @param key - The key of the value to get * @param key - The key of the value to get
@ -66,7 +62,7 @@ export class Entry {
getValueAs<T>(key: string): T { getValueAs<T>(key: string): T {
return this._data[key]; return this._data[key];
} }
/** /**
* *
* @param key - The key of the value to get * @param key - The key of the value to get
@ -95,6 +91,31 @@ export class Entry {
return this._data[key]; return this._data[key];
} }
/**
*
* @returns The name of the collection of the entry
*
*/
getCollectionName(): string {
return this._collectionName;
}
protected _setData(data: EntryData) {
this._data = data;
}
}
export class Entry extends BaseEntry {
private _accessToken: string;
private _entryId: string;
constructor(accessToken: string, collectionName: string, data: EntryData) {
super(data, collectionName);
this._accessToken = accessToken;
this._entryId = data._id;
}
/** /**
* *
* @param key - The key of the value to update * @param key - The key of the value to update
@ -120,7 +141,7 @@ export class Entry {
*/ */
async updateValue(key: string, value: any): Promise<EntryData> { async updateValue(key: string, value: any): Promise<EntryData> {
try { try {
const result = await fetch(`https://api.marcsync.dev/v1/entries/${this._collectionName}`, { const result = await fetch(`https://api.marcsync.dev/v1/entries/${this.getCollectionName()}`, {
method: "PUT", method: "PUT",
headers: { headers: {
authorization: this._accessToken, authorization: this._accessToken,
@ -140,8 +161,10 @@ export class Entry {
} catch (err) { } catch (err) {
throw new EntryUpdateFailed(err); throw new EntryUpdateFailed(err);
} }
this._data[key] = value; const data = this.getValues();
return this._data; data[key] = value;
this._setData(data);
return data;
} }
/** /**
@ -172,7 +195,7 @@ export class Entry {
*/ */
async updateValues(values: EntryData): Promise<EntryData> { async updateValues(values: EntryData): Promise<EntryData> {
try { try {
const result = await fetch(`https://api.marcsync.dev/v1/entries/${this._collectionName}`, { const result = await fetch(`https://api.marcsync.dev/v1/entries/${this.getCollectionName()}`, {
method: "PUT", method: "PUT",
headers: { headers: {
authorization: this._accessToken, authorization: this._accessToken,
@ -190,10 +213,12 @@ export class Entry {
} catch (err) { } catch (err) {
throw new EntryUpdateFailed(err); throw new EntryUpdateFailed(err);
} }
const data = this.getValues();
for (const key in values) { for (const key in values) {
this._data[key] = values[key]; data[key] = values[key];
} }
return this._data; this._setData(data);
return data;
} }
/** /**
@ -203,7 +228,7 @@ export class Entry {
*/ */
async delete(): Promise<void> { async delete(): Promise<void> {
try { try {
const result = await fetch(`https://api.marcsync.dev/v1/entries/${this._collectionName}`, { const result = await fetch(`https://api.marcsync.dev/v1/entries/${this.getCollectionName()}`, {
method: "DELETE", method: "DELETE",
headers: { headers: {
authorization: this._accessToken, authorization: this._accessToken,