Skip to content

Commit

Permalink
Merge pull request #23 from Celesca/22-add-user-rating-feature
Browse files Browse the repository at this point in the history
Feature: User Rating
  • Loading branch information
Celesca authored Nov 23, 2024
2 parents 6cf4e92 + d124b17 commit cea2898
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .env
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"
Binary file modified bun.lockb
Binary file not shown.
17 changes: 17 additions & 0 deletions prisma/migrations/20241123135005_user_rating/migration.sql
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;
19 changes: 16 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,29 @@ model User {
WorkingStyle String?
ProfileImage String?
Bio String?
AverageRating Float?
AverageRating Float? // Calculated from UserRating
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)
UserSkills UserSkills[] // Relation to UserSkills (many-to-many with Skills)
UserTeams UserTeam[] // Relation to UserTeam (many-to-many with Teams)
RatingsGiven UserRating[] @relation("RatingsGiven") // Ratings this user has given
RatingsReceived UserRating[] @relation("RatingsReceived") // Ratings this user has received
}

model UserRating {
UserRatingID Int @id @default(autoincrement()) // Primary Key
RatedBy User @relation("RatingsGiven", fields: [RatedByID], references: [UserID])
RatedByID String
RatedUser User @relation("RatingsReceived", fields: [RatedUserID], references: [UserID])
RatedUserID String
RatingValue Int // Rating value (e.g., 1-5 stars)
Comment String? // Optional comment
CreatedAt DateTime @default(now())
}

model Message {
Expand Down
106 changes: 106 additions & 0 deletions src/controllers/ratingController.ts
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()),
}),
})
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Elysia } from "elysia";
import { cors } from "@elysiajs/cors";
import { swagger } from "@elysiajs/swagger";
import { cors } from "@elysiajs/cors";
import { swipeController } from "./controllers/swipeController";
import { userController } from "./controllers/userController";
import { skillController } from "./controllers/skillController";
import { messageController } from "./controllers/messageController";
import { notificationController } from "./controllers/notificationController";
import { hackathonController } from "./controllers/hackathonController";
import { teamController } from "./controllers/teamController";
import { ratingController } from "./controllers/ratingController";

const app = new Elysia()
.get("/", () => "Hello Elysia")
Expand All @@ -23,6 +23,7 @@ const app = new Elysia()
.use(messageController)
.use(hackathonController)
.use(teamController)
.use(ratingController)
.use(cors({
origin: "*",
}))
Expand Down

0 comments on commit cea2898

Please sign in to comment.