diff --git a/prisma/migrations/20241124090731_personal/migration.sql b/prisma/migrations/20241124090731_personal/migration.sql new file mode 100644 index 0000000..b02c66e --- /dev/null +++ b/prisma/migrations/20241124090731_personal/migration.sql @@ -0,0 +1,23 @@ +-- CreateTable +CREATE TABLE `Personal` ( + `PersonalID` INTEGER NOT NULL AUTO_INCREMENT, + `Personal_type` VARCHAR(191) NOT NULL, + `Personal_type_detail` VARCHAR(191) NOT NULL, + + PRIMARY KEY (`PersonalID`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- CreateTable +CREATE TABLE `UserPersonal` ( + `UserPersonalID` INTEGER NOT NULL AUTO_INCREMENT, + `UserID` VARCHAR(191) NOT NULL, + `PersonalID` INTEGER NOT NULL, + + PRIMARY KEY (`UserPersonalID`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- AddForeignKey +ALTER TABLE `UserPersonal` ADD CONSTRAINT `UserPersonal_UserID_fkey` FOREIGN KEY (`UserID`) REFERENCES `User`(`UserID`) ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE `UserPersonal` ADD CONSTRAINT `UserPersonal_PersonalID_fkey` FOREIGN KEY (`PersonalID`) REFERENCES `Personal`(`PersonalID`) ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/migrations/20241124091311_change_typo/migration.sql b/prisma/migrations/20241124091311_change_typo/migration.sql new file mode 100644 index 0000000..accb53c --- /dev/null +++ b/prisma/migrations/20241124091311_change_typo/migration.sql @@ -0,0 +1,15 @@ +/* + Warnings: + + - You are about to drop the column `Personal_type` on the `personal` table. All the data in the column will be lost. + - You are about to drop the column `Personal_type_detail` on the `personal` table. All the data in the column will be lost. + - Added the required column `PersonalType` to the `Personal` table without a default value. This is not possible if the table is not empty. + - Added the required column `PersonalType_detail` to the `Personal` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE `personal` DROP COLUMN `Personal_type`, + DROP COLUMN `Personal_type_detail`, + ADD COLUMN `PersonalType` VARCHAR(191) NOT NULL, + ADD COLUMN `PersonalType_detail` VARCHAR(191) NOT NULL, + MODIFY `PersonalID` INTEGER NOT NULL; diff --git a/prisma/migrations/20241124091339_typo_personal/migration.sql b/prisma/migrations/20241124091339_typo_personal/migration.sql new file mode 100644 index 0000000..6d77922 --- /dev/null +++ b/prisma/migrations/20241124091339_typo_personal/migration.sql @@ -0,0 +1,10 @@ +/* + Warnings: + + - You are about to drop the column `PersonalType_detail` on the `personal` table. All the data in the column will be lost. + - Added the required column `PersonalTypeDetail` to the `Personal` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE `personal` DROP COLUMN `PersonalType_detail`, + ADD COLUMN `PersonalTypeDetail` VARCHAR(191) NOT NULL; diff --git a/prisma/migrations/20241124092849_personal_type/migration.sql b/prisma/migrations/20241124092849_personal_type/migration.sql new file mode 100644 index 0000000..6e62be7 --- /dev/null +++ b/prisma/migrations/20241124092849_personal_type/migration.sql @@ -0,0 +1,33 @@ +/* + Warnings: + + - You are about to drop the `personal` table. If the table is not empty, all the data it contains will be lost. + - You are about to drop the `userpersonal` table. If the table is not empty, all the data it contains will be lost. + +*/ +-- DropForeignKey +ALTER TABLE `userpersonal` DROP FOREIGN KEY `UserPersonal_PersonalID_fkey`; + +-- DropForeignKey +ALTER TABLE `userpersonal` DROP FOREIGN KEY `UserPersonal_UserID_fkey`; + +-- AlterTable +ALTER TABLE `user` ADD COLUMN `PersonalTypeID` INTEGER NULL; + +-- DropTable +DROP TABLE `personal`; + +-- DropTable +DROP TABLE `userpersonal`; + +-- CreateTable +CREATE TABLE `PersonalType` ( + `PersonalTypeID` INTEGER NOT NULL AUTO_INCREMENT, + `PersonalType` VARCHAR(191) NOT NULL, + `PersonalTypeDetail` VARCHAR(191) NOT NULL, + + PRIMARY KEY (`PersonalTypeID`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; + +-- AddForeignKey +ALTER TABLE `User` ADD CONSTRAINT `User_PersonalTypeID_fkey` FOREIGN KEY (`PersonalTypeID`) REFERENCES `PersonalType`(`PersonalTypeID`) ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 2783b90..323aa2d 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -22,6 +22,8 @@ model User { ProfileImage String? Bio String? AverageRating Float? // Calculated from UserRating + PersonalType PersonalType? @relation(fields: [PersonalTypeID], references: [PersonalTypeID]) + PersonalTypeID Int? CreatedAt DateTime @default(now()) UpdatedAt DateTime @default(now()) @updatedAt MessagesSent Message[] @relation("MessagesSent") @@ -123,3 +125,10 @@ model UserTeam { Role String // Role of the user in the team (e.g., Member, Leader) JoinedAt DateTime @default(now()) } + +model PersonalType { + PersonalTypeID Int @id @default(autoincrement()) + PersonalType String + PersonalTypeDetail String + Users User[] +} \ No newline at end of file diff --git a/src/controllers/personalController.ts b/src/controllers/personalController.ts new file mode 100644 index 0000000..f3cc731 --- /dev/null +++ b/src/controllers/personalController.ts @@ -0,0 +1,170 @@ +import { Elysia, t } from "elysia"; +import { prisma } from "../prisma"; // Prisma client + +export const personalController = new Elysia({ prefix: "/personal" }) + +// Get PersonalType of the user +.get("/:userID", async ({ params, error }) => { + const { userID } = params; + + // Check if the user exists + const user = await prisma.user.findUnique({ + where: { UserID: userID }, + }); + + if (!user) { + return error(404, "User not found"); + } + + // Get all personal information of the user + const personal = await prisma.user.findMany({ + where: { UserID: userID }, + }); + + // Get the personalTypeID of the user + const personalTypeID = personal.length > 0 ? personal[0].PersonalTypeID : null; + + if (!personalTypeID) { + return error(404, "Personal type not found"); + } + + // Get the personal type of the user + const personalType = await prisma.personalType.findUnique({ + where: { PersonalTypeID: personalTypeID }, + }); + + // Return the json of the userID and the personalType + return { + userID: userID, + personalType: personalType, + }; +}, { + params: t.Object({ + userID: t.String(), + }), +}) + +// Create the new personal type in the personal table. +.post('/create', async ({ body, error }) => { + const { personalType, personalTypeDetail } = body; + + // Create the new personal type + const newPersonalType = await prisma.personalType.create({ + data: { + PersonalType: personalType, + PersonalTypeDetail: personalTypeDetail, + }, + }); + + return newPersonalType; +}, { + body: t.Object({ + personalType: t.String(), + personalTypeDetail: t.String(), + }), +}) + +// Add the personal type to the user +.post('/addToUser', async ({ body, error }) => { + const { userID, personalTypeID } = body; + + // Check if the user exists + const user = await prisma.user.findUnique({ + where: { UserID: userID }, + }); + + if (!user) { + return error(404, "User not found"); + } + + // Check if the personal type exists + const personalType = await prisma.personalType.findUnique({ + where: { PersonalTypeID: personalTypeID }, + }); + + if (!personalType) { + return error(404, "Personal type not found"); + } + + // Add the personal type to the user + const newPersonal = await prisma.user.update({ + where: { UserID: userID }, + data: { + PersonalTypeID: personalTypeID, + }, + }); + + return newPersonal; +}, { + body: t.Object({ + userID: t.String(), + personalTypeID: t.Number(), + }), +}) + +// Update the personal type of the user +.put('/update', async ({ body, error }) => { + const { userID, personalTypeID } = body; + + // Check if the user exists + const user = await prisma.user.findUnique({ + where: { UserID: userID }, + }); + + if (!user) { + return error(404, "User not found"); + } + + // Check if the personal type exists + const personalType = await prisma.personalType.findUnique({ + where: { PersonalTypeID: personalTypeID }, + }); + + if (!personalType) { + return error(404, "Personal type not found"); + } + + // Update the personal type of the user + const updatedPersonal = await prisma.user.update({ + where: { UserID: userID }, + data: { + PersonalTypeID: personalTypeID, + }, + }); + + return updatedPersonal; +}, { + body: t.Object({ + userID: t.String(), + personalTypeID: t.Number(), + }), +}) + +// delete the user +.delete('/:userID', async ({ params, error }) => { + const { userID } = params; + + // Check if the user exists + const user = await prisma.user.findUnique({ + where: { UserID: userID }, + }); + + if (!user) { + return error(404, "User not found"); + } + + // Update the personalTypeID to null + const updatedPersonal = await prisma.user.update({ + where: { UserID: userID }, + data: { + PersonalTypeID: null, + }, + }); + + return updatedPersonal; + +}, { + params: t.Object({ + userID: t.String(), + }), +}) diff --git a/src/index.ts b/src/index.ts index de36461..fbcf57c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,10 +9,11 @@ import { notificationController } from "./controllers/notificationController"; import { hackathonController } from "./controllers/hackathonController"; import { teamController } from "./controllers/teamController"; import { ratingController } from "./controllers/ratingController"; +import { personalController } from "./controllers/personalController"; const app = new Elysia() .get("/", () => "Hello Elysia") - .use(swagger()) + .use(swagger({ path: '/v2/swagger' })) .use(cors({ origin: "*", })) @@ -24,6 +25,7 @@ const app = new Elysia() .use(hackathonController) .use(teamController) .use(ratingController) + .use(personalController) .use(cors({ origin: "*", }))