upd
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import authMiddleware from "../middlewares/auth";
|
||||
import getComments from "../services/comments/getComments";
|
||||
import { createComment } from "../services/comments/createComment";
|
||||
|
||||
export const commentsController = new Elysia({ prefix: "comments" })
|
||||
.use(authMiddleware)
|
||||
.get(
|
||||
"/:sessionId",
|
||||
async ({ params: { sessionId }, auth: { userId } }) =>
|
||||
await getComments(sessionId, userId),
|
||||
{
|
||||
params: t.Object({
|
||||
sessionId: t.String(),
|
||||
}),
|
||||
}
|
||||
)
|
||||
.post(
|
||||
"/:sessionId",
|
||||
async ({ params: { sessionId }, auth: { userId }, body: { text } }) =>
|
||||
await createComment(sessionId, userId, text),
|
||||
{
|
||||
params: t.Object({
|
||||
sessionId: t.String(),
|
||||
}),
|
||||
body: t.Object({
|
||||
text: t.String(),
|
||||
}),
|
||||
}
|
||||
);
|
||||
@@ -0,0 +1,28 @@
|
||||
import { pgTable, serial, text, timestamp, uuid } from "drizzle-orm/pg-core";
|
||||
import { usersTable } from "./users";
|
||||
import { sessionsTable } from "./sessions";
|
||||
import { relations } from "drizzle-orm";
|
||||
|
||||
export const commentsTable = pgTable("comments", {
|
||||
id: serial("id").primaryKey(),
|
||||
text: text("text").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at").notNull().defaultNow(),
|
||||
ownerId: uuid("owner_id")
|
||||
.notNull()
|
||||
.references(() => usersTable.id),
|
||||
sessionId: uuid("session_id")
|
||||
.notNull()
|
||||
.references(() => sessionsTable.id),
|
||||
});
|
||||
|
||||
export const commentsRelations = relations(commentsTable, ({ one }) => ({
|
||||
owner: one(usersTable, {
|
||||
fields: [commentsTable.ownerId],
|
||||
references: [usersTable.id],
|
||||
}),
|
||||
session: one(sessionsTable, {
|
||||
fields: [commentsTable.sessionId],
|
||||
references: [sessionsTable.id],
|
||||
}),
|
||||
}));
|
||||
@@ -6,3 +6,4 @@ export * from "./sessions";
|
||||
export * from "./clients";
|
||||
export * from "./apps";
|
||||
export * from "./actions";
|
||||
export * from "./comments";
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { error } from "elysia";
|
||||
import db from "../../db";
|
||||
import { commentsTable } from "../../db/schema";
|
||||
|
||||
export async function createComment(
|
||||
sessionId: string,
|
||||
userId: string,
|
||||
text: string
|
||||
) {
|
||||
try {
|
||||
const comment = await db
|
||||
.insert(commentsTable)
|
||||
.values({
|
||||
sessionId,
|
||||
ownerId: userId,
|
||||
text,
|
||||
})
|
||||
.returning();
|
||||
return comment;
|
||||
} catch (err) {
|
||||
console.log((err as Error).message);
|
||||
return error(500, "Internal Server Error");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { error } from "elysia";
|
||||
import { and, desc, eq } from "drizzle-orm";
|
||||
import db from "../../db";
|
||||
import { commentsTable } from "../../db/schema";
|
||||
|
||||
export async function getComments(sessionId: string, userId: string) {
|
||||
try {
|
||||
const comments = await db.query.commentsTable.findMany({
|
||||
where: and(
|
||||
eq(commentsTable.sessionId, sessionId),
|
||||
eq(commentsTable.ownerId, userId)
|
||||
),
|
||||
with: {
|
||||
owner: {
|
||||
columns: {
|
||||
fullname: true,
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: desc(commentsTable.createdAt),
|
||||
});
|
||||
return comments;
|
||||
} catch (err) {
|
||||
console.log((err as Error).message);
|
||||
return error(500, "Internal Server Error");
|
||||
}
|
||||
}
|
||||
|
||||
export default getComments;
|
||||
@@ -41,6 +41,13 @@ export default async function getServers(
|
||||
app: {
|
||||
columns: {
|
||||
name: true,
|
||||
id: true,
|
||||
},
|
||||
},
|
||||
server: true,
|
||||
owner: {
|
||||
columns: {
|
||||
fullname: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user