/// <inheritdoc /> public async Task CreateCommentAsync(CommentDomainModel commentDomainModel) { try { _logger.Info($"Looking for good with id: {commentDomainModel.GoodId}"); await this._context.Goods.SingleAsync(g => g.Id == commentDomainModel.GoodId); _logger.Info($"Good with id: {commentDomainModel.GoodId} exists in database."); } catch (InvalidOperationException ex) { throw new ArgumentException($"Good with specified id: {commentDomainModel.GoodId} doesn't exist.", ex); } try { this._commentDomainModelValidator.ValidateAndThrow(commentDomainModel); } catch (ValidationException ex) { throw new ArgumentException($"Error when validating {commentDomainModel}.", ex); } var comment = this._mapper.Map <Comment>(commentDomainModel); _logger.Info("Adding comment to database"); this._context.Comments.Add(comment); _logger.Info("Saving changes to database"); await this._context.SaveChangesAsync(); }
public async Task <IHttpActionResult> Post(CommentDomainModel commentDomainModel) { _logger.Info($"Start process of adding comment to database."); try { await this._commentService.CreateCommentAsync(commentDomainModel); } catch (ArgumentException ex) { _logger.Info(ex, ex.ToString()); return(this.BadRequest(ex.Message)); } catch (Exception ex) { _logger.Error(ex, ex.ToString()); return(this.InternalServerError()); } string messageIfSuccess = $"Comment has been successfully added."; _logger.Info(messageIfSuccess); return(this.Ok(commentDomainModel)); }