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 @@ }; + +