public async Task <Comment> AddComment(Guid adId, Guid userId, AdCommentRequest adCommentRequest) { var user = await userManager.FindByIdAsync(userId.ToString()) ?? throw new ArgumentNullException(); var ad = await Ads .Where(a => a.Id == adId) .Include(a => a.Comments) .SingleAsync() ?? throw new ArgumentNullException(); var newComment = new Comment { Text = adCommentRequest.Text, AuthorId = user.Id, Author = user, AdId = ad.Id, Ad = ad, CommentTime = DateTime.UtcNow }; ad.Comments.Add(newComment); dbContext.Ads.Update(ad); await dbContext.SaveChangesAsync(); return(newComment); }
public async Task <IActionResult> AddComment(Guid adId, [FromBody] AdCommentRequest adCommentRequest) { try { var newComment = await adManager.AddComment(adId, GetCurrentUserId(), adCommentRequest); return(Ok(mapper.Map <CommentView>(newComment))); } catch (InvalidOperationException ioe) { logger.LogDebug(ioe.Message + "\n" + ioe.StackTrace); return(NotFound("Can't find ad or user")); } catch (ArgumentNullException ane) { logger.LogDebug(ane.Message + "\n" + ane.StackTrace); return(NotFound("Can't find ad ot user")); } catch (Exception ex) { logger.LogDebug(ex.Message + "\n" + ex.StackTrace); return(StatusCode(500)); } }