From 87b3077ca3356eff94129228684696514a0c7b8b Mon Sep 17 00:00:00 2001 From: dhzdhd Date: Fri, 3 Nov 2023 20:47:12 +0530 Subject: [PATCH] Add loadBids and loadProducts services --- backend/src/api/bid/services/bid.ts | 15 ++++---- backend/src/api/product/services/product.ts | 38 +++++++++++-------- backend/src/index.ts | 27 +++++++++++-- frontend/src/routes/auth/+page.svelte | 2 + .../src/routes/home/[id=int]/+page.svelte | 27 ++++++++++--- 5 files changed, 76 insertions(+), 33 deletions(-) diff --git a/backend/src/api/bid/services/bid.ts b/backend/src/api/bid/services/bid.ts index fda75e4..c7ae86a 100644 --- a/backend/src/api/bid/services/bid.ts +++ b/backend/src/api/bid/services/bid.ts @@ -32,15 +32,16 @@ export default factories.createCoreService("api::bid.bid", ({ strapi }) => ({ } }, - getWinner(params) { - const bids = strapi.service("api::bid.bid").findMany({ - fields: ["user", "value"], - filters: { product: params.product }, - sort: { value: "DESC" }, + async loadBids(id) { + const bids = await strapi.entityService.findMany("api::bid.bid", { + filters: { product: id }, + populate: { user: true }, }); - console.log(bids); + const response = { value: bids[0].value, user: bids[0].user.id }; - return bids; + console.log(response); + + return response; }, })); diff --git a/backend/src/api/product/services/product.ts b/backend/src/api/product/services/product.ts index 2dbbf1a..160649a 100644 --- a/backend/src/api/product/services/product.ts +++ b/backend/src/api/product/services/product.ts @@ -7,28 +7,34 @@ import { factories } from "@strapi/strapi"; export default factories.createCoreService( "api::product.product", ({ strapi }) => ({ - loadBids(id) { - return strapi.entityService.findOne("api::product.product", id, { - fields: "*", - populate: { - bids: { - limit: 5, - sort: "createdAt:desc", - populate: { - account: { - fields: ["id"], - populate: { - user: { - fields: ["username"], + async loadProducts(id) { + let product = await strapi.entityService.findOne( + "api::product.product", + id, + { + fields: "*", + populate: { + bids: { + limit: 5, + sort: "createdAt:desc", + populate: { + account: { + fields: ["id"], + populate: { + user: { + fields: ["username"], + }, }, }, }, }, + + image: true, }, + } + ); - image: true, - }, - }); + return product; }, async findAndUpdateBidPrice(found, price) { diff --git a/backend/src/index.ts b/backend/src/index.ts index 4552952..5360043 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -22,12 +22,15 @@ module.exports = { verify(token) */ + let interval; + var io = require("socket.io")(strapi.server.httpServer, { cors: { origin: "*", methods: ["GET", "POST"], }, }); + io.use(async (socket, next) => { try { //Socket Authentication @@ -44,15 +47,24 @@ module.exports = { }).on("connection", function (socket) { console.log("a user connected"); - socket.on("loadBids", async (data) => { + if (interval) { + clearInterval(interval); + } + + interval = setInterval(() => { + io.emit("serverTime", { time: new Date().getTime() }); + }, 1000); + + socket.on("loadProducts", async (data) => { // id: product id let params = data; try { let data = await strapi .service("api::product.product") - .loadBids(params.id); - io.emit("loadBids", data); + .loadProducts(params.id); + + io.emit("loadProducts", data); } catch (error) { console.log(error); } @@ -82,9 +94,14 @@ module.exports = { let updatedProduct = await strapi .service("api::product.product") + .loadProducts(product.id); + + let updatedBid = await strapi + .service("api::bid.bid") .loadBids(product.id); - io.emit("loadBids", updatedProduct); + io.emit("loadProducts", updatedProduct); + io.emit("loadBids", updatedBid); } catch (error) { console.log(error); } @@ -92,8 +109,10 @@ module.exports = { socket.on("disconnect", () => { console.log("user disconnected"); + clearInterval(interval); }); }); + strapi.io = io; }, }; diff --git a/frontend/src/routes/auth/+page.svelte b/frontend/src/routes/auth/+page.svelte index ce0d153..cfff478 100644 --- a/frontend/src/routes/auth/+page.svelte +++ b/frontend/src/routes/auth/+page.svelte @@ -22,6 +22,8 @@ }; + +
diff --git a/frontend/src/routes/home/[id=int]/+page.svelte b/frontend/src/routes/home/[id=int]/+page.svelte index 991c8d0..6042b09 100644 --- a/frontend/src/routes/home/[id=int]/+page.svelte +++ b/frontend/src/routes/home/[id=int]/+page.svelte @@ -10,6 +10,11 @@ let product = data.product; let socket: Socket; let currentBid: number = 0; + let isWinner: boolean = false; + + const time = moment.parseZone(product.auctionEnd, moment.ISO_8601); + const now = moment(); + let remaining = moment.duration(time.diff(now)); const updateProduct = (data: any) => { const newProduct = { @@ -23,6 +28,11 @@ const calcTime = () => { const now = moment(); remaining = moment.duration(time.diff(now)); + + if (now.isSameOrAfter(time)) { + socket.disconnect(); + product.available = false; + } }; const makeBid = () => { @@ -37,17 +47,18 @@ onMount(() => { socket = socketIOClient(CMS_URL, { query: { token: data.token } }); - socket.emit('loadBids', { id: product.id }); - socket.on('loadBids', (data: any) => { + socket.emit('loadProducts', { id: product.id }); + socket.on('loadProducts', (data: any) => { console.log(data); updateProduct(data); }); - }); - const time = moment.parseZone(product.auctionEnd, moment.ISO_8601); - const now = moment(); - let remaining = moment.duration(time.diff(now)); + socket.on('loadBids', (data: any) => { + isWinner = Number(data.user) === 0; + // TODO: Get user id from writable made in login page + }); + }); setInterval(calcTime, 1000); @@ -77,6 +88,10 @@