Example #1
0
        public async Task <IActionResult> Post(CreateUserRequest request, CancellationToken token)
        {
            var user = new ApplicationUser {
                UserName = request.UserName, CurrencyBase = request.CurrencyBase, Email = request.Email
            };
            var result = await _userManager.CreateAsync(user, request.Password);

            if (!result.Succeeded)
            {
                var errorType = result.ToString() switch
                {
                    var passwordCode when passwordCode.ToLowerInvariant().Contains("password") => "PasswordInvalid",
                    var userCode when userCode.ToLowerInvariant().Contains("duplicateuser") => "DuplicateUserName",
                    var invalidUserNameCode when invalidUserNameCode.ToLowerInvariant().Contains("invalidusername") => "InvalidUserName",
                    _ => "UserNotCreated"
                };
                var errors = result.Errors.Select(e => e.Description).ToArray();
                return(BadRequest(new ErrorResponse(errorType, errors)));
            }

            var bearerToken = await _tokenService.GetBearerToken(request.UserName, request.Password);

            if (bearerToken)
            {
                var accountBatchCommand = new AddAccountBatchCommand
                {
                    CreatedBy = Guid.Parse(user.Id),
                    CreatedOn = DateTime.UtcNow,
                    Accounts  = _factory.GeAccountItems(request.Locale, request.CurrencyBase).ToArray()
                };

                await _accountService.AddAccountBatch(accountBatchCommand, token);

                var categoryBatchCommand = new AddCategoryBatchCommand
                {
                    CreatedBy  = Guid.Parse(user.Id),
                    CreatedOn  = DateTime.UtcNow,
                    Categories = _factory.GetCategoryItems(request.Locale).ToArray()
                };

                await _categoryService.AddCategoryBatch(categoryBatchCommand, token);

                return(Ok(new TokenResponse
                {
                    AccessToken = bearerToken.Value,
                    ExpiresIn = _options.TokenLifetime,
                    TokenType = "BearerToken"
                }));
            }

            return(BadRequest(new ErrorResponse(bearerToken.ErrorType, "Can't create access token")));
        }
Example #2
0
 public Task AddAccountBatch(AddAccountBatchCommand command, CancellationToken token)
 => HandlerDispatcher.Invoke <AddAccountBatchHandler, AddAccountBatchCommand>(command, token);