Implement lastUpdated timestamp
This commit is contained in:
@@ -35,6 +35,7 @@ await dbClient.execute(`
|
|||||||
ownerId varchar(20) NOT NULL,
|
ownerId varchar(20) NOT NULL,
|
||||||
name varchar(200) NOT NULL,
|
name varchar(200) NOT NULL,
|
||||||
folder varchar(200) NOT NULL,
|
folder varchar(200) NOT NULL,
|
||||||
|
lastUpdated timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
data longtext NOT NULL,
|
data longtext NOT NULL,
|
||||||
deleted tinyint(1) NOT NULL DEFAULT 0,
|
deleted tinyint(1) NOT NULL DEFAULT 0,
|
||||||
PRIMARY KEY (id),
|
PRIMARY KEY (id),
|
||||||
|
|||||||
12
mod.ts
12
mod.ts
@@ -67,7 +67,7 @@ Deno.serve({ port: config.api.port }, async (req) => {
|
|||||||
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't read DB.");
|
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't read DB.");
|
||||||
if (!userMatch.length) return genericResponse(STATUS_CODE.NotFound, 'User ID does not exist.');
|
if (!userMatch.length) return genericResponse(STATUS_CODE.NotFound, 'User ID does not exist.');
|
||||||
|
|
||||||
const plans = await dbClient.query('SELECT id, name, folder FROM plans WHERE ownerId = ? AND deleted = 0', [userId]).catch(() => {
|
const plans = await dbClient.query('SELECT id, name, folder, lastUpdated FROM plans WHERE ownerId = ? AND deleted = 0', [userId]).catch(() => {
|
||||||
failed = true;
|
failed = true;
|
||||||
});
|
});
|
||||||
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't read DB.");
|
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't read DB.");
|
||||||
@@ -153,7 +153,7 @@ Deno.serve({ port: config.api.port }, async (req) => {
|
|||||||
if (!planMatch.length) return genericResponse(STATUS_CODE.NotFound, 'Plan ID does not exist.');
|
if (!planMatch.length) return genericResponse(STATUS_CODE.NotFound, 'Plan ID does not exist.');
|
||||||
if (planMatch[0].ownerId !== id) return genericResponse(STATUS_CODE.Forbidden, "You don't own this plan.");
|
if (planMatch[0].ownerId !== id) return genericResponse(STATUS_CODE.Forbidden, "You don't own this plan.");
|
||||||
|
|
||||||
await dbClient.execute('UPDATE plans SET deleted = 0 WHERE id = ?', [planId]).catch(() => {
|
await dbClient.execute('UPDATE plans SET deleted = 0, lastUpdated = ? WHERE id = ?', [new Date(), planId]).catch(() => {
|
||||||
failed = true;
|
failed = true;
|
||||||
});
|
});
|
||||||
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't update DB.");
|
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't update DB.");
|
||||||
@@ -167,7 +167,7 @@ Deno.serve({ port: config.api.port }, async (req) => {
|
|||||||
if (!planMatch.length) return genericResponse(STATUS_CODE.NotFound, 'Plan ID does not exist.');
|
if (!planMatch.length) return genericResponse(STATUS_CODE.NotFound, 'Plan ID does not exist.');
|
||||||
if (planMatch[0].ownerId !== id) return genericResponse(STATUS_CODE.Forbidden, "You don't own this plan.");
|
if (planMatch[0].ownerId !== id) return genericResponse(STATUS_CODE.Forbidden, "You don't own this plan.");
|
||||||
|
|
||||||
await dbClient.execute('UPDATE plans SET data = ? WHERE id = ?', [body.data, planId]).catch(() => {
|
await dbClient.execute('UPDATE plans SET data = ?, lastUpdated = ? WHERE id = ?', [body.data, new Date(), planId]).catch(() => {
|
||||||
failed = true;
|
failed = true;
|
||||||
});
|
});
|
||||||
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't update DB.");
|
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't update DB.");
|
||||||
@@ -183,7 +183,7 @@ Deno.serve({ port: config.api.port }, async (req) => {
|
|||||||
if (!planMatch.length) return genericResponse(STATUS_CODE.NotFound, 'Plan ID does not exist.');
|
if (!planMatch.length) return genericResponse(STATUS_CODE.NotFound, 'Plan ID does not exist.');
|
||||||
if (planMatch[0].ownerId !== id) return genericResponse(STATUS_CODE.Forbidden, "You don't own this plan.");
|
if (planMatch[0].ownerId !== id) return genericResponse(STATUS_CODE.Forbidden, "You don't own this plan.");
|
||||||
|
|
||||||
await dbClient.execute('UPDATE plans SET name = ? WHERE id = ?', [body.planName, planId]).catch(() => {
|
await dbClient.execute('UPDATE plans SET name = ?, lastUpdated = ? WHERE id = ?', [body.planName, new Date(), planId]).catch(() => {
|
||||||
failed = true;
|
failed = true;
|
||||||
});
|
});
|
||||||
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't update DB.");
|
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't update DB.");
|
||||||
@@ -199,7 +199,7 @@ Deno.serve({ port: config.api.port }, async (req) => {
|
|||||||
if (!planMatch.length) return genericResponse(STATUS_CODE.NotFound, 'Plan ID does not exist.');
|
if (!planMatch.length) return genericResponse(STATUS_CODE.NotFound, 'Plan ID does not exist.');
|
||||||
if (planMatch[0].ownerId !== id) return genericResponse(STATUS_CODE.Forbidden, "You don't own this plan.");
|
if (planMatch[0].ownerId !== id) return genericResponse(STATUS_CODE.Forbidden, "You don't own this plan.");
|
||||||
|
|
||||||
await dbClient.execute('UPDATE plans SET folder = ? WHERE id = ?', [body.folder, planId]).catch(() => {
|
await dbClient.execute('UPDATE plans SET folder = ?, lastUpdated = ? WHERE id = ?', [body.folder, new Date(), planId]).catch(() => {
|
||||||
failed = true;
|
failed = true;
|
||||||
});
|
});
|
||||||
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't update DB.");
|
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't update DB.");
|
||||||
@@ -280,7 +280,7 @@ Deno.serve({ port: config.api.port }, async (req) => {
|
|||||||
if (!planMatch.length) return genericResponse(STATUS_CODE.NotFound, 'Plan ID does not exist.');
|
if (!planMatch.length) return genericResponse(STATUS_CODE.NotFound, 'Plan ID does not exist.');
|
||||||
if (planMatch[0].ownerId !== id) return genericResponse(STATUS_CODE.Forbidden, "You don't own this plan.");
|
if (planMatch[0].ownerId !== id) return genericResponse(STATUS_CODE.Forbidden, "You don't own this plan.");
|
||||||
|
|
||||||
await dbClient.execute('UPDATE plans SET deleted = 1 WHERE id = ?', [planId]).catch(() => {
|
await dbClient.execute('UPDATE plans SET deleted = 1, lastUpdated = ? WHERE id = ?', [new Date(), planId]).catch(() => {
|
||||||
failed = true;
|
failed = true;
|
||||||
});
|
});
|
||||||
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't update DB.");
|
if (failed) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't update DB.");
|
||||||
|
|||||||
@@ -6,27 +6,34 @@ interface Plan {
|
|||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
folder: string;
|
folder: string;
|
||||||
|
lastUpdated: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const zeroPad = (s: number) => s.toString().padStart(2, '0');
|
||||||
|
|
||||||
|
const formatDateTime = (d: Date) =>
|
||||||
|
`${d.getFullYear()}/${zeroPad(d.getMonth() + 1)}/${zeroPad(d.getDate())} ${d.getHours() === 12 ? 12 : d.getHours() % 12}:${zeroPad(d.getMinutes())} ${d.getHours() >= 12 ? 'PM' : 'AM'}`;
|
||||||
|
|
||||||
const makePlanButtons = (planId: string, deleted: boolean) =>
|
const makePlanButtons = (planId: string, deleted: boolean) =>
|
||||||
deleted
|
deleted
|
||||||
? `<button onclick="doAction('undelete','${planId}')">restore</button><button onclick="doAction('perm-delete','${planId}')">perm delete</button>`
|
? `<button onclick="doAction('undelete','${planId}')">restore</button><button onclick="doAction('perm-delete','${planId}')">perm delete</button>`
|
||||||
: `<button onclick="openPlan('${planId}')">open</button><button onclick="sharePlan('${planId}')">share</button><button onclick="doAction('rename','${planId}')">rename</button><button onclick="doAction('move','${planId}')">move</button><button onclick="doAction('delete','${planId}')">delete</button>`;
|
: `<button onclick="openPlan('${planId}')">open</button><button onclick="sharePlan('${planId}')">share</button><button onclick="doAction('rename','${planId}')">rename</button><button onclick="doAction('move','${planId}')">move</button><button onclick="doAction('delete','${planId}')">delete</button>`;
|
||||||
|
|
||||||
const makePlanItem = (plan: Plan, deleted: boolean) => `<li>${plan.folder}${plan.folder && '/'}${plan.name} - ${makePlanButtons(plan.id, deleted)}</li>`;
|
const makePlanItem = (plan: Plan, deleted: boolean) =>
|
||||||
|
`<li>${plan.folder}${plan.folder && '/'}${plan.name} - ${formatDateTime(plan.lastUpdated)} - ${makePlanButtons(plan.id, deleted)}</li>`;
|
||||||
|
|
||||||
export default async (userId: string, userName: string) => {
|
export default async (userId: string, userName: string) => {
|
||||||
let failed = false;
|
let failed = false;
|
||||||
|
|
||||||
const plans: Plan[] = await dbClient
|
const plans: Plan[] = await dbClient
|
||||||
.query('SELECT id, name, folder FROM plans WHERE ownerId = ? AND deleted = 0 GROUP BY folder,name,id ORDER BY folder ASC,name ASC', [userId])
|
.query('SELECT id, name, folder, lastUpdated FROM plans WHERE ownerId = ? AND deleted = 0 GROUP BY folder,name,id ORDER BY folder ASC,name ASC', [userId])
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
failed = true;
|
failed = true;
|
||||||
});
|
});
|
||||||
if (failed) return "Couldn't read DB.";
|
if (failed) return "Couldn't read DB.";
|
||||||
|
|
||||||
const deletedPlans: Plan[] = await dbClient
|
const deletedPlans: Plan[] = await dbClient
|
||||||
.query('SELECT id, name, folder FROM plans WHERE ownerId = ? AND deleted = 1 GROUP BY folder,name,id ORDER BY folder ASC,name ASC', [userId])
|
.query('SELECT id, name, folder, lastUpdated FROM plans WHERE ownerId = ? AND deleted = 1 GROUP BY folder,name,id ORDER BY folder ASC,name ASC', [userId])
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
failed = true;
|
failed = true;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user