From 2b9392cc7795b60257570348fd04e2f7b82ebd38 Mon Sep 17 00:00:00 2001 From: celesca Date: Mon, 18 Nov 2024 23:49:11 +0700 Subject: [PATCH 01/10] modified: prisma --- prisma/schema.prisma | 128 +++++++++++++++++++++---------------------- 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index ed9372b..8edab84 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -14,97 +14,97 @@ datasource db { } model User { - UserID String @id @default(uuid()) // Primary Key - UserName String - Email String @unique - Password String - WorkingStyle String? - ProfileImage String? - Bio String? - CreatedAt DateTime @default(now()) - UpdatedAt DateTime @updatedAt @default(now()) - MessagesSent Message[] @relation("MessagesSent") - MessagesReceived Message[] @relation("MessagesReceived") - SwipesMade Swipe[] @relation("SwipesMade") - SwipesReceived Swipe[] @relation("SwipesReceived") - Notifications Notification[] - UserSkills UserSkills[] // Relation to UserSkills (many-to-many with Skills) - UserTeams UserTeam[] // Relation to UserTeam (many-to-many with Teams) + UserID String @id @default(uuid()) // Primary Key + UserName String + Email String @unique + Password String + WorkingStyle String? + ProfileImage String? + Bio String? + CreatedAt DateTime @default(now()) + UpdatedAt DateTime @default(now()) @updatedAt + MessagesSent Message[] @relation("MessagesSent") + MessagesReceived Message[] @relation("MessagesReceived") + SwipesMade Swipe[] @relation("SwipesMade") + SwipesReceived Swipe[] @relation("SwipesReceived") + Notifications Notification[] + UserSkills UserSkills[] // Relation to UserSkills (many-to-many with Skills) + UserTeams UserTeam[] // Relation to UserTeam (many-to-many with Teams) } model Message { - MessageID Int @id @default(autoincrement()) // Primary Key - Sender User @relation("MessagesSent", fields: [SenderID], references: [UserID]) + MessageID Int @id @default(autoincrement()) // Primary Key + Sender User @relation("MessagesSent", fields: [SenderID], references: [UserID]) SenderID String - Receiver User @relation("MessagesReceived", fields: [ReceiverID], references: [UserID]) + Receiver User @relation("MessagesReceived", fields: [ReceiverID], references: [UserID]) ReceiverID String MessageContent String - Timestamp DateTime @default(now()) - ReadStatus Boolean @default(false) + Timestamp DateTime @default(now()) + ReadStatus Boolean @default(false) } model Swipe { - SwipeID Int @id @default(autoincrement()) // Primary Key - SwipingUser User @relation("SwipesMade", fields: [SwipingUserID], references: [UserID]) - SwipingUserID String - SwipedUser User @relation("SwipesReceived", fields: [SwipedUserID], references: [UserID]) - SwipedUserID String - SwipeAction String // "Like" or "Dislike" - Timestamp DateTime @default(now()) + SwipeID Int @id @default(autoincrement()) // Primary Key + SwipingUser User @relation("SwipesMade", fields: [SwipingUserID], references: [UserID]) + SwipingUserID String + SwipedUser User @relation("SwipesReceived", fields: [SwipedUserID], references: [UserID]) + SwipedUserID String + SwipeAction String // "Like" or "Dislike" + Timestamp DateTime @default(now()) } model Notification { - NotificationID Int @id @default(autoincrement()) // Primary Key - User User @relation(fields: [UserID], references: [UserID]) - UserID String + NotificationID Int @id @default(autoincrement()) // Primary Key + User User @relation(fields: [UserID], references: [UserID]) + UserID String NotificationContent String - Timestamp DateTime @default(now()) - ReadStatus Boolean @default(false) + Timestamp DateTime @default(now()) + ReadStatus Boolean @default(false) } model Skills { - Skill_ID Int @id @default(autoincrement()) // Primary Key - Skill_Name String @unique // Unique skill name - UserSkills UserSkills[] // Relation to UserSkills + Skill_ID Int @id @default(autoincrement()) // Primary Key + Skill_Name String @unique // Unique skill name + UserSkills UserSkills[] // Relation to UserSkills } model UserSkills { - UserSkill_ID Int @id @default(autoincrement()) // Primary Key - User User @relation(fields: [UserID], references: [UserID]) - UserID String - Skill Skills @relation(fields: [Skill_ID], references: [Skill_ID], onDelete: Cascade) - Skill_ID Int + UserSkill_ID Int @id @default(autoincrement()) // Primary Key + User User @relation(fields: [UserID], references: [UserID]) + UserID String + Skill Skills @relation(fields: [Skill_ID], references: [Skill_ID], onDelete: Cascade) + Skill_ID Int // Add more fields if you need, like skill level or proficiency } model Hackathon { - HackathonID Int @id @default(autoincrement()) - Name String - Description String - StartDate DateTime - EndDate DateTime - Location String - Teams Team[] // One Hackathon can have multiple teams - CreatedAt DateTime @default(now()) - UpdatedAt DateTime @updatedAt + HackathonID Int @id @default(autoincrement()) + Name String + Description String + StartDate DateTime + EndDate DateTime + Location String + Teams Team[] // One Hackathon can have multiple teams + CreatedAt DateTime @default(now()) + UpdatedAt DateTime @updatedAt } model Team { - TeamID Int @id @default(autoincrement()) - TeamName String - HackathonID Int - Hackathon Hackathon @relation(fields: [HackathonID], references: [HackathonID]) - UserTeams UserTeam[] // Relation to UserTeam (many-to-many with Users) - CreatedAt DateTime @default(now()) - UpdatedAt DateTime @updatedAt + TeamID Int @id @default(autoincrement()) + TeamName String + HackathonID Int + Hackathon Hackathon @relation(fields: [HackathonID], references: [HackathonID]) + UserTeams UserTeam[] // Relation to UserTeam (many-to-many with Users) + CreatedAt DateTime @default(now()) + UpdatedAt DateTime @updatedAt } model UserTeam { - UserTeamID Int @id @default(autoincrement()) // Primary Key - User User @relation(fields: [UserID], references: [UserID]) - UserID String - Team Team @relation(fields: [TeamID], references: [TeamID]) - TeamID Int - Role String // Role of the user in the team (e.g., Member, Leader) - JoinedAt DateTime @default(now()) + UserTeamID Int @id @default(autoincrement()) // Primary Key + User User @relation(fields: [UserID], references: [UserID]) + UserID String + Team Team @relation(fields: [TeamID], references: [TeamID]) + TeamID Int + Role String // Role of the user in the team (e.g., Member, Leader) + JoinedAt DateTime @default(now()) } From 5f956fddb398d5f85fb71c3bb42243f22726ab7d Mon Sep 17 00:00:00 2001 From: celesca Date: Mon, 18 Nov 2024 23:50:23 +0700 Subject: [PATCH 02/10] add: userteam migration --- .../20241118165010_userteam/migration.sql | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 prisma/migrations/20241118165010_userteam/migration.sql diff --git a/prisma/migrations/20241118165010_userteam/migration.sql b/prisma/migrations/20241118165010_userteam/migration.sql new file mode 100644 index 0000000..47163ed --- /dev/null +++ b/prisma/migrations/20241118165010_userteam/migration.sql @@ -0,0 +1,31 @@ +/* + Warnings: + + - You are about to drop the `_TeamMembers` table. If the table is not empty, all the data it contains will be lost. + +*/ +-- DropForeignKey +ALTER TABLE `_TeamMembers` DROP FOREIGN KEY `_TeamMembers_A_fkey`; + +-- DropForeignKey +ALTER TABLE `_TeamMembers` DROP FOREIGN KEY `_TeamMembers_B_fkey`; + +-- DropTable +DROP TABLE `_TeamMembers`; + +-- CreateTable +CREATE TABLE `UserTeam` ( + `UserTeamID` INTEGER NOT NULL AUTO_INCREMENT, + `UserID` VARCHAR(191) NOT NULL, + `TeamID` INTEGER NOT NULL, + `Role` VARCHAR(191) NOT NULL, + `JoinedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), + + PRIMARY KEY (`UserTeamID`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- AddForeignKey +ALTER TABLE `UserTeam` ADD CONSTRAINT `UserTeam_UserID_fkey` FOREIGN KEY (`UserID`) REFERENCES `User`(`UserID`) ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `UserTeam` ADD CONSTRAINT `UserTeam_TeamID_fkey` FOREIGN KEY (`TeamID`) REFERENCES `Team`(`TeamID`) ON DELETE RESTRICT ON UPDATE CASCADE; From bdb56de9e963f7811fbabe59354c39794b0ab2f1 Mon Sep 17 00:00:00 2001 From: celesca Date: Mon, 18 Nov 2024 23:51:31 +0700 Subject: [PATCH 03/10] add teamController.ts --- src/controllers/teamController.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/controllers/teamController.ts diff --git a/src/controllers/teamController.ts b/src/controllers/teamController.ts new file mode 100644 index 0000000..2234aa7 --- /dev/null +++ b/src/controllers/teamController.ts @@ -0,0 +1,10 @@ +import { Elysia, t } from "elysia"; +import { prisma } from "../prisma"; // Prisma client + +export const teamController = new Elysia({ prefix: "/team" }) + +.get("/", async () => { + const teams = await prisma.team.findMany(); + return teams; +}) + From e9265c55eeb9a78f1e1af211388c2af4b5ba876e Mon Sep 17 00:00:00 2001 From: celesca Date: Tue, 19 Nov 2024 00:17:51 +0700 Subject: [PATCH 04/10] get: hackathon_id --- src/controllers/teamController.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/controllers/teamController.ts b/src/controllers/teamController.ts index 2234aa7..8e3fd1d 100644 --- a/src/controllers/teamController.ts +++ b/src/controllers/teamController.ts @@ -8,3 +8,22 @@ export const teamController = new Elysia({ prefix: "/team" }) return teams; }) +.get("/hackathon/:id", async ({ params: { id }, error }) => { + const teams = await prisma.team.findMany({ + where: { + HackathonID: id, + }, + }); + + if (!teams) { + return error(404, "Teams not found"); + } + + return teams; +}, +{ + params: t.Object({ + id: t.Number(), + }), +} +) \ No newline at end of file From 7a4037ae3ed2bc7b7155ccfdc80e7efb27de58fb Mon Sep 17 00:00:00 2001 From: Sawit Koseeyaumporn Date: Tue, 19 Nov 2024 20:09:43 +0700 Subject: [PATCH 05/10] add: matched messages --- bun.lockb | Bin 9976 -> 9979 bytes src/controllers/swipeController.ts | 15 +++++++++++++++ src/controllers/teamController.ts | 5 ++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/bun.lockb b/bun.lockb index e63cf052bd8ade01d23de4485ec32a45d56daefa..5e2d093a51f2686364c28e3f72b94b5ff87ed161 100644 GIT binary patch delta 2074 zcmcIlc~q2D6o22x3^Rkw4Ew+g%dn~t<1oYEun0_OY9In>1PU_j1SrU&L@|I1U|7&Q zYALy;siB|_3TR{<$V5}m@u(ps9?OidFt<=d>b{W=D-VD8NB5lf{ocLzy?ft%%kO(Q z@A14knR9-Wgu^R6-4v=WK6(E0s@kNlT8kR}d>SL#xSvg(HMlstetwoNZ+TWht`?#6 zg0$karG*7yG(tjz%H>7~i2w&cw*u@2)YW*Kn3q{#r zbJ`0>(OkX&vZ`PZ1V!UkI)=u*>|p*naEAQXIb}H%P|8?g8Y8p8?gjSFHnV_SeQhfa zrEU4YgK;i1cs#2g)D-xfH6N_cjiryVaIaA?&0%9PE133Y<9L8gY`huZI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XuCB3+U$(AQV3 zQ3WV90jy4^^h;9ut61N}M4g|e%+1OB->=4D?(TTCoQH*Oe4OPr>OTg { + const { TeamName, HackathonID, TeamMembers \ No newline at end of file From 58b9544d3cd5230ba207a15e1be33a27ae3a6a38 Mon Sep 17 00:00:00 2001 From: Sawit Koseeyaumporn Date: Tue, 19 Nov 2024 20:18:05 +0700 Subject: [PATCH 06/10] post: create team --- src/controllers/teamController.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/controllers/teamController.ts b/src/controllers/teamController.ts index a8a6f11..25c9188 100644 --- a/src/controllers/teamController.ts +++ b/src/controllers/teamController.ts @@ -29,4 +29,19 @@ export const teamController = new Elysia({ prefix: "/team" }) ) .post("/create", async ({ body, error }) => { - const { TeamName, HackathonID, TeamMembers \ No newline at end of file + const { teamName, hackathonID } = body; + + await prisma.team.create({ + data: { + TeamName: teamName, + HackathonID: hackathonID, + }, + }); + + +}, { + body: t.Object({ + teamName: t.String(), + hackathonID: t.Number(), + }), +}) \ No newline at end of file From f933895a58c9503f7aace4f55d183ec6ae7677bb Mon Sep 17 00:00:00 2001 From: Sawit Koseeyaumporn Date: Tue, 19 Nov 2024 20:19:14 +0700 Subject: [PATCH 07/10] update: team --- src/controllers/teamController.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/controllers/teamController.ts b/src/controllers/teamController.ts index 25c9188..d5c6586 100644 --- a/src/controllers/teamController.ts +++ b/src/controllers/teamController.ts @@ -44,4 +44,22 @@ export const teamController = new Elysia({ prefix: "/team" }) teamName: t.String(), hackathonID: t.Number(), }), +}) + +.put("/update", async ({ body, error }) => { + const { teamID, teamName } = body; + + const team = await prisma.team.update({ + where: { TeamID: teamID }, + data: { + TeamName: teamName, + }, + }); + + return team; +}, { + body: t.Object({ + teamID: t.Number(), + teamName: t.String(), + }), }) \ No newline at end of file From 476e977711ae66ebe11a62760a7abd255e5286c6 Mon Sep 17 00:00:00 2001 From: Sawit Koseeyaumporn Date: Tue, 19 Nov 2024 20:19:44 +0700 Subject: [PATCH 08/10] delete: hackathons --- src/controllers/teamController.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/controllers/teamController.ts b/src/controllers/teamController.ts index d5c6586..65b26f1 100644 --- a/src/controllers/teamController.ts +++ b/src/controllers/teamController.ts @@ -62,4 +62,18 @@ export const teamController = new Elysia({ prefix: "/team" }) teamID: t.Number(), teamName: t.String(), }), +}) + +.delete("/delete", async ({ body, error }) => { + const { teamID } = body; + + const team = await prisma.team.delete({ + where: { TeamID: teamID }, + }); + + return team; +}, { + body: t.Object({ + teamID: t.Number(), + }), }) \ No newline at end of file From cce46fbabd43826f1e85aa7641c3eaa6947a1f12 Mon Sep 17 00:00:00 2001 From: Sawit Koseeyaumporn Date: Tue, 19 Nov 2024 20:21:12 +0700 Subject: [PATCH 09/10] add: delete notificationController --- src/controllers/notificationController.ts | 28 ++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/controllers/notificationController.ts b/src/controllers/notificationController.ts index 02ae42f..85daedb 100644 --- a/src/controllers/notificationController.ts +++ b/src/controllers/notificationController.ts @@ -78,4 +78,30 @@ export const notificationController = new Elysia({ prefix: "/noti" }) body: t.Object({ ReadStatus: t.Boolean(), }), -}) \ No newline at end of file +}) + +// Delete a notification +.delete("/:notificationID", async ({ params, error }) => { + const { notificationID } = params; + + // Check if notification exists + const notification = await prisma.notification.findUnique({ + where: { NotificationID: notificationID }, + }); + + if (!notification) { + return error(404, "Notification not found"); + } + + // Delete the notification + await prisma.notification.delete({ + where: { NotificationID: notificationID }, + }); + + return { message: "Notification deleted successfully" }; +}, +{ + params: t.Object({ + notificationID: t.Number(), + }), +}) From 1494602f842dcb1e25ce6a194645b7635ef32691 Mon Sep 17 00:00:00 2001 From: Sawit Koseeyaumporn Date: Tue, 19 Nov 2024 20:26:23 +0700 Subject: [PATCH 10/10] delete Messages --- src/controllers/messageController.ts | 114 ++++++++++++++++----------- 1 file changed, 68 insertions(+), 46 deletions(-) diff --git a/src/controllers/messageController.ts b/src/controllers/messageController.ts index 6b010b9..650a216 100644 --- a/src/controllers/messageController.ts +++ b/src/controllers/messageController.ts @@ -9,7 +9,6 @@ export const messageController = new Elysia({ prefix: "/message" }) async ({ body, error }) => { const { senderID, receiverID, messageContent } = body; - // Check if both users exist const sender = await prisma.user.findUnique({ where: { UserID: senderID }, @@ -44,58 +43,53 @@ export const messageController = new Elysia({ prefix: "/message" }) ) // Get all messages between two users - .get( - "/:senderID/:receiverID", - async ({ params, error }) => { - const { senderID, receiverID } = params; - - // Check if both users exist - const sender = await prisma.user.findUnique({ - where: { UserID: senderID }, - }); - - const receiver = await prisma.user.findUnique({ - where: { UserID: receiverID }, - }); + .get("/:senderID/:receiverID", async ({ params, error }) => { + const { senderID, receiverID } = params; - if (!sender || !receiver) { - return error(404, "Sender or receiver not found"); - } + // Check if both users exist + const sender = await prisma.user.findUnique({ + where: { UserID: senderID }, + }); - // Get all messages between the two users - const messages = await prisma.message.findMany({ - where: { - OR: [ - { SenderID: senderID, ReceiverID: receiverID }, - { SenderID: receiverID, ReceiverID: senderID }, - ], - }, - orderBy: { - Timestamp: 'asc', - }, - }); + const receiver = await prisma.user.findUnique({ + where: { UserID: receiverID }, + }); - return messages; + if (!sender || !receiver) { + return error(404, "Sender or receiver not found"); } - ) + + // Get all messages between the two users + const messages = await prisma.message.findMany({ + where: { + OR: [ + { SenderID: senderID, ReceiverID: receiverID }, + { SenderID: receiverID, ReceiverID: senderID }, + ], + }, + orderBy: { + Timestamp: "asc", + }, + }); + + return messages; + }) // Query the message from each users that users contact with - .get( - "/inbox/:userID", - async ({ params, error }) => { - const { userID } = params; + .get("/inbox/:userID", async ({ params, error }) => { + const { userID } = params; - // Check if the user exists - const user = await prisma.user.findUnique({ - where: { UserID: userID }, - }); + // Check if the user exists + const user = await prisma.user.findUnique({ + where: { UserID: userID }, + }); - if (!user) { - return error(404, "User not found"); - } + if (!user) { + return error(404, "User not found"); + } - // Get the latest message from each conversation - const latestMessages = await prisma.$queryRaw` + // Get the latest message from each conversation + const latestMessages = await prisma.$queryRaw` SELECT DISTINCT ON (LEAST("SenderID", "ReceiverID"), GREATEST("SenderID", "ReceiverID")) "MessageID", "SenderID", "ReceiverID", "MessageContent", "Timestamp" FROM "Message" @@ -103,6 +97,34 @@ export const messageController = new Elysia({ prefix: "/message" }) ORDER BY LEAST("SenderID", "ReceiverID"), GREATEST("SenderID", "ReceiverID"), "Timestamp" DESC `; - return latestMessages; + return latestMessages; + }) + + // Delete a message + .delete( + "/delete/:messageID", + async ({ params, error }) => { + const { messageID } = params; + + // Check if the message exists + const message = await prisma.message.findUnique({ + where: { MessageID: parseInt(messageID) }, + }); + + if (!message) { + return error(404, "Message not found"); + } + + // Delete the message + await prisma.message.delete({ + where: { MessageID: parseInt(messageID) }, + }); + + return { message: "Message deleted successfully" }; + }, + { + params: t.Object({ + messageID: t.String(), + }), } - ); \ No newline at end of file + );