mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 22:09:16 +01:00
refactor(database): 🚚 Move Likes to our custom ORM
This commit is contained in:
parent
e52e230ce3
commit
5a26bdf2f8
7 changed files with 262 additions and 119 deletions
|
|
@ -2,8 +2,6 @@ import { apiRoute, applyConfig, auth } from "@/api";
|
|||
import { createRoute } from "@hono/zod-openapi";
|
||||
import { z } from "zod";
|
||||
import { Note } from "~/classes/database/note";
|
||||
import { createLike } from "~/classes/functions/like";
|
||||
import { db } from "~/drizzle/db";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
import { ErrorSchema } from "~/types/api";
|
||||
|
||||
|
|
@ -79,21 +77,10 @@ export default apiRoute((app) =>
|
|||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
const existingLike = await db.query.Likes.findFirst({
|
||||
where: (like, { and, eq }) =>
|
||||
and(eq(like.likedId, note.data.id), eq(like.likerId, user.id)),
|
||||
});
|
||||
await user.like(note);
|
||||
|
||||
if (!existingLike) {
|
||||
await createLike(user, note);
|
||||
}
|
||||
await note.reload(user.id);
|
||||
|
||||
const newNote = await Note.fromId(id, user.id);
|
||||
|
||||
if (!newNote) {
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
return context.json(await newNote.toApi(user), 200);
|
||||
return context.json(await note.toApi(user), 200);
|
||||
}),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ import { apiRoute, applyConfig, auth } from "@/api";
|
|||
import { createRoute } from "@hono/zod-openapi";
|
||||
import { z } from "zod";
|
||||
import { Note } from "~/classes/database/note";
|
||||
import { deleteLike } from "~/classes/functions/like";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
import { ErrorSchema } from "~/types/api";
|
||||
|
||||
|
|
@ -77,14 +76,10 @@ export default apiRoute((app) =>
|
|||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
await deleteLike(user, note);
|
||||
await user.unlike(note);
|
||||
|
||||
const newNote = await Note.fromId(id, user.id);
|
||||
await note.reload(user.id);
|
||||
|
||||
if (!newNote) {
|
||||
return context.json({ error: "Record not found" }, 404);
|
||||
}
|
||||
|
||||
return context.json(await newNote.toApi(user), 200);
|
||||
return context.json(await note.toApi(user), 200);
|
||||
}),
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue