public IActionResult Update([FromBody] DataContracts.Requests.Slip contract) { var wrappedSlip = ModelFromContract(contract); if (wrappedSlip.HasError) { return(BadRequest(wrappedSlip.ErrorMessage)); } cashRegister.AddPayments(wrappedSlip.Model); return(Ok()); }
private ValidationResult <Slip> ModelFromContract(DataContracts.Requests.Slip contract) { if (contract.Id <= 0) { return("The contract id of a slip to be updated cannot be 0 or negative"); } if (contract.Payments == null) { return("The contract payments cannot be null"); } if (!contract.Payments.Any()) { return("The contract payments cannot be empty"); } if (contract.Payments.Any(p => p.Amount <= 0)) { return("Payment amounts cannot be 0 or negative"); } if (contract.Payments.Any(p => p.Amount % 0.01m != 0)) { return("Payment amounts cannot have a fractional part below the cent"); } IList <Payment> payments = contract.Payments.Select(p => new Payment(p.Id, paymentMethods.Get(p.PaymentMethodId), p.Amount)).ToList(); if (payments.Any(p => p.Method == null)) { return("One or several payment method ids are unknown"); } return(new Slip(contract.Id, DateTime.Now, payments)); }