コード例 #1
0
        UpdateCommentAsync(CommentDTOAdministration comment, CancellationToken cancellationToken = default(CancellationToken))
        {
            comment.Update = DateTime.UtcNow;
            Comment commentForUpdate = _mapper.Map <Comment>(comment);

            _db.Entry(commentForUpdate).Property(c => c.Title).IsModified = true;
            _db.Entry(commentForUpdate).Property(c => c.Text).IsModified  = true;

            _db.Entry(commentForUpdate).Property(c => c.Update).IsModified = true;

            try
            {
                await _db.SaveChangesAsync(cancellationToken);

                return((Result <CommentDTOAdministration>) Result <CommentDTOAdministration>
                       .Ok(comment));
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return((Result <CommentDTOAdministration>) Result <CommentDTOAdministration>
                       .Fail <CommentDTOAdministration>($"Cannot update model. {ex.Message}"));
            }
            catch (DbUpdateException ex)
            {
                return((Result <CommentDTOAdministration>) Result <CommentDTOAdministration>
                       .Fail <CommentDTOAdministration>($"Cannot update model. {ex.Message}"));
            }
        }
コード例 #2
0
        public async void UpdateCommentAsync_PositiveAndNegative_TestAsync()
        {
            var options = new DbContextOptionsBuilder <ApartmentContext>()
                          .UseInMemoryDatabase(databaseName: "UpdateCommentAsync_PositiveAndNegative_TestAsync")
                          .Options;

            using (var context = new ApartmentContext(options))
            {
                context.AddRange(_comments);
                await context.SaveChangesAsync();
            }

            using (var context = new ApartmentContext(options))
            {
                var comment = await context.Comments.AsNoTracking().FirstOrDefaultAsync();

                var service = new CommentAdministrationService(context, _mapper);

                CommentDTOAdministration updateComment = new CommentDTOAdministration()
                {
                    Id    = comment.Id.ToString(),
                    Title = "newTitle",
                    Text  = "newText"
                };

                CommentDTOAdministration failComment = new CommentDTOAdministration()
                {
                    Id    = new Guid().ToString(),
                    Title = "newTitle",
                    Text  = "newText"
                };

                var resultPositive = await service.UpdateCommentAsync(updateComment);

                var resultNegative = await service.UpdateCommentAsync(failComment);

                resultPositive.IsSuccess.Should().BeTrue();
                resultPositive.Data.Title.Should().BeEquivalentTo(updateComment.Title);
                resultPositive.Data.Title.Should().NotBeEquivalentTo(comment.Title);

                resultNegative.IsSuccess.Should().BeFalse();
            }
        }
コード例 #3
0
        UpdateCommentAsync([FromBody, CustomizeValidator]  CommentDTOAdministration comment,
                           CancellationToken cancellationToken = default(CancellationToken))
        {
            if (comment is null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var result = await _service.UpdateCommentAsync(comment, cancellationToken);

                return(result.IsError
                    ? throw new InvalidOperationException(result.Message)
                    : (IActionResult)Ok(result.Data));
            }
            catch (InvalidOperationException ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }