示例#1
0
        public async Task <IActionResult> Put([FromBody] Comment modifiedComment)
        {
            try
            {
                ModelState.Clear();
                TryValidateModel(modifiedComment);

                foreach (var key in ModelState.Keys)
                {
                    if (key.StartsWith("User") || key.StartsWith("Product"))
                    {
                        ModelState.Remove(key);
                    }
                }

                if (!ModelState.IsValid)
                {
                    CoreFunc.ExtractErrors(ModelState, ref ErrorsList);
                    return(UnprocessableEntity(ErrorsList));
                }

                Comment comment = await _DbContext.Comments
                                  .Include(c => c.User)
                                  .SingleOrDefaultAsync(c => c.Id == modifiedComment.Id && c.User.Id == AppFunc.GetUserId(User)).ConfigureAwait(false);

                if (comment == null)
                {
                    CoreFunc.Error(ref ErrorsList, "Comment not exist.");
                    return(StatusCode(412, ErrorsList));
                }

                comment.Description = modifiedComment.Description;
                comment.Rate        = modifiedComment.Rate;
                await comment.CencoredDescription();

                _DbContext.Comments.Update(comment);
                await _DbContext.SaveChangesAsync().ConfigureAwait(false);

                return(Ok(comment));
            }
            catch (Exception ex)
            {
                CoreFunc.Error(ref ErrorsList, _LoggingService.LogException(Request.Path, ex, User));
                return(StatusCode(417, ErrorsList));
            }
        }