public static void CheckIfValidItem(UpdateShoppingItemRequest item) { try { if (item == null) { throw new System.Exception("The shopping item object given was null."); } if (item.ItemFor != null && item.ItemFor.Count <= 0) { throw new System.Exception("The shopping item must be created for at least one person"); } } catch (System.Exception ex) { throw new System.Exception("The shopping item object cannot be validated: " + ex.Message, ex); } }
public void UpdateItem(UpdateShoppingItemRequest shoppingRequest) { _connection.Open(); try { var setValues = new List <string>(); if (shoppingRequest.Name != null) { setValues.Add($"\"Name\"='{shoppingRequest.Name}'"); } if (shoppingRequest.Purchased != null) { setValues.Add($"\"Purchased\"={shoppingRequest.Purchased.ToString().ToUpper()}"); } NpgsqlCommand command; NpgsqlDataReader reader; if (setValues.Count > 0) { command = new NpgsqlCommand("UPDATE public.\"ShoppingItem\" " + $"SET {string.Join(", ", setValues)} " + $"WHERE \"Id\" = {shoppingRequest.Id}", _connection); reader = command.ExecuteReader(); while (reader.Read()) { } reader.Close(); } if (shoppingRequest.ItemFor == null || shoppingRequest.ItemFor.Count == 0) { return; } command = new NpgsqlCommand("DELETE FROM public.\"ShoppingItemFor\" " + $"WHERE \"ShoppingItemId\" = {shoppingRequest.Id}", _connection); reader = command.ExecuteReader(); while (reader.Read()) { } reader.Close(); foreach (var peopleId in shoppingRequest.ItemFor) { command = new NpgsqlCommand( "INSERT INTO public.\"ShoppingItemFor\" (\"ShoppingItemId\", \"PersonId\") " + $"VALUES ({shoppingRequest.Id}, {peopleId})", _connection); reader = command.ExecuteReader(); while (reader.Read()) { } reader.Close(); } } catch (System.Exception exception) { throw new System.Exception($"An Error occured while updating the payment (ID: {shoppingRequest.Id})", exception); } finally { _connection.Close(); } }