Exemple #1
0
        public async Task <AddDishResponse> AddDish(AddDishRequest addDishRequest)
        {
            AddDishResponse addDishResponse = addDishRequest.IsValid();

            if (addDishResponse.Success)
            {
                try
                {
                    Dish dish = MapToDataBaseModel(addDishRequest);
                    await _dishRepository.Create(dish);

                    addDishResponse.Id = dish.Id.ToString();
                }
                catch (TimeoutException)
                {
                    addDishResponse.SetError(ErrorMessage.TimeoutError);
                }
                catch (Exception ex)
                {
                    addDishResponse.SetError(ex);
                }
            }

            return(addDishResponse);
        }
        public static AddDishResponse IsValid(this AddDishRequest addDishRequest)
        {
            AddDishResponse addDishResponse = new AddDishResponse();

            if (string.IsNullOrEmpty(addDishRequest.Title))
            {
                addDishResponse.SetError(ErrorMessage.TitleParameterNotValid);
                return(addDishResponse);
            }
            if (string.IsNullOrEmpty(addDishRequest.Category))
            {
                addDishResponse.SetError(ErrorMessage.CategoryParameterNotValid);
                return(addDishResponse);
            }
            if (string.IsNullOrEmpty(addDishRequest.SubCategory))
            {
                addDishResponse.SetError(ErrorMessage.SubCategoryParameterNotValid);
                return(addDishResponse);
            }
            if (addDishRequest.Price <= 0)
            {
                addDishResponse.SetError(ErrorMessage.PriceParameterNotValid);
                return(addDishResponse);
            }
            if (addDishRequest.EstimatedTime <= 0)
            {
                addDishResponse.SetError(ErrorMessage.EstimatedTimeParameterNotValid);
                return(addDishResponse);
            }

            foreach (var item in addDishRequest.MealType)
            {
                if (!Enum.IsDefined(typeof(MealType), item))
                {
                    addDishResponse.SetError(ErrorMessage.MealTypeParameterNotValid);
                    return(addDishResponse);
                }
            }
            foreach (var item in addDishRequest.Days)
            {
                if (!Enum.IsDefined(typeof(Day), item))
                {
                    addDishResponse.SetError(ErrorMessage.DayParameterNotValid);
                    return(addDishResponse);
                }
            }
            if (addDishRequest.Ingredients.Count > 0)
            {
                foreach (var item in addDishRequest.Ingredients)
                {
                    if (string.IsNullOrEmpty(item.Key) || string.IsNullOrEmpty(item.Value))
                    {
                        addDishResponse.SetError(ErrorMessage.IngredientsParameterNotValid);
                        return(addDishResponse);
                    }
                }
            }

            return(addDishResponse);
        }