From 81d71ef776a8af75fbf89fd86d29371115f828fc Mon Sep 17 00:00:00 2001 From: LONECODER1 Date: Tue, 28 Oct 2025 11:32:43 +0530 Subject: [PATCH 1/3] added auction logic --- backend/src/controllers/auction.controller.ts | 0 backend/src/models/auction.model.ts | 56 +++++++++++++++++++ backend/src/models/bid.model.ts | 25 +++++++++ backend/src/routes/auctionRoutes.ts | 20 +++++++ 4 files changed, 101 insertions(+) create mode 100644 backend/src/controllers/auction.controller.ts create mode 100644 backend/src/models/auction.model.ts create mode 100644 backend/src/models/bid.model.ts create mode 100644 backend/src/routes/auctionRoutes.ts diff --git a/backend/src/controllers/auction.controller.ts b/backend/src/controllers/auction.controller.ts new file mode 100644 index 0000000..e69de29 diff --git a/backend/src/models/auction.model.ts b/backend/src/models/auction.model.ts new file mode 100644 index 0000000..0d6e596 --- /dev/null +++ b/backend/src/models/auction.model.ts @@ -0,0 +1,56 @@ +import mongoose from "mongoose"; + +const auctionSchema = new mongoose.Schema({ + itemName: { + type: String, + required: true, + trim: true, + }, + itemDescription: { + type: String, + trim: true, + }, + startPrice: { + type: Number, + required: true, + min: 0, + }, + currentHighestBid: { + type: Number, + default: 0, + }, + highestBidder: { + type: mongoose.Schema.Types.ObjectId, + ref: "User", + default: null, + }, + seller: { + type: mongoose.Schema.Types.ObjectId, + ref: "User", + required: true, + }, + createdAt: { + type: Date, + default: Date.now, + }, + expiresAt: { + type: Date, + required: true, + }, + status: { + type: String, + enum: ["active", "expired", "sold"], + default: "active", + }, + soldTo: { + type: mongoose.Schema.Types.ObjectId, + ref: "User", + default: null, + }, + soldPrice: { + type: Number, + default: 0, + }, +}); + +export default mongoose.model("Auction", auctionSchema); diff --git a/backend/src/models/bid.model.ts b/backend/src/models/bid.model.ts new file mode 100644 index 0000000..1dfb400 --- /dev/null +++ b/backend/src/models/bid.model.ts @@ -0,0 +1,25 @@ +import mongoose from "mongoose"; + +const bidSchema = new mongoose.Schema({ + auctionId: { + type: mongoose.Schema.Types.ObjectId, + ref: "Auction", + required: true, + }, + bidder: { + type: mongoose.Schema.Types.ObjectId, + ref: "User", + required: true, + }, + amount: { + type: Number, + required: true, + min: 0, + }, + createdAt: { + type: Date, + default: Date.now, + }, +}); + +export default mongoose.model("Bid", bidSchema); diff --git a/backend/src/routes/auctionRoutes.ts b/backend/src/routes/auctionRoutes.ts new file mode 100644 index 0000000..5f4f908 --- /dev/null +++ b/backend/src/routes/auctionRoutes.ts @@ -0,0 +1,20 @@ +import express from "express"; +import { + createAuction, + placeBid, + acceptHighestBid, +} from "../controllers/auction.controller"; +import { authenticate, authorizeRole } from "../middleware/authMiddleware"; + +const router = express.Router(); + +// Create a new auction (only sellers) +router.post("/", authenticate, authorizeRole("seller"), createAuction); + +// Place a bid (only buyers) +router.post("/:id/bid", authenticate, authorizeRole("buyer"), placeBid); + +// Seller accepts the highest bid (only sellers) +router.post("/:id/accept", authenticate, authorizeRole("seller"), acceptHighestBid); + +export default router; From a358fb3d9389547845bef5ff88fa4e91203bb481 Mon Sep 17 00:00:00 2001 From: Shubham Kumar <161634026+04shubham7@users.noreply.github.com> Date: Wed, 29 Oct 2025 09:19:07 +0530 Subject: [PATCH 2/3] Update backend/src/models/auction.model.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- backend/src/models/auction.model.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/models/auction.model.ts b/backend/src/models/auction.model.ts index 0d6e596..ce56b61 100644 --- a/backend/src/models/auction.model.ts +++ b/backend/src/models/auction.model.ts @@ -17,7 +17,7 @@ const auctionSchema = new mongoose.Schema({ }, currentHighestBid: { type: Number, - default: 0, + default: function () { return this.startPrice; }, }, highestBidder: { type: mongoose.Schema.Types.ObjectId, From a52b2edfbcbc30119c56c5761f4fbf856bb3f071 Mon Sep 17 00:00:00 2001 From: Shubham Kumar <161634026+04shubham7@users.noreply.github.com> Date: Wed, 29 Oct 2025 09:19:20 +0530 Subject: [PATCH 3/3] Update backend/src/models/bid.model.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- backend/src/models/bid.model.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/models/bid.model.ts b/backend/src/models/bid.model.ts index 1dfb400..b4f3c5c 100644 --- a/backend/src/models/bid.model.ts +++ b/backend/src/models/bid.model.ts @@ -14,7 +14,7 @@ const bidSchema = new mongoose.Schema({ amount: { type: Number, required: true, - min: 0, + min: 1, }, createdAt: { type: Date,