public async Task <IActionResult> GetUser(int id) { var userFromRepo = await _repo.GetUser(id); var userToReturn = _mapper.Map <UserForDetailsDto>(userFromRepo); return(Ok(userToReturn)); }
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"); }
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"); }