-
Notifications
You must be signed in to change notification settings - Fork 2
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 #23 from Celesca/22-add-user-rating-feature
Feature: User Rating
- Loading branch information
Showing
6 changed files
with
142 additions
and
5 deletions.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
PORT = 3000 | ||
DATABASE_URL="mysql://root:@localhost:3307/hack_db" | ||
DATABASE_URL="mysql://root:@localhost:3306/hack_db" | ||
|
||
# --- For Docker-compose | ||
# DATABASE_URL="mysql://root:root@localhost:3306/hack_db" |
17 changes: 17 additions & 0 deletions
17
prisma/migrations/20241123135005_user_rating/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,17 @@ | ||
-- CreateTable | ||
CREATE TABLE `UserRating` ( | ||
`UserRatingID` INTEGER NOT NULL AUTO_INCREMENT, | ||
`RatedByID` VARCHAR(191) NOT NULL, | ||
`RatedUserID` VARCHAR(191) NOT NULL, | ||
`RatingValue` INTEGER NOT NULL, | ||
`Comment` VARCHAR(191) NULL, | ||
`CreatedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), | ||
|
||
PRIMARY KEY (`UserRatingID`) | ||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE `UserRating` ADD CONSTRAINT `UserRating_RatedByID_fkey` FOREIGN KEY (`RatedByID`) REFERENCES `User`(`UserID`) ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE `UserRating` ADD CONSTRAINT `UserRating_RatedUserID_fkey` FOREIGN KEY (`RatedUserID`) REFERENCES `User`(`UserID`) ON DELETE RESTRICT 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,106 @@ | ||
import { Elysia, t } from "elysia"; | ||
import { prisma } from "../prisma"; // Prisma client | ||
|
||
export const ratingController = new Elysia({ prefix: "/rating" }) | ||
|
||
// Get all ratings for a user | ||
.get("/:ratedUserID", async ({ params, error }) => { | ||
const { ratedUserID } = params; | ||
|
||
// Check if the user exists | ||
const user = await prisma.user.findUnique({ | ||
where: { UserID: ratedUserID }, | ||
}); | ||
|
||
if (!user) { | ||
return error(404, "User not found"); | ||
} | ||
|
||
// Get all ratings for the user | ||
const ratings = await prisma.userRating.findMany({ | ||
where: { RatedUserID: ratedUserID }, | ||
}); | ||
|
||
return ratings; | ||
}, { | ||
params: t.Object({ | ||
ratedUserID: t.String(), | ||
}), | ||
}) | ||
|
||
// Rating to someone | ||
ratingController.post("/", async ({ body, error }) => { | ||
const { ratedByID, ratedUserID, ratingValue, comment } = body; | ||
|
||
// Check if the user exists | ||
const ratedBy = await prisma.user.findUnique({ | ||
where: { UserID: ratedByID }, | ||
}); | ||
|
||
if (!ratedBy) { | ||
return error(404, "User not found"); | ||
} | ||
|
||
// Check if the user exists | ||
const ratedUser = await prisma.user.findUnique({ | ||
where: { UserID: ratedUserID }, | ||
}); | ||
|
||
if (!ratedUser) { | ||
return error(404, "User not found"); | ||
} | ||
|
||
// Create the new rating | ||
const newRating = await prisma.userRating.create({ | ||
data: { | ||
RatedByID: ratedByID, | ||
RatedUserID: ratedUserID, | ||
RatingValue: ratingValue, | ||
Comment: comment, | ||
}, | ||
}); | ||
|
||
return newRating; | ||
|
||
}, { | ||
body: t.Object({ | ||
ratedByID: t.String(), | ||
ratedUserID: t.String(), | ||
ratingValue: t.Number(), | ||
comment: t.Optional(t.String()), | ||
}), | ||
}) | ||
|
||
// Update rating | ||
.put("/:userRatingID", async ({ params, body, error }) => { | ||
const { userRatingID } = params; | ||
const { ratingValue, comment } = body; | ||
|
||
// Check if the rating exists | ||
const rating = await prisma.userRating.findUnique({ | ||
where: { UserRatingID: userRatingID }, | ||
}); | ||
|
||
if (!rating) { | ||
return error(404, "Rating not found"); | ||
} | ||
|
||
// Update the rating | ||
const updatedRating = await prisma.userRating.update({ | ||
where: { UserRatingID: userRatingID }, | ||
data: { | ||
RatingValue: ratingValue, | ||
Comment: comment, | ||
}, | ||
}); | ||
|
||
return updatedRating; // Return the updated rating | ||
}, { | ||
params: t.Object({ | ||
userRatingID: t.Number(), | ||
}), | ||
body: t.Object({ | ||
ratingValue: t.Optional(t.Number()), | ||
comment: t.Optional(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