Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature : Personal preference #25

Merged
merged 19 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions prisma/migrations/20241124090731_personal/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
15 changes: 15 additions & 0 deletions prisma/migrations/20241124091311_change_typo/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 10 additions & 0 deletions prisma/migrations/20241124091339_typo_personal/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
33 changes: 33 additions & 0 deletions prisma/migrations/20241124092849_personal_type/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 9 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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[]
}
170 changes: 170 additions & 0 deletions src/controllers/personalController.ts
Original file line number Diff line number Diff line change
@@ -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(),
}),
})
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "*",
}))
Expand All @@ -24,6 +25,7 @@ const app = new Elysia()
.use(hackathonController)
.use(teamController)
.use(ratingController)
.use(personalController)
.use(cors({
origin: "*",
}))
Expand Down