Exemplo n.º 1
0
        public async Task <IActionResult> DeleteRecipe(int recipeId)
        {
            var userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            var recipeFromRepo = await _repo.GetRecipe(recipeId);

            if (userId != recipeFromRepo.UserId)
            {
                return(Unauthorized());
            }

            _repo.Delete(recipeFromRepo);

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

            throw new Exception("Error deleting the message");
        }
Exemplo n.º 2
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");
        }