Skip to content

Commit

Permalink
Add loadBids and loadProducts services
Browse files Browse the repository at this point in the history
  • Loading branch information
dhzdhd committed Nov 3, 2023
1 parent 57da260 commit 87b3077
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 33 deletions.
15 changes: 8 additions & 7 deletions backend/src/api/bid/services/bid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
}));
38 changes: 22 additions & 16 deletions backend/src/api/product/services/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 23 additions & 4 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand Down Expand Up @@ -82,18 +94,25 @@ 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);
}
});

socket.on("disconnect", () => {
console.log("user disconnected");
clearInterval(interval);
});
});

strapi.io = io;
},
};
2 changes: 2 additions & 0 deletions frontend/src/routes/auth/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
};
</script>

<!-- TODO: Save user id to a writeable through form response -->

<div in:fly|local={{ y: 200 }} class="container" id="container">
<div class="form-container {isSignIn ? 'sign-in-container' : 'sign-up-container'}">
<form method="POST" action={isSignIn ? '?/login' : '?/register'} use:enhance>
Expand Down
27 changes: 21 additions & 6 deletions frontend/src/routes/home/[id=int]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 = () => {
Expand All @@ -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);
</script>
Expand Down Expand Up @@ -77,6 +88,10 @@
</div>
<input bind:value={currentBid} type="number" />
<Button id="bid-btn" text="Make Bid" func={makeBid} />

{#if !product.available}
<h1>Auction ended!</h1>
{/if}
</section>

<style lang="sass">
Expand Down

0 comments on commit 87b3077

Please sign in to comment.