public IHttpActionResult PostVoucher(VoucherModel voucher) { if (!ModelState.IsValid || voucher.OfferID == null || voucher.BuyerID == null) { return(BadRequest(ModelState)); } OfferModel offer = offersService.GetOffer((int)voucher.OfferID); UserModel buyer = usersService.GetUser((int)voucher.BuyerID); if (offer == null || buyer == null) { return(NotFound()); } if (buyer.UserRole != UserRoles.ROLE_CUSTOMER) { return(BadRequest("User's role must be ROLE_CUSTOMER")); } voucher.Offer = offer; voucher.Buyer = buyer; VoucherModel createdVoucher = vouchersService.CreateVoucher(voucher); return(CreatedAtRoute("PostVoucher", new { id = createdVoucher.ID }, createdVoucher)); }
public IHttpActionResult GetOfferModel(int id) { Offer offerModel = offerService.GetOffer(id); if (offerModel == null) { return(NotFound()); } return(Ok(offerModel)); }
public IHttpActionResult PutVoucherModel(int id, Voucher voucherModel) { //if (!ModelState.IsValid) //{ // return BadRequest(ModelState); //} //if (id != voucherModel.id) //{ // return BadRequest(); //} //voucherService.UpdateVoucher(id, voucherModel); //return StatusCode(HttpStatusCode.NoContent); if (!ModelState.IsValid || voucherModel.offerId == null || voucherModel.userId == null) { return(BadRequest(ModelState)); } if (id != voucherModel.id) { return(BadRequest()); } Offer offer = offerService.GetOffer((int)voucherModel.offerId); User buyer = userService.GetUser((int)voucherModel.userId); if (offer == null || buyer == null) { return(NotFound()); } if (buyer.user_role != Models.User.UserRoles.ROLE_CUSTOMER) { return(base.BadRequest("User's role must be ROLE_CUSTOMER")); } voucherModel.offerModel = offer; voucherModel.userModel = buyer; Voucher updatedVoucher = voucherService.UpdateVoucher(id, voucherModel); if (updatedVoucher == null) { return(NotFound()); } return(Ok(updatedVoucher)); }
protected async Task <(Product, decimal)> RetrieveProductAndCost(Guid offerId, Guid pointOfSaleId, decimal quantity) { var offer = await _offSvc.GetOffer(offerId); if (offer is null) { throw new BaristaException("invalid_offer_id", $"The offer with ID '{offerId}' was not found."); } else if (offer.PointOfSaleId != pointOfSaleId) { throw new BaristaException("invalid_offer_id", $"The offer with ID '{offerId}' is not associated with PoS with ID '{pointOfSaleId}'"); } var product = await _prodSvc.GetProduct(offer.ProductId); if (product is null) { throw new BaristaException("invalid_offer_product_id", $"The offer with ID '{offerId}' offers product with ID '{offer.ProductId}' which was not found."); } var saleCostNullable = product.RecommendedPrice ?? offer.RecommendedPrice; if (saleCostNullable is null) { throw new BaristaException("invalid_offer_settings", $"The offer with ID '{offerId}' does not specify the recommended cost and neither does product '{offer.ProductId}'"); } return(product, saleCostNullable.Value *quantity); }
public IHttpActionResult PostBill(BillModel bill) { if (!ModelState.IsValid || bill.OfferID == null || bill.BuyerID == null) { return(BadRequest(ModelState)); } OfferModel offer = offersService.GetOffer((int)bill.OfferID); UserModel buyer = usersService.GetUser((int)bill.BuyerID); if (offer == null || buyer == null) { return(NotFound()); } if (buyer.UserRole != UserRoles.ROLE_CUSTOMER) { return(BadRequest("User's role must be ROLE_CUSTOMER")); } bill.Offer = offer; bill.Buyer = buyer; offersService.UpdateOffer(offer, true); BillModel createdBill = billsService.CreateBill(bill); return(CreatedAtRoute("PostBill", new { id = createdBill.ID }, createdBill)); }
protected async Task EnsureRemovalOfSaleBasedOp(Guid saleId) { var sale = await _accountingService.GetSale(saleId); if (sale is null) { throw new BaristaException("sale_not_found", $"Error retrieving sale with ID {saleId} that supposedly changed state."); } var offer = await _offersService.GetOffer(sale.OfferId); if (offer is null) { _logger.LogInformation($"The sale {saleId} changed state to cancelled but its offer could not be found"); return; } if (!(offer.StockItemId is Guid stockItemId)) { return; } var autoStockOps = await _stockOperationsService.BrowseSaleBasedStockOperations(new BrowseSaleBasedStockOperations { StockItemId = new Guid[] { stockItemId }, SaleId = sale.Id }); if (autoStockOps.TotalResults == 0) { return; } else if (autoStockOps.TotalResults > 1) { throw new BaristaException("multiple_stock_ops_per_one_sale", $"Multiple stock operations yielded by query of stockItemId={stockItemId} and saleId={sale.Id}"); } var autoStockOp = autoStockOps.Items.Single(); var removalOpResult = await _busPublisher.SendRequest(new DeleteSaleBasedStockOperation(autoStockOp.Id)); if (!removalOpResult.Successful) { throw removalOpResult.ToException(); } }
public IHttpActionResult PostBillModel(Bill billModel) { //// ModelState is not enough, we need nonempty offerId and buyerId foreign keys too //if (!ModelState.IsValid) //{ // return BadRequest(ModelState); //} // return CreatedAtRoute("SingleBillById", new { id = billModel.id }, billModel); // they are nullable, although the entity will not be created! FIX THE MODEL if (!ModelState.IsValid || billModel.offerId == null || billModel.userId == null) { return(BadRequest(ModelState)); } Offer offer = offerService.GetOffer((int)billModel.offerId); User buyer = userService.GetUser((int)billModel.userId); if (offer == null || buyer == null) { return(NotFound()); } if (buyer.user_role != Models.User.UserRoles.ROLE_CUSTOMER) { return(base.BadRequest("User's role must be ROLE_CUSTOMER")); } billModel.offerModel = offer; billModel.userModel = buyer; // VAZNO: ovo nije eksplicitno objasnjeno ali sledi iz logike rada // Kada je kreiran racun, koji svakako mora biti povezan sa ponudom, moramo azurirati odredjena polja i u ponudi offerService.UpdateOffer(offer, true); Bill createdBill = billService.CreateBill(billModel); return(CreatedAtRoute("PostBill", new { id = createdBill.id }, createdBill)); }
public async Task <ActionResult <Offer> > GetOffer(Guid id) => Single(await _offersService.GetOffer(id));