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}"); } } if (flatItem.Status == (int)OrderStatesInternal.Ausgeliehen && !existingItem.Ausleihdauer.Equals(flatItem.Ausleihdauer)) { var user = ControllerHelper.UserDataAccess.GetUser(ControllerHelper.GetCurrentUserId()); updateOrderItemData.InternalComment = updateOrderItemData.InternalComment.Prepend( $"Erwartetes Rückgabedatum von {existingItem.Ausgabedatum.Value.AddDays(existingItem.Ausleihdauer):dd.MM.yyyy} " + $"auf {flatItem.Ausgabedatum.Value.AddDays(flatItem.Ausleihdauer):dd.MM.yyyy} angepasst," + $" {DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss")}, {user.FirstName} {user.FamilyName}"); } var data = new UpdateOrderDetailData { OrderItem = updateOrderItemData, Ordering = updateOrderingData }; await orderManagerClient.UpdateOrderDetail(data); return(StatusCode(HttpStatusCode.NoContent)); }
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)); }