コード例 #1
0
 private static void ValidateItemsNames(UpdateShoppingListCommand cmd, IList <Error> errorsList)
 {
     if (cmd.Items != null && cmd.Items.Any(i => string.IsNullOrWhiteSpace(i.ItemName)))
     {
         errorsList.Add(new Error(ShoppingListErrors.BlankItemName));
     }
 }
コード例 #2
0
 private static void ValidateItemsNotNull(UpdateShoppingListCommand cmd, IList <Error> errorsList)
 {
     if (cmd.Items == null)
     {
         errorsList.Add(new Error(ShoppingListErrors.ItemsNull));
     }
 }
コード例 #3
0
 private static void ValidateTitle(UpdateShoppingListCommand cmd, IList <Error> errorsList)
 {
     if (string.IsNullOrWhiteSpace(cmd.Title))
     {
         errorsList.Add(new Error(ShoppingListErrors.BlankTitle));
     }
 }
コード例 #4
0
 private static void ValidateId(UpdateShoppingListCommand cmd, IList <Error> errorsList)
 {
     if (cmd.GetId() == null || cmd.GetId() == Guid.Empty)
     {
         errorsList.Add(new Error(ShoppingListErrors.InvalidId));
     }
 }
コード例 #5
0
 private static void ValidateItemsNotRepeated(UpdateShoppingListCommand cmd, IList <Error> errorsList)
 {
     if (cmd.Items != null)
     {
         var hasRepeatedItem = cmd.Items.GroupBy(i => i.ItemName, i => i)
                               .Select(d => (d.Key, d.Count()))
                               .Any(d => d.Item2 > 1);
         if (hasRepeatedItem)
         {
             errorsList.Add(new Error(ShoppingListErrors.RepeatedItems));
         }
     }
 }
コード例 #6
0
 private static void ValidateItemsQuantities(UpdateShoppingListCommand cmd, IList <Error> errorsList)
 {
     if (cmd.Items != null)
     {
         foreach (ShoppingListItem i in cmd.Items)
         {
             if (i.Quantity <= 0)
             {
                 errorsList.Add(new Error(ShoppingListErrors.ItemQuantityZeroOrLess, i.ItemName));
             }
         }
     }
 }
コード例 #7
0
        public static bool Validate(this UpdateShoppingListCommand cmd, out Error[]?errors)
        {
            var errorsList = new List <Error>();

            ValidateId(cmd, errorsList);
            ValidateTitle(cmd, errorsList);
            ValidateItemsNotNull(cmd, errorsList);
            ValidateItemsNames(cmd, errorsList);
            ValidateItemsQuantities(cmd, errorsList);
            ValidateItemsNotRepeated(cmd, errorsList);

            errors = errorsList.Count > 0 ? errorsList.ToArray() : null;
            return(errorsList.Count == 0);
        }