Esempio n. 1
0
        public async Task <IActionResult> AddRecipe(RecipeForAddDto recipeForAddDto)
        {
            var userFromRepo = await _repo.GetUser(int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value));

            var recipeToCreate = _mapper.Map <Recipe>(recipeForAddDto);

            recipeToCreate.DateAdded = DateTime.Now;

            userFromRepo.Recipes.Add(recipeToCreate);

            if (await _repo.SaveAllChanges())
            {
                return(NoContent());
            }

            throw new Exception($"Adding recipe failed on save");
        }
Esempio n. 2
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDto userForUpdateDto)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(id);

            _mapper.Map(userForUpdateDto, userFromRepo);

            if (await _repo.SaveAllChanges())
            {
                return(NoContent());
            }

            throw new Exception($"Updating user {id} failed on save");
        }
Esempio n. 3
0
        public async Task <IActionResult> AddComment(int recipeId, Comment comment)
        {
            var recipeFromRepo = await _repo.GetRecipe(recipeId);

            var userFromRepo = await _repo.GetUser(int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value));

            var commentToAdd = new Comment
            {
                Message   = comment.Message,
                DateAdded = DateTime.Now,
                Recipe    = recipeFromRepo,
                User      = userFromRepo
            };

            recipeFromRepo.Comments.Add(commentToAdd);

            if (await _repo.SaveAllChanges())
            {
                var commentToReturn = _mapper.Map <CommentForRecipeDetailsDto>(commentToAdd);
                return(CreatedAtRoute("GetComment", new { id = commentToAdd.CommentId }, commentToReturn));
            }

            throw new Exception("Something went wrong");
        }