Untitled
unknown
csharp
a year ago
5.8 kB
4
Indexable
Never
[HttpPut("{id}")] public async Task<ActionResult<Data.Dtos.Order.OrderDto>> PutOrder(int id, OrderUpdateRequestDto updateOrderDto) { if (id != updateOrderDto.Id) { return BadRequest(); } bool hasOrderedZero = updateOrderDto.Items .Any(i => i.Count == 0); if (hasOrderedZero) { return BadRequest(); } var orderResponse = await _orderCluster.GetOrder(id, updateOrderDto.Region); if (orderResponse == null) { return NotFound(); } var userResponse = await _productCluster.GetUser(orderResponse.User.Id, orderResponse.Region.Id); if (userResponse == null) { return NotFound(); } var productCodes = updateOrderDto.Items .Select(i => i.Code) .Distinct() .ToList(); var products = await _productCluster.GetProducts(productCodes, updateOrderDto.Region); if (products == null) { return NotFound(); } if (products.Count < productCodes.Count) { return BadRequest(); } foreach (var orderedItem in updateOrderDto.Items) { var product = products.Where(p => p.Code == orderedItem.Code).First(); var orderedProduct = orderResponse.Items .Where(i => i.Code == orderedItem.Code) .Where(i => i.Specification.Id == orderedItem.Feature) .FirstOrDefault(); int previouslyOrderedCount = orderedProduct == null ? 0 : orderedProduct.Count; if (orderedItem.Count > product.Features[orderedItem.Feature].Stock + previouslyOrderedCount) { return BadRequest(); } } List<Data.Dtos.Order.Cluster.ItemDto> items = new(); foreach (var orderedItem in updateOrderDto.Items) { if (orderedItem == null) { Console.WriteLine("null"); } var product = products.Where(p => p.Code == orderedItem.Code).First(); var orderedProduct = orderResponse.Items .Where(i => i.Code == orderedItem.Code) .Where(i => i.Specification.Id == orderedItem.Feature) .FirstOrDefault(); int previouslyOrderedCount = orderedProduct == null ? 0 : orderedProduct.Count; int difference = previouslyOrderedCount - orderedItem.Count; var comments = product.Comments.Select(_mapper.Map<Comment>).ToList(); var features = product.Features.Select(_mapper.Map<ProductFeatureDto>).ToList(); features[orderedItem.Feature].Stock += difference; var productUpdateRequest = new ProductFragmentUpdateRequest { Code = product.Code, Description = product.Description, Comments = comments, Manufacturer = product.Manufacturer, Name = product.Name, Photos = product.Photos, Price = product.Price, Region = product.Region.Id, Type = product.Type.Id, Features = features, }; await _productCluster.UpdateProduct(productUpdateRequest); var item = new Data.Dtos.Order.Cluster.ItemDto( orderedItem.Code, orderedItem.Count, product.Name, product.Price, new SpecificationDto( orderedItem.Feature, product.Features[orderedItem.Feature].Color, product.Features[orderedItem.Feature].Material, product.Features[orderedItem.Feature].Size)); items.Add(item); } var orderRequest = new ClusterOrderUpdateRequestDto( updateOrderDto.Id, updateOrderDto.DeliveryOption, updateOrderDto.OrderStatus, updateOrderDto.PaymentDate, updateOrderDto.Region, updateOrderDto.Country, updateOrderDto.City, updateOrderDto.Address, items); await _orderCluster.UpdateOrder(orderRequest); var updatedOrderResponse = await _orderCluster.GetOrder(updateOrderDto.Id, updateOrderDto.Region); var order = new Order { Id = updatedOrderResponse.Id, DeliveryOption = updatedOrderResponse.DeliveryOption, OrderDate = updatedOrderResponse.OrderDate, PaymentDate = updatedOrderResponse.PaymentDate, Region = updatedOrderResponse.Region, Country = updatedOrderResponse.Country, City = updatedOrderResponse.City, Address = updatedOrderResponse.Address, Items = updatedOrderResponse.Items, OrderStatus = updatedOrderResponse.OrderStatus, TotalPrice = updatedOrderResponse.TotalPrice, User = _mapper.Map<Data.Entities.User>(userResponse) }; return Ok(_mapper.Map<Data.Dtos.Order.OrderDto>(order)); }