Esempio n. 1
0
        /// <summary>
        /// Gets PizzaModel object by identifier
        /// </summary>
        /// <param name="id">id of pizza</param>
        /// <returns>Single PizzaModel object or list of errors</returns>
        public async Task <IServiceResult <PizzaToReturnDto> > GetByIdAsync(int id)
        {
            try
            {
                // try to get pizza with all child objects
                PizzaModel pizza = await GetPizzaById(id);

                if (pizza != null)
                {
                    PizzaToReturnDto pizzaToReturn = CreatePizzaToReturn(pizza);

                    // return the object if it was found in database
                    return(new ServiceResult <PizzaToReturnDto>(ResultType.Correct, pizzaToReturn));
                }

                // pass error state to controller
                return(new ServiceResult <PizzaToReturnDto>(ResultType.Error, new List <string> {
                    "Cannot load pizza from database"
                }));
            }
            catch (Exception e)
            {
                // catch exception and pass errors to controller
                return(new ServiceResult <PizzaToReturnDto>(ResultType.Error, new List <string> {
                    e.Message
                }));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates new pizza in database
        /// </summary>
        /// <param name="pizzaToCreate">data of pizza to be created</param>
        /// <returns>Pizza data to return or errors that occured during creation (depending on returned ServiceResult state)</returns
        public async Task <IServiceResult <PizzaToReturnDto> > CreateAsync(PizzaToCreateDto pizzaToCreate)
        {
            try
            {
                PizzaModel nameUsed = await GetPizzasByName(pizzaToCreate.Name);

                if (nameUsed == null)
                {
                    PizzaModel createdPizza = await CreatePizza(pizzaToCreate);

                    PizzaToReturnDto pizzaToReturn = CreatePizzaToReturn(createdPizza);

                    return(new ServiceResult <PizzaToReturnDto>(ResultType.Created, pizzaToReturn));
                }

                return(new ServiceResult <PizzaToReturnDto>(ResultType.Error, new List <string> {
                    $"Pizza name: {pizzaToCreate.Name} is already taken"
                }));
            }
            catch (Exception e)
            {
                return(new ServiceResult <PizzaToReturnDto>(ResultType.Error, new List <string> {
                    e.Message
                }));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Pizza stored with name passed by parameter
        /// </summary>
        /// <param name="name">name of pizza</param>
        /// <returns>Pizza to return object with specific name</returns>
        public async Task <IServiceResult <PizzaToReturnDto> > GetByNameAsync(string name)
        {
            try
            {
                // get pizza that have name equal parameter value
                PizzaModel pizza = await GetPizzasByName(name);

                if (pizza != null)
                {
                    // convert it to object to retun
                    PizzaToReturnDto pizzaToReturn = CreatePizzaToReturn(pizza);

                    // return converted pizza objects
                    return(new ServiceResult <PizzaToReturnDto>(ResultType.Correct, pizzaToReturn));
                }

                // pass error if pizzas with this name was not found in database
                return(new ServiceResult <PizzaToReturnDto>(ResultType.Error, new List <string> {
                    $"Pizzas with name: {name} were not found"
                }));
            }
            catch (Exception e)
            {
                // catch exception and pass errors to controller
                return(new ServiceResult <PizzaToReturnDto>(ResultType.Error, new List <string> {
                    e.Message
                }));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Adds ingredient to existing pizza
        /// </summary>
        /// <param name="pizzaName">name of pizza that ingredient will be added for</param>
        /// <param name="ingredientId">identifier of ingredient</param>
        /// <returns>PizzaToReturn object or list of errors</returns>
        public async Task <IServiceResult <PizzaToReturnDto> > AddIngredientAsync(string pizzaName, int ingredientId)
        {
            try
            {
                PizzaModel pizzaToUpdate = await GetPizzasByName(pizzaName);

                // if pizza was found
                if (pizzaToUpdate != null)
                {
                    // check if pizza already have the ingredient to be added
                    bool isIncluded = pizzaToUpdate.PizzaIngredients.SingleOrDefault(x => x.IngredientId == ingredientId) == null ? false : true;

                    if (isIncluded == false)
                    {
                        // take the ingredient from database
                        IngredientModel ingredientToAdd = (await _repository.Ingredients.GetByExpressionAsync(x => x.Id == ingredientId, i => i.Include(p => p.IngredientDetails))).SingleOrDefault();

                        // update all sizes of pizza
                        pizzaToUpdate.PizzaIngredients.Add(new PizzaIngredientsModel {
                            Ingredient = ingredientToAdd, Pizza = pizzaToUpdate
                        });
                        UpdateTotalPizzaPrices(pizzaToUpdate);
                        _repository.Pizzas.Update(pizzaToUpdate);
                        await _repository.SaveChangesAsync();

                        // return new version of pizza object
                        PizzaToReturnDto pizzaToReturn = CreatePizzaToReturn(pizzaToUpdate);

                        return(new ServiceResult <PizzaToReturnDto>(ResultType.Edited, pizzaToReturn));
                    }
                }

                return(new ServiceResult <PizzaToReturnDto>(ResultType.Error, new List <string> {
                    $"Error during addition of new ingredient to {pizzaName} pizza"
                }));
            }
            catch (Exception e)
            {
                return(new ServiceResult <PizzaToReturnDto>(ResultType.Error, new List <string> {
                    e.Message
                }));
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Deletes ingredient from existing pizza
        /// </summary>
        /// <param name="pizzaName">name of pizza that ingredient will be removed from</param>
        /// <param name="ingredientId">identifier of ingredient</param>
        /// <returns>PizzaToReturn object or list of errors</returns>
        public async Task <IServiceResult <PizzaToReturnDto> > DeleteIngredientAsync(string pizzaName, int ingredientId)
        {
            try
            {
                // get pizza to update
                PizzaModel pizzaToUpdate = await GetPizzasByName(pizzaName);

                if (pizzaToUpdate != null)
                {
                    bool isIncluded = pizzaToUpdate.PizzaIngredients.SingleOrDefault(x => x.IngredientId == ingredientId) == null ? false : true;

                    // if pizza contains the ingredient to delete
                    if (isIncluded == true)
                    {
                        pizzaToUpdate.PizzaIngredients.Remove(pizzaToUpdate.PizzaIngredients.SingleOrDefault(x => x.IngredientId == ingredientId));
                        UpdateTotalPizzaPrices(pizzaToUpdate);

                        //update pizza price after deletion of ingredient
                        _repository.Pizzas.Update(pizzaToUpdate);
                        await _repository.SaveChangesAsync();

                        PizzaToReturnDto pizzaToReturn = CreatePizzaToReturn(pizzaToUpdate);

                        // return edited version of pizza object
                        return(new ServiceResult <PizzaToReturnDto>(ResultType.Edited, pizzaToReturn));
                    }
                }

                // ingredient has not been deleted - pass error state to controller
                return(new ServiceResult <PizzaToReturnDto>(ResultType.Error, new List <string> {
                    $"Error during deletion of new ingredient to {pizzaName} pizza"
                }));
            }
            catch (Exception e)
            {
                return(new ServiceResult <PizzaToReturnDto>(ResultType.Error, new List <string> {
                    e.Message
                }));
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Converts PizzaModel object to PizzaToReturnDto
        /// </summary>
        /// <param name="pizza">PizzaModel object to be converted</param>
        /// <returns>Converted pizza object</returns>
        private PizzaToReturnDto CreatePizzaToReturn(PizzaModel pizza)
        {
            List <int> ingredientsToReturn = new List <int>();

            // convert included ingredients
            foreach (PizzaIngredientsModel i in pizza.PizzaIngredients)
            {
                ingredientsToReturn.Add(i.Ingredient.Id);
            }

            // create object to return
            PizzaToReturnDto pizzaToReturn = new PizzaToReturnDto
            {
                Id          = pizza.Id,
                Name        = pizza.Name,
                TotalPrices = new List <PriceToReturnDto>(),
                Ingredients = ingredientsToReturn,
                Category    = Enum.GetName(typeof(PizzaCategory), pizza.Category)
            };

            if (pizza.Photo != null)
            {
                pizzaToReturn.PhotoUrl = pizza.Photo.Url;
            }

            foreach (PizzaDetailsModel p in pizza.PizzaDetails)
            {
                pizzaToReturn.TotalPrices.Add(new PriceToReturnDto
                {
                    Size  = p.Size,
                    Price = p.TotalPrice
                });
            }

            return(pizzaToReturn);
        }