Ejemplo n.º 1
0
        public ActionResult Post([FromBody] Comment comment)
        {
            if (string.IsNullOrWhiteSpace(comment.Name))
            {
                ModelState.AddModelError("name", "пустое поле имени");
            }

            if (string.IsNullOrWhiteSpace(comment.Text))
            {
                ModelState.AddModelError("text", "пустое поле комментария");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.Comments.Add(comment);

            try
            {
                db.SaveChanges();
                _logger.LogAddCommentToDB(comment);

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);

                return(BadRequest());
            }
        }