Esempio n. 1
0
        private async Task <IHttpActionResult> UpdateOrderItem([FromBody] OrderingFlatItem flatItem, ModulFunktionsblock modulFunktionsblock)
        {
            var existingOrdering = await orderManagerClient.GetOrdering(flatItem.OrderId);

            if (existingOrdering == null)
            {
                return(BadRequest("Ordering does not exist in DB"));
            }

            var existingItem = existingOrdering.Items.FirstOrDefault(i => i.Id == flatItem.ItemId);

            if (existingItem == null)
            {
                return(BadRequest("OrderItem does not exist in DB"));
            }

            var updateOrderingData  = new UpdateOrderingData();
            var updateOrderItemData = new UpdateOrderItemData();

            // map flatitem back to to ordering & orderitem
            foreach (var field in GetOrderingFieldsInternal(modulFunktionsblock))
            {
                var flatItemProperty = typeof(OrderingFlatItem).GetProperty(field.Name);
                if (flatItemProperty == null)
                {
                    throw new ArgumentOutOfRangeException(field.Name);
                }

                var newVal          = flatItemProperty.GetValue(flatItem);
                var originAttribute = flatItemProperty.GetCustomAttributes(true).OfType <OriginAttribute>().FirstOrDefault();

                if (string.IsNullOrWhiteSpace(originAttribute?.Column) || string.IsNullOrWhiteSpace(originAttribute.Table))
                {
                    continue; // field is not updateable, because it is missing the origin attribute
                }

                switch (originAttribute.Table)
                {
                case nameof(Ordering):
                    EnsureUpdateIsGranted(newVal, existingOrdering, originAttribute.Column, field.IsReadonly);
                    Reflective.SetValue(updateOrderingData, originAttribute.Column, newVal);
                    break;

                case nameof(OrderItem):
                    EnsureUpdateIsGranted(newVal, existingItem, originAttribute.Column, field.IsReadonly);
                    Reflective.SetValue(updateOrderItemData, originAttribute.Column, newVal);
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(originAttribute.Table), $"Unknown table: {originAttribute.Table}");
                }
            }

            var data = new UpdateOrderDetailData
            {
                OrderItem = updateOrderItemData,
                Ordering  = updateOrderingData
            };

            await orderManagerClient.UpdateOrderDetail(data);

            return(StatusCode(HttpStatusCode.NoContent));
        }