public void CreateOrderItem(DTO.OrderItem createdOrderItem) { CheckHelper.ArgumentNotNull(createdOrderItem, "createdOrderItem"); CheckHelper.ArgumentWithinCondition(createdOrderItem.IsNew(), "Order item is not new."); Container.Get <IValidateService>().CheckIsValid(createdOrderItem); CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in."); CheckHelper.WithinCondition(SecurityService.IsCurrentUserSeller, "Only seller can create order item."); var persistentService = Container.Get <IPersistentService>(); var order = persistentService.GetEntityById <DataAccess.Order>(createdOrderItem.Order.Id); CheckHelper.NotNull(order, "Order does not exist"); CheckHelper.WithinCondition(order.CreatedBy.Id == SecurityService.CurrentUser.Id, "Order is created by another seller."); var doesAnotherOrderItemWithTheSameProductSizeExist = persistentService .GetEntitySet <OrderItem>() .Any(oi => oi.OrderId == order.Id && oi.ProductSizeId == createdOrderItem.ProductSize.Id); if (doesAnotherOrderItemWithTheSameProductSizeExist) { throw new OrderServiceException("Строка заказа с заданным размером товара уже существует в текущем заказе."); } var orderItem = new OrderItem { Price = createdOrderItem.Price, Quantity = createdOrderItem.Quantity, TotalPrice = createdOrderItem.Price * createdOrderItem.Quantity, Active = createdOrderItem.Active }; UpdateOrder(orderItem, createdOrderItem.Order); UpdateProductSize(orderItem, createdOrderItem.ProductSize); orderItem.UpdateTrackFields(Container); persistentService.Add(orderItem); persistentService.SaveChanges(); createdOrderItem.Id = orderItem.Id; createdOrderItem.CreateDate = orderItem.CreateDate; createdOrderItem.CreateUser = orderItem.CreatedBy.GetFullName(); createdOrderItem.ChangeDate = orderItem.ChangeDate; createdOrderItem.ChangeUser = orderItem.ChangedBy.GetFullName(); createdOrderItem.PurchaserPaid = 0m; }
public void UpdateOrderItem(DTO.OrderItem updatedOrderItem) { CheckHelper.ArgumentNotNull(updatedOrderItem, "updatedOrderItem"); CheckHelper.ArgumentWithinCondition(!updatedOrderItem.IsNew(), "Order item is new."); Container.Get <IValidateService>().CheckIsValid(updatedOrderItem); CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in."); CheckHelper.WithinCondition( SecurityService.IsCurrentUserSeller || SecurityService.IsCurrentUserPurchaser, "Only seller and purchaser can change order item."); var persistentService = Container.Get <IPersistentService>(); var order = persistentService.GetEntityById <DataAccess.Order>(updatedOrderItem.Order.Id); CheckHelper.NotNull(order, "Order does not exist"); CheckHelper.WithinCondition( SecurityService.IsCurrentUserPurchaser || IsCurrentUserSellerForOrder(order), "Order is created by another seller."); var doesAnotherOrderItemWithTheSameProductSizeExist = persistentService .GetEntitySet <OrderItem>() .Any(oi => oi.Id != updatedOrderItem.Id && oi.OrderId == order.Id && oi.ProductSizeId == updatedOrderItem.ProductSize.Id); if (doesAnotherOrderItemWithTheSameProductSizeExist) { throw new OrderServiceException("Строка заказа с заданным размером товара уже существует в текущем заказе."); } var orderItem = persistentService.GetEntityById <OrderItem>(updatedOrderItem.Id); CheckHelper.NotNull(order, "Order item does not exist"); if (IsCurrentUserSellerForOrder(order)) { orderItem.Price = updatedOrderItem.Price; orderItem.Quantity = updatedOrderItem.Quantity; orderItem.TotalPrice = updatedOrderItem.TotalPrice; orderItem.Active = updatedOrderItem.Active; UpdateOrder(orderItem, updatedOrderItem.Order); UpdateProductSize(orderItem, updatedOrderItem.ProductSize); } else { updatedOrderItem.Price = orderItem.Price; updatedOrderItem.Quantity = orderItem.Quantity; updatedOrderItem.Active = orderItem.Active; Func <DataAccess.Order, bool> predicate = null; if (SecurityService.IsCurrentUserSeller && !SecurityService.IsCurrentUserPurchaser) { predicate = o => o.CreateUserId == SecurityService.CurrentUser.Id; } updatedOrderItem.Order = Container.Get <IDtoService>().CreateOrder(orderItem.Order, false, predicate); updatedOrderItem.ProductSize = Container.Get <IDtoService>().CreateProductSize(orderItem.ProductSize); } if (SecurityService.IsCurrentUserPurchaser) { orderItem.PurchaserPaid = updatedOrderItem.PurchaserPaid; } else { updatedOrderItem.PurchaserPaid = orderItem.PurchaserPaid; } orderItem.UpdateTrackFields(Container); persistentService.SaveChanges(); }