コード例 #1
0
        public async Task <IActionResult> DeleteMailingGroup([FromRoute][Required] int mailingGroupId)
        {
            Logger.Trace($"Executing '{nameof(DeleteMailingGroup)}'.");

            var userId = HttpContext.GetUserId();

            if (userId == null)
            {
                return(Unauthorized());
            }

            var request = new DeleteMailingGroupRequest(mailingGroupId);

            request.SetUserId(userId.Value);

            try
            {
                var result = await _mediator.Send(request);

                return(result.GetResult());
            }
            catch (Exception ex)
            {
                Logger.Error(ex, $"Unexpected error thrown while executing '{nameof(DeleteMailingGroupHandler)}'.");
                throw;
            }
        }
コード例 #2
0
        public async Task Should_ReturnOK_When_DeleteMailingGroup(int mailingGroupIdToDelete, int userId)
        {
            var cancellationToken = new CancellationToken();

            var databaseContext = Create.MockedDbContextFor <DatabaseContext>();

            databaseContext.Set <MailingGroup>().Add(new MailingGroup()
            {
                Id           = 1,
                SystemUserId = 1,
            });
            databaseContext.Set <MailingGroup>().Add(new MailingGroup()
            {
                Id           = 2,
                SystemUserId = 2,
            });
            databaseContext.Set <EmailAddress>().Add(new EmailAddress()
            {
                Id             = 1,
                MailingGroupId = 1
            });
            databaseContext.Set <EmailAddress>().Add(new EmailAddress()
            {
                Id             = 2,
                MailingGroupId = 1
            });
            databaseContext.Set <EmailAddress>().Add(new EmailAddress()
            {
                Id             = 3,
                MailingGroupId = 2
            });
            databaseContext.Set <EmailAddress>().Add(new EmailAddress()
            {
                Id             = 4,
                MailingGroupId = 2
            });

            databaseContext.SaveChanges();

            var request = new DeleteMailingGroupRequest(mailingGroupIdToDelete);

            request.SetUserId(userId);

            var result = await new DeleteMailingGroupHandler(databaseContext).Handle(request, cancellationToken);

            Assert.Multiple(() =>
            {
                Assert.IsTrue(result.Success);
                Assert.AreEqual(result.StatusCode, HttpStatusCode.OK);
                Assert.IsFalse(databaseContext.EmailAddress.Any(x => x.MailingGroupId == mailingGroupIdToDelete));
                Assert.IsFalse(databaseContext.MailingGroups.Any(x => x.Id == mailingGroupIdToDelete));
            });
        }