mirror of
https://git.sr.ht/~roxwize/mipilin
synced 2025-05-11 15:53:07 +00:00
uhhh yeah
Signed-off-by: roxwize <rae@roxwize.xyz>
This commit is contained in:
parent
5abe0b5fad
commit
afc634b0d2
43 changed files with 573 additions and 3792 deletions
|
@ -2,12 +2,13 @@ import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|||
import { Express } from "express";
|
||||
import {
|
||||
follows,
|
||||
inviteCodes,
|
||||
journalEntries,
|
||||
profiles,
|
||||
updates,
|
||||
users
|
||||
} from "../db/schema.js";
|
||||
import { and, desc, eq } from "drizzle-orm";
|
||||
import { and, count, desc, eq, sql } from "drizzle-orm";
|
||||
import dayjs from "dayjs";
|
||||
import { getMoods, render } from "./util.js";
|
||||
|
||||
|
@ -72,12 +73,39 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
};
|
||||
});
|
||||
|
||||
// user invite codes
|
||||
const codes = (await db
|
||||
.select({ token: inviteCodes.token, expires: inviteCodes.expires })
|
||||
.from(inviteCodes)
|
||||
.where(eq(inviteCodes.user, req.session["uid"]))).map((e) => {
|
||||
return {
|
||||
token: e.token,
|
||||
expires: now.to(dayjs(e.expires || 0))
|
||||
}
|
||||
});
|
||||
const { codesUsed } = (
|
||||
await db
|
||||
.select({ codesUsed: count() })
|
||||
.from(inviteCodes)
|
||||
.where(
|
||||
and(
|
||||
eq(inviteCodes.user, req.session["uid"]),
|
||||
eq(
|
||||
sql`extract(month from granted)`,
|
||||
sql`extract(month from current_date)`
|
||||
)
|
||||
)
|
||||
)
|
||||
)[0];
|
||||
|
||||
render(db, "dashboard", "Dashboard", res, req, {
|
||||
user,
|
||||
moods,
|
||||
moodsSorted,
|
||||
moodHistory,
|
||||
recentUpdates,
|
||||
codes,
|
||||
codesUsed,
|
||||
feed: []
|
||||
});
|
||||
});
|
||||
|
@ -120,6 +148,26 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
app.get("/journal", async (req, res) => {
|
||||
render(db, "journal", "Journal", res, req);
|
||||
});
|
||||
app.get("/journal/:id", async (req, res) => {
|
||||
const entry = (
|
||||
await db
|
||||
.select({
|
||||
uname: users.name,
|
||||
content: journalEntries.entry,
|
||||
date: journalEntries.date
|
||||
})
|
||||
.from(journalEntries)
|
||||
.where(eq(journalEntries.id, parseInt(req.params.id)))
|
||||
.leftJoin(users, eq(journalEntries.user, users.id))
|
||||
)[0];
|
||||
if (!entry) {
|
||||
//! TODO write a 404 page
|
||||
res.statusCode = 404;
|
||||
res.write("404 not found?? :(");
|
||||
return;
|
||||
}
|
||||
render(db, "journal_view", "Journal Entry", res, req, { entry });
|
||||
});
|
||||
app.post("/update/journal", async (req, res) => {
|
||||
if (!req.session["loggedIn"]) {
|
||||
res.redirect("/login");
|
||||
|
@ -141,21 +189,31 @@ export default async function (app: Express, db: NodePgDatabase) {
|
|||
|
||||
let id: number;
|
||||
try {
|
||||
// @ts-expect-error
|
||||
const entry = await db.insert(journalEntries).values({
|
||||
user: req.session["uid"],
|
||||
moodChange,
|
||||
visibility,
|
||||
entry: req.body.description,
|
||||
date: new Date(Date.now())
|
||||
}).returning({ id: journalEntries.id });
|
||||
const entry = await db
|
||||
.insert(journalEntries)
|
||||
// @ts-expect-error
|
||||
.values({
|
||||
user: req.session["uid"],
|
||||
moodChange,
|
||||
visibility,
|
||||
entry: req.body.description,
|
||||
date: new Date(Date.now())
|
||||
})
|
||||
.returning({ id: journalEntries.id });
|
||||
id = entry[0].id;
|
||||
} catch (err) {
|
||||
req.flash("error", "Failed to create your entry. Try again later or send these logs to roxwize so she can know what's up:<br><br>"+err);
|
||||
req.flash(
|
||||
"error",
|
||||
"Failed to create your entry. Try again later or send these logs to roxwize so she can know what's up:<br><br>" +
|
||||
err
|
||||
);
|
||||
res.redirect("/journal");
|
||||
return;
|
||||
}
|
||||
req.flash("success", `Your journal entry is now available as <a href="/journal/view?id=${id}">#${id}</a>!`);
|
||||
req.flash(
|
||||
"success",
|
||||
`Your journal entry is now available as <a href="/journal/${id}">#${id}</a>!`
|
||||
);
|
||||
res.redirect("/journal");
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue