// POST entries/{entryId}/comment // Comment on an entry. public async Task <ActionResult> Comment([FromBody] EntryCommentDto entryCommentDto) { var currentUser = await _userService.GetUserAsync(User); // Get the entry. var entry = await _entryService.GetEntryAsync(currentUser, entryCommentDto.EntryId); if (entry == null) { _logger.LogInformation($"Comment action called with entry id of {entryCommentDto.EntryId} which was not valid when being requested by user id of {currentUser.Id}"); return(new BadRequestResult()); } // Validate the dto. if (entryCommentDto.Author == null) { _logger.LogInformation($"Comment action called with entry id of {entryCommentDto.EntryId} by user id {currentUser.Id} with an empty author"); return(new BadRequestResult()); } if (entryCommentDto.Author.Id != currentUser.Id) { _logger.LogInformation($"Comment action called with entry id of {entryCommentDto.EntryId} by user id {currentUser.Id} with a different author id of {entryCommentDto.Author.Id}"); return(new BadRequestResult()); } if (string.IsNullOrWhiteSpace(entryCommentDto.Comment)) { _logger.LogInformation($"Comment action called with entry id of {entryCommentDto.EntryId} by user id {currentUser.Id} with an empty comment"); return(new BadRequestResult()); } entryCommentDto.CreatedAt = DateTime.UtcNow; var newId = _entryService.CreateComment(entryCommentDto); return(Json(newId)); }
public int CreateComment(EntryCommentDto entryCommentDto) { var entryAggregate = _repository.GetById <EntryAggregate>(entryCommentDto.EntryId); var newId = entryAggregate.CreateComment(entryCommentDto.Comment, entryCommentDto.Author.Id, entryCommentDto.CreatedAt); _repository.Save(entryAggregate, commitId: Guid.NewGuid(), updateHeaders: null); return(newId); }