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 PostVoucherModel(Voucher voucher) { //if (!ModelState.IsValid) //{ // return BadRequest(ModelState); //} //VoucherModel createdVoucher = voucherService.CreateVoucher(voucherModel); //return CreatedAtRoute("SingleVoucherById", new { id = createdVoucher.id }, createdVoucher); if (!ModelState.IsValid || voucher.offerId == null || voucher.userId == null) { return(BadRequest(ModelState)); } Offer offer = offerService.GetOffer((int)voucher.offerId); User buyer = userService.GetUser((int)voucher.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")); } voucher.offerModel = offer; voucher.userModel = buyer; Voucher createdVoucher = voucherService.CreateVoucher(voucher); return(CreatedAtRoute("PostVoucher", new { id = createdVoucher.id }, createdVoucher)); }