Example #1
0
        public virtual async Task <int> Add(TCreateDto createDto)
        {
            var entity = DtoMapper.CreateEntity(createDto);

            _repository.Add(entity);
            return(await _dbContextProvider.SaveChangesAsync());
        }
        public override async Task <int> Add(CreatePreparationStepDto createDto)
        {
            var preparationStep = DtoMapper.CreateEntity(createDto);
            var maxOder         = await Repository.GetMaxOrder(createDto.RecipeId);

            preparationStep.Order = (byte)(maxOder + 1);

            Repository.Add(preparationStep);
            return(await DbContextProvider.SaveChangesAsync());
        }
Example #3
0
        public async Task <int> AddFull(CreateRecipeDto createDto)
        {
            var recipe = new Recipe
            {
                Name        = createDto.Name,
                Description = createDto.Description
            };

            Repository.Add(recipe);

            await DbContextProvider.SaveChangesAsync();

            // Add ingredients
            foreach (var recipeIngredientDto in createDto.Ingredients)
            {
                var ingredientDto = await _ingredientsService.GetOrCreate(recipeIngredientDto.Name);

                var recipeIngredient = new RecipeIngredient
                {
                    IngredientId = ingredientDto.Id,
                    Amount       = recipeIngredientDto.Amount,
                    Unit         = recipeIngredientDto.Unit
                };

                recipe.RecipeIngredients.Add(recipeIngredient);
            }

            // Add preperation steps
            foreach (var preparationStepDto in createDto.PreparationSteps)
            {
                var preparationStep = new PreparationStep
                {
                    Description = preparationStepDto.Description
                };

                recipe.PreparationSteps.Add(preparationStep);
            }

            return(await DbContextProvider.SaveChangesAsync());
        }
Example #4
0
        public async Task <IngredientDto> GetOrCreate(string name)
        {
            var ingredient = await Repository.GetByName(name);

            if (ingredient == null)
            {
                var newIngredient = new Ingredient
                {
                    Name = name
                };

                Repository.Add(newIngredient);

                await DbContextProvider.SaveChangesAsync();

                ingredient = await Repository.GetSingle(newIngredient.Id);
            }

            var ingredientDto = DtoMapper.ToDto(ingredient);

            return(ingredientDto);
        }
        public override async Task <int> Delete(int id)
        {
            var toDelete = await Repository.GetSingleOrDefault(id);

            if (toDelete == null)
            {
                return(0);
            }

            var changes = await base.Delete(id);

            // Update order of other preperation steps
            var preparationSteps = await Repository.GetByRecipe(toDelete.RecipeId, true);

            for (int i = 0; i < preparationSteps.Length; i++)
            {
                preparationSteps[i].Order = (byte)(i + 1);
            }

            changes = changes + await DbContextProvider.SaveChangesAsync();

            return(changes);
        }
        public async Task <int> SwitchOrder(int fromId, int toId)
        {
            // Get from and to entities
            var from = await Repository.GetSingleOrDefault(fromId, true);

            var to = await Repository.GetSingleOrDefault(toId, true);

            // Return 0 when any of the entities does not exist
            if (from == null || to == null)
            {
                return(0);
            }

            // Make sure both are part of the same recipe
            if (from.RecipeId != to.RecipeId)
            {
                throw new InvalidOperationException("Preparation steps must be part of the same recipe.");
            }

            var changes = 0;

            var fromOrder = from.Order;
            var toOrder   = to.Order;

            // Switch the order
            to.Order = 0;
            changes  = changes + await DbContextProvider.SaveChangesAsync();

            from.Order = toOrder;
            changes    = changes + await DbContextProvider.SaveChangesAsync();

            to.Order = fromOrder;
            changes  = changes + await DbContextProvider.SaveChangesAsync();

            return(changes);
        }