コード例 #1
0
        public async Task <Member> BlacklistMember(BlacklistMemberCommand command)
        {
            if (command == null)
            {
                throw new CustomBadRequestException(ExceptionMessage.NullCommand);
            }

            if (DateTime.Compare(command.StartDate, command.EndDate) > 0)
            {
                throw new CustomBadRequestException(ExceptionMessage.BlacklistInvalidDate);
            }

            if (DateTime.Compare(command.EndDate, DateTime.Today) < 0)
            {
                throw new CustomBadRequestException(ExceptionMessage.BlacklistInvalidEndDate);
            }

            var member = _context.Members
                         .Include(m => m.Blacklists)
                         .SingleOrDefault(m => m.Id == command.MemberId);

            if (member == null)
            {
                throw new CustomNotFoundException(ExceptionMessage.MemberIdNotFound);
            }

            member.Blacklists.Add(new Blacklist
            {
                EndDate   = command.EndDate,
                StartDate = command.StartDate
            });
            await _context.SaveChangesAsync();

            return(member);
        }
コード例 #2
0
        public async Task BlacklistMember_CommandIsNull_ThrowException()
        {
            InjectClassFor(InitializeContext());
            BlacklistMemberCommand command = null;

            var exception = await Assert.ThrowsAsync <CustomBadRequestException>(() => ClassUnderTest.BlacklistMember(command));

            Assert.Equal(ExceptionMessage.NullCommand, exception.Message);
        }
コード例 #3
0
        public async Task BlacklistMember_StartDateEarlierThanEndDate_ThrowException()
        {
            InjectClassFor(InitializeContext());
            var command = new BlacklistMemberCommand
            {
                MemberId  = 1,
                EndDate   = new DateTime(2019, 1, 1),
                StartDate = new DateTime(2020, 1, 1)
            };

            var exception = await Assert.ThrowsAsync <CustomBadRequestException>(() => ClassUnderTest.BlacklistMember(command));

            Assert.Equal(ExceptionMessage.BlacklistInvalidDate, exception.Message);
        }
コード例 #4
0
        public async Task <ActionResult <GetMemberViewModel> > BlacklistMember(int memberId, [FromBody] BlacklistMemberCommand command)
        {
            command.MemberId = memberId;
            var member = await _memberService.BlacklistMember(command);

            var memberViewModel = _mapper.Map <GetMemberViewModel>(member);

            return(CreatedAtAction(nameof(GetById), new { memberId = memberViewModel.Id }, memberViewModel));
        }