public byte[] Process(ActionContext context) { var data = context.IncomingMessage; var sri = SaleRequestInfoParser.Parse(data.RawBytes); var saleOperation = new SaleOperation( machineId: sri.MachineId, clientCardId: sri.ClientCardId, itemsCount: sri.ItemsCount, itemsId: sri.ItemsId, price: sri.Price ); var saleResult = _saleService.Sale(saleOperation); var card = _clientCardRepository.Get(sri.ClientCardId); return(BuildResponse(saleResult, card)); }
//TODO: Review floating points comparisons public OperationResult Sale(SaleOperation sale) { var decimalPrice = new decimal(sale.Price); var machine = _machineRepository.Get(sale.MachineId); var client = _clientCardRepository.Get(sale.ClientCardId); var products = _productRepository.GetMany(sale.ItemsId.Select(_ => (int)_)).ToList(); var errors = new List <OperationError>(); if (client == null) { errors.Add(OperationErrorFactory.FromWellKnowErrors(WellKnowErrors.ClientNotFound)); } if (client != null && client.Credit < decimalPrice) { errors.Add(OperationErrorFactory.FromWellKnowErrors(WellKnowErrors.ClientWithNoEnoughCredit)); } if (products.Count != sale.ItemsCount) { errors.Add(OperationErrorFactory.FromWellKnowErrors(WellKnowErrors.InvalidProduct)); } if (products.Sum(_ => _.Price) != decimalPrice) { errors.Add(OperationErrorFactory.FromWellKnowErrors(WellKnowErrors.InvalidPrice)); } if (products.Count != sale.ItemsCount) { errors.Add(OperationErrorFactory.FromWellKnowErrors(WellKnowErrors.InvalidProduct)); } if (errors.Any()) { return(OperationResult.Failed(errors.ToArray())); } DoTransaction(client, machine, products, decimalPrice); return(OperationResult.Success); }