Skip to content

Commit

Permalink
Add sockets and fix stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
dhzdhd committed Nov 2, 2023
1 parent cafb69a commit f00f657
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 152 deletions.
74 changes: 74 additions & 0 deletions backend/config/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,78 @@ module.exports = ({ env }) => ({
},
},
},
// io: {
// enabled: false,
// config: {
// IOServerOptions: {
// cors: {
// origin: "*",
// methods: ["GET", "POST"],
// },
// },
// contentTypes: {
// message: "*",
// chat: "*",
// },
// events: [
// {
// name: "connection",
// handler: ({ strapi }, socket) => {
// strapi.log.info(`[io] new connection with id ${socket.id}`);

// socket.on("loadBids", async (data) => {
// let params = data;

// try {
// let data = await strapi
// .service("api::product.product")
// .loadBids(params.id);
// strapi.$io.emit("loadBids", data);
// } catch (error) {
// console.log(error);
// }
// });

// socket.on("makeBid", async (data) => {
// let params = data;
// try {
// let found = await strapi.entityService.findOne(
// "api::product.product",
// params.product,
// { fields: "bid_price" }
// );

// const account = await strapi
// .service("api::account.account")
// .getUserAccount(socket.user);

// //Check whether user has enough more to make the bid

// if (parseInt(account.balance) >= parseInt(found.bid_price)) {
// await strapi
// .service("api::bid.bid")
// .makeBid({ ...params, account: account.id });
// let product = await strapi
// .service("api::product.product")
// .findAndUpdateBidPrice(found, params.bidValue);
// let updatedProduct = await strapi
// .service("api::product.product")
// .loadBids(product.id);
// strapi.$io.emit("loadBids", updatedProduct);
// } else {
// console.log("Balance Is low");
// }
// } catch (error) {
// console.log(error);
// }
// });

// socket.on("disconnect", () => {
// console.log("user disconnected");
// });
// },
// },
// ],
// },
// },
});
46 changes: 29 additions & 17 deletions backend/src/api/account/services/account.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
'use strict';
"use strict";

/**
* account service.
*/
const { createCoreService } = require('@strapi/strapi').factories;
module.exports = createCoreService('api::account.account', ({ strapi }) => ({
newUser(user_id) {
return strapi.service('api::account.account').create({
data: {
balance: 0, user: user_id
}
});
},
getUserAccount(user_id) {
return strapi.db.query('api::account.account').findOne({
where: { user: user_id },
})
}
}));

const { createCoreService } = require("@strapi/strapi").factories;

module.exports = createCoreService("api::account.account", ({ strapi }) => ({
deposit(id, balance) {
return strapi
.service("api::account.account")
.update(id, { data: { balance } });
},
withdraw(id, balance) {
return strapi
.service("api::account.account")
.update(id, { data: { balance } });
},

newUser(user_id) {
return strapi
.service("api::account.account")
.create({ data: { balance: 0, user: user_id } });
},

getUserAccount(user_id) {
return strapi.db.query("api::account.account").findOne({
where: { user: user_id },
});
},
}));
7 changes: 0 additions & 7 deletions backend/src/api/account/services/account.ts

This file was deleted.

22 changes: 22 additions & 0 deletions backend/src/api/bid/services/bid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";

/**
* bid service.
*/

const { createCoreService } = require("@strapi/strapi").factories;

module.exports = createCoreService("api::bid.bid", ({ strapi }) => ({
makeBid(params) {
return strapi
.service("api::bid.bid")
.create({
data: {
value: params.bidValue,
account: params.account,
product: params.product,
publishedAt: new Date(),
},
});
},
}));
7 changes: 0 additions & 7 deletions backend/src/api/bid/services/bid.ts

This file was deleted.

66 changes: 35 additions & 31 deletions backend/src/api/product/services/product.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
'use strict';
"use strict";

/**
* product service.
*/
const { createCoreService } = require('@strapi/strapi').factories;
module.exports = 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']
}
}
}
}

const { createCoreService } = require("@strapi/strapi").factories;

module.exports = 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"],
},
image: true
},
},
});
},
async findAndUpdateBidPrice(found, price) {
return strapi.entityService.update('api::product.product', found.id, {
data: {
bid_price: parseInt(found.bid_price) + parseInt(price)
},
});
}
}));
},
},

image: true,
},
});
},
async findAndUpdateBidPrice(found, price) {
return strapi.entityService.update("api::product.product", found.id, {
data: {
bid_price: parseInt(found.bid_price) + parseInt(price),
},
});
},
}));
7 changes: 0 additions & 7 deletions backend/src/api/product/services/product.ts

This file was deleted.

Loading

0 comments on commit f00f657

Please sign in to comment.