Exemple #1
0
        public async Task <IngredientModel> Handle(CreateIngredientCommand request, CancellationToken cancellationToken)
        {
            if (await _ingredientHelper.CheckIfIngredientExists(request.Name))
            {
                throw new ArgumentException($"Ingredient of name {request.Name} already exists.");
            }

            var ingredient = _mapper.Map <Ingredient>(request);

            _shoppingListDbContext.Ingredients.Add(ingredient);
            await _shoppingListDbContext.SaveChangesAsync(cancellationToken);

            return(_mapper.Map <IngredientModel>(ingredient));
        }
Exemple #2
0
        public async Task <Guid> Handle(CreateRecipeCommand request, CancellationToken cancellationToken)
        {
            var recipe = _mapper.Map <Recipe>(request);

            foreach (var recipePart in recipe.RecipeParts)
            {
                _shoppingListDbContext.RecipeParts.Add(recipePart);
            }

            _shoppingListDbContext.Recipes.Add(recipe);
            await _shoppingListDbContext.SaveChangesAsync(cancellationToken);

            return(recipe.Id);
        }
        public async Task <Guid> Handle(RegisterCommand request, CancellationToken cancellationToken)
        {
            var user = _mapper.Map <User>(request);

            var hashSalt = PasswordEncryptionUtilities.GenerateSaltedHash(request.Password);

            user.Salt = hashSalt.Salt;
            user.Hash = hashSalt.Hash;

            await _shoppingListDbContext.Users.AddAsync(user, cancellationToken);

            await _shoppingListDbContext.SaveChangesAsync(cancellationToken);

            return(user.Id);
        }
        public async Task <Unit> Handle(DeleteRecipeCommand request, CancellationToken cancellationToken)
        {
            var recipe = await _shoppingListDbContext.Recipes
                         .FindAsync(request.Id);

            if (recipe == null)
            {
                throw new KeyNotFoundException($"{nameof(Recipe)} to delete with ID: {request.Id} was not found in database");
            }

            recipe.IsDeleted = true;
            recipe.DeletedAt = DateTime.Now; // TODO: Maybe do it somewhere in the override SaveChanges function in ShoppingList.Database project?

            await _shoppingListDbContext.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <RecipeModel> Handle(UpdateRecipeCommand request, CancellationToken cancellationToken)
        {
            var recipePartsToRemove = await _shoppingListDbContext.Recipes
                                      .Where(x => x.Id == request.Id)
                                      .SelectMany(x => x.RecipeParts)
                                      .ToListAsync(cancellationToken);

            _shoppingListDbContext.RecipeParts.RemoveRange(recipePartsToRemove);

            var recipe = _mapper.Map <Recipe>(request);

            _shoppingListDbContext.Recipes.Update(recipe);
            _shoppingListDbContext.Entry(recipe).State = EntityState.Modified;
            _shoppingListDbContext.Entry(recipe).Property(x => x.CreatedBy).IsModified = false;
            _shoppingListDbContext.Entry(recipe).Property(x => x.CreatedAt).IsModified = false;

            await _shoppingListDbContext.SaveChangesAsync(cancellationToken);

            return(_mapper.Map <RecipeModel>(recipe));
        }