public async Task ShouldDeleteAnnouncementProperly()
        {
            //Arrange
            var repository   = new Mock <IAnnouncementsRepository>();
            var announcement = new Announcement(new Guid(), new Guid(), string.Empty, string.Empty, string.Empty, new List <string>(),
                                                new List <string>());
            var dateUpdated = new DateTime();

            repository.Setup(x => x.Get(It.IsAny <Guid>())).Returns(announcement);
            repository.Setup(x => x.UpdateAsync(It.IsAny <Announcement>()))
            .Callback <Announcement>(x =>
            {
                dateUpdated  = x.ChangedAt;
                announcement = x;
            })
            .Returns(Task.CompletedTask);

            var deleteAnnouncementHandler = new DeleteAnnouncementHandler(repository.Object);
            var command = new DeleteAnnouncementCommand(new Guid());

            //Act
            await deleteAnnouncementHandler.HandleAsync(command);

            //Assert
            repository.Verify(x => x.Get(It.IsAny <Guid>()), Times.Once);
            repository.Verify(x => x.UpdateAsync(It.IsAny <Announcement>()), Times.Once);
            Assert.That(announcement.IsDeleted, Is.True);
            Assert.That(dateUpdated, Is.EqualTo(announcement.ChangedAt));
        }
Ejemplo n.º 2
0
        public async Task <BaseApiResponse> Delete(DeleteRequest request)
        {
            request.CheckNotNull(nameof(request));
            //判断
            var announcement = _announcementQueryService.Find(request.Id);

            if (announcement == null)
            {
                return(new BaseApiResponse {
                    Code = 400, Message = "没找到该公告"
                });
            }
            //删除
            var command = new DeleteAnnouncementCommand(request.Id);
            var result  = await ExecuteCommandAsync(command);

            if (!result.IsSuccess())
            {
                return(new BaseApiResponse {
                    Code = 400, Message = "命令没有执行成功:{0}".FormatWith(result.GetErrorMessage())
                });
            }
            //添加操作记录
            var currentAdmin = _contextService.GetCurrentAdmin(HttpContext.Current);

            RecordOperat(currentAdmin.AdminId.ToGuid(), "删除公告", request.Id, announcement.Title);

            return(new BaseApiResponse());
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> DeleteAnnouncementAsync([FromBody] DeleteAnnouncementRequest request)
        {
            if (GetAnnouncement(request.AnnouncementId).AuthorId != AuthorizedUser.Id)
            {
                Unauthorized();
            }

            var command = new DeleteAnnouncementCommand(request.AnnouncementId);
            await commandBus.ExecuteAsync(command);

            return(Ok());
        }
        public async Task <BaseApiResponse> Delete(DeleteRequest request)
        {
            request.CheckNotNull(nameof(request));
            //判断
            var announcement = _announcementQueryService.Find(request.Id);

            if (announcement == null)
            {
                return(new BaseApiResponse {
                    Code = 400, Message = "没找到该公告"
                });
            }
            //删除
            var command = new DeleteAnnouncementCommand(request.Id);
            var result  = await ExecuteCommandAsync(command);

            if (!result.IsSuccess())
            {
                return(new BaseApiResponse {
                    Code = 400, Message = "命令没有执行成功:{0}".FormatWith(result.GetErrorMessage())
                });
            }
            return(new BaseApiResponse());
        }
Ejemplo n.º 5
0
 public void Handle(ICommandContext context, DeleteAnnouncementCommand command)
 {
     context.Get <Announcement>(command.AggregateRootId).Delete();
 }