Skip to content

Commit

Permalink
Tweak makeBid to upsert instead of create
Browse files Browse the repository at this point in the history
  • Loading branch information
dhzdhd committed Nov 3, 2023
1 parent a0bedd3 commit 57da260
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
34 changes: 25 additions & 9 deletions backend/src/api/bid/services/bid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@
import { factories } from "@strapi/strapi";

export default factories.createCoreService("api::bid.bid", ({ strapi }) => ({
makeBid(params) {
return strapi.service("api::bid.bid").create({
data: {
value: params.bidValue,
user: params.user,
product: params.product,
publishedAt: new Date(),
},
});
async makeBid(params) {
const currentBids: any[] = await strapi.entityService.findMany(
"api::bid.bid",
{
filters: { product: params.product },
}
);

if (currentBids.length === 0) {
return strapi.service("api::bid.bid").create({
data: {
value: params.bidValue,
user: params.user,
product: params.product,
publishedAt: new Date(),
},
});
} else {
return strapi.entityService.update("api::bid.bid", currentBids[0].id, {
data: {
value: params.bidValue,
user: params.user,
},
});
}
},

getWinner(params) {
Expand Down
1 change: 1 addition & 0 deletions backend/src/api/product/services/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default factories.createCoreService(
},
});
},

async findAndUpdateBidPrice(found, price) {
return strapi.entityService.update("api::product.product", found.id, {
data: {
Expand Down

0 comments on commit 57da260

Please sign in to comment.