コード例 #1
0
        public async Task <bool> IsMemberExisted(string email)
        {
            if (string.IsNullOrWhiteSpace(email))
            {
                var error = "Email is required.";
                throw new BusinessException(error, BusinessErrors.BadRequest(error));
            }
            var member = await _memberDataAccessor.GetByEmail(email);

            return(member != null);
        }
コード例 #2
0
        public AccountStatusIds?Convert(string accountStatus, ResolutionContext context)
        {
            if (accountStatus == null)
            {
                return(null);
            }
            if (!Enum.TryParse(accountStatus.Trim(), true, out AccountStatusIds accountStatusId))
            {
                var error = "Invalid account status";
                throw new BusinessException(error, BusinessErrors.BadRequest(error));
            }

            return(accountStatusId);
        }
コード例 #3
0
        public async Task GivenGlobalExceptionHandler_WhenBusinessExceptionRaised_IfErrorIsBadRequest_ShouldSendBadRequest()
        {
            // assign
            var context    = new DefaultHttpContext();
            var parameters = new object[] { };
            var ex         = new BusinessException("Some Exception", BusinessErrors.BadRequest("Bad Request"), parameters);

            _requestDelegate.Invoke(Arg.Any <HttpContext>()).Returns(callInfo => throw ex);

            // act
            await _handler.Invoke(context);

            // assert
            AssertLogging(LogLevel.Warning, ex.Message, null);
            await AssertHttpResponse(context, HttpStatusCode.BadRequest, ex.BusinessErrorMessage);
        }
コード例 #4
0
        public async Task GivenGlobalExceptionHandler_WhenExceptionRaised_IfInnerExceptionIsBadRequestBusinessException_ShouldSendBadRequest()
        {
            // assign
            var context           = new DefaultHttpContext();
            var businessException = new BusinessException("Some Bad Request",
                                                          BusinessErrors.BadRequest("Bad Request"));
            var ex = new Exception("Some Exception", businessException);

            _requestDelegate.Invoke(Arg.Any <HttpContext>()).Returns(callInfo => throw ex);

            // act
            await _handler.Invoke(context);

            // assert
            AssertLogging(LogLevel.Warning, businessException.Message, null);
            await AssertHttpResponse(context, HttpStatusCode.BadRequest,
                                     businessException.BusinessErrorMessage);
        }
コード例 #5
0
        public Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next)
        {
            var errors = _validators
                         .Select(v => v.Validate(request))
                         .SelectMany(v => v.Errors)
                         .Where(e => e != null)
                         .Select(e => e.ErrorMessage)
                         .ToList();

            if (errors.Any())
            {
                var builder = new StringBuilder();
                errors.ForEach(e => builder.AppendLine(e));
                var errorMessage = builder.ToString();
                throw new BusinessException(errorMessage, BusinessErrors.BadRequest(errorMessage));
            }
            return(next());
        }
コード例 #6
0
        public async Task <SimpleResponse <Account> > Handle(SignUpAccountCommand request, CancellationToken cancellationToken)
        {
            var member = await _memberService.GetMemberById(request.MemberId);

            if (member == null)
            {
                throw new BusinessException(
                          "Member {memberId} is invalid.",
                          BusinessErrors.BadRequest("Invalid member details."),
                          request.MemberId);
            }
            var account = await _accountService.SignUpAccount(member);

            await _userUnitOfWork.SaveChangesAsync(cancellationToken);

            account = await _accountService.GetAccountById(account.Id);

            return(SimpleResponse <Account> .Create(account));
        }
コード例 #7
0
        public async Task <Member> SignUpMember(Member member)
        {
            if (!member.IsNew)
            {
                throw new BusinessException("Member {memberId} is not marked as new.",
                                            BusinessErrors.Critical("Member is not marked as new."),
                                            member.Id);
            }
            var memberExisted = await IsMemberExisted(member.Email);

            if (memberExisted)
            {
                var error = $"Cannot sign up member for email {member.Email}.";
                throw new BusinessException(error, BusinessErrors.BadRequest(error));
            }
            await _memberDataAccessor.Create(member);

            return(member);
        }
コード例 #8
0
        public async Task <Account> SignUpAccount(Member member)
        {
            var strategy = await _accountSignUpStrategyDataAccessor.GetDefaultStrategy();

            var result = _accountSignUpEvaluator.Evaluate(member, strategy);

            if (!result.IsSuccess)
            {
                throw new BusinessException("Sign up account failed for member {memberId}. {error}",
                                            BusinessErrors.BadRequest(result.ReasonPhase),
                                            member.Id,
                                            result.ReasonPhase);
            }
            var accountNumberSeq = await _accountDataAccessor.GetNextAccountNumberSeq();

            var accountNumber = $"ZIP{accountNumberSeq:D10}";
            var account       = _accountFactory.Create(accountNumber, member.Id);
            await _accountDataAccessor.Create(account);

            return(account);
        }