-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from Celesca/24-feature-personal-preference
Feature : Personal preference
- Loading branch information
Showing
7 changed files
with
263 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
prisma/migrations/20241124091311_change_typo/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
prisma/migrations/20241124091339_typo_personal/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
prisma/migrations/20241124092849_personal_type/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters