public async Task AddAccountUnitTests( AddAccountRequest request, Type exceptionType) { var userDbHandler = new Mock <IUserDbHandler>(); var accountDbHandler = new Mock <IAccountDbHandler>(); userDbHandler.Setup(h => h.GetUsersAsync()).ReturnsAsync( new List <User>() { new User() { Id = 1, Name = "N1", Email = "[email protected]", Salary = 2000, Expenses = 100 }, new User() { Id = 2, Name = "M1", Email = "[email protected]", Salary = 5000, Expenses = 4500 } }); var accountService = new AccountService(accountDbHandler.Object, userDbHandler.Object); if (exceptionType == null) { await accountService.AddAccountAsync(request); accountDbHandler.Verify(a => a.AddAccountAsync(It.IsAny <Account>()), Times.Once); } else { await Assert.ThrowsAsync(exceptionType, () => accountService.AddAccountAsync(request)); accountDbHandler.Verify(a => a.AddAccountAsync(It.IsAny <Account>()), Times.Never); } }
public ValidationResult Validate(AddAccountRequest request) { var error = new List <string>(); if (request == null) { error.Add("Provide a valid request object"); return(new ValidationResult(error)); } var account = request.Account; if (account == null) { error.Add("Provide a valid account object"); return(new ValidationResult(error)); } if (account.Number.Length != 8) { error.Add("Provide a valid account number. 8 digits"); } if (account.Bank == null) { error.Add("Provide a valid bank object"); } if (account.Bank != null && (string.IsNullOrWhiteSpace(account.Bank.Name) || account.Bank.Name.Length > 200)) { error.Add("Provide a valid bank name. 1 - 200 digits"); } if (account.User == null) { error.Add("Provide a valid User object"); } if (account.User != null && (string.IsNullOrWhiteSpace(account.User.Name) || account.User.Name.Length > 200)) { error.Add("Provide a valid user name. 1 - 200 digits"); } if (account.User != null && (string.IsNullOrWhiteSpace(account.User.Username) || account.User.Username.Length > 200)) { error.Add("Provide a valid user username. 1 - 200 digits"); } if (account.User != null && (string.IsNullOrWhiteSpace(account.User.Password) || account.User.Password.Length != 8)) { error.Add("Provide a valid user password. 8 digits"); } //TODO finsh validation, then refactor into seperate validator classes return(error.Any() ? new ValidationResult(error) : new ValidationResult()); }
public async Task <IActionResult> AddAccount([FromBody] AddAccountRequest request) { string method = nameof(AddAccount); LogHandler.LogMethod(LogHandler.EventType.Call, Logger, method, request); SingleResponse <object> response = new SingleResponse <object>(); try { if (!request.CheckValidation(response)) { return(response.ToHttpResponse(Logger, method)); } var account = request.ToEntity(); var existingEntity = await DbContext.GetAccount(account); if (existingEntity != null) { response.ErrorCode = (int)ErrorHandler.ErrorCode.AddRepeatedEntity; return(response.ToHttpResponse(Logger, method)); } DbContext.Add(account); int save = await DbContext.SaveChangesAsync(); if (save == 0) { response.ErrorCode = (int)ErrorHandler.ErrorCode.DbSaveNotDone; return(response.ToHttpResponse(Logger, method)); } var entity_list = new List <Account> { account } .Select(x => new { x.id, x.create_date, x.creator_id, x.accounter_id, x.accounter_name, x.fund_name, x.fund_id, x.name, x.status, x.no }).First(); response.Model = entity_list; response.ErrorCode = (int)ErrorHandler.ErrorCode.OK; } catch (Exception ex) { LogHandler.LogError(Logger, response, method, ex); } return(response.ToHttpResponse(Logger, method)); }
public async Task <IActionResult> Post(Guid id, OpenAccount openAccount) { var request = new AddAccountRequest { CustomerId = id, InitialCredit = openAccount.InitialCredit }; var response = await _mediator.Send(request); return(response.ToActionResult(_ => _.Account)); }
public async Task <AddAccountResponse> AddAccountAsync( AdvertiserAccount account) { var request = new AddAccountRequest { Account = account }; return(await CustomerManagementService.CallAsync((s, r) => s.AddAccountAsync(r), request)); }
public HttpResponseMessage PostAccount(AddAccountRequest request) { var hrm = new HttpResponseMessage(); try { if (string.IsNullOrWhiteSpace(request?.UserSSN) || string.IsNullOrWhiteSpace(request?.AccountType)) { Request.CreateErrorResponse(HttpStatusCode.BadRequest, "UserSSN and AccountType are required."); } else { var users = unitOfWork.GetRepository <User>(); var user = users.Get(x => x.SSN == request.UserSSN).FirstOrDefault(); if (user == null) { //If the user is null then return an error hrm = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No user found with matching SSN."); } else { //If the user exists then create the account var account = new Account() { Balance = request.Balance, Type = request.AccountType }; //TODO: Refactor account to only have a single account mapping instead of a collection var accountMapping = new List <AccountMapping>() { new AccountMapping() { Account = account, User = user } }; account.AccountMappings = accountMapping; var accounts = unitOfWork.GetRepository <Account>(); accounts.Insert(account); //Save the changes unitOfWork.Save(); hrm = Request.CreateResponse(HttpStatusCode.OK); } } } catch (Exception e) { hrm = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.ToString()); } return(hrm); }
public AddAccountResponse AddAccount(AddAccountRequest request) { AddAccountResponse response = new AddAccountResponse(); response.Errors = new List <string>(); if (_accountRepository.GetAccountByEmail(request.Email) != null) { response.Success = false; response.Errors.Add("Email Already exists"); return(response); } _accountRepository.AddAccount(request.Email, request.Password); response.Success = true; return(response); }
public async Task <AddAccountResponse> AddAccountAsync(ApiAuthentication auth, AdvertiserAccount account) { var request = new AddAccountRequest { Account = account, }; try { SetAuthHelper.SetAuth(auth, request); return(await Check().AddAccountAsync(request)); } catch (Exception ex) { Log(new LogEventArgs(ServiceType.CustomerManagement, "AddAccountAsync", ex.Message, new { Request = request }, ex)); } return(null); }
public async Task AddAccountAsync(AddAccountRequest request) { var users = await userDbHandler.GetUsersAsync(); var user = users.FirstOrDefault(u => u.Id == request.UserId); if (user == null) { throw new UserNotFountException(request.UserId); } if (user.Salary - user.Expenses < 1000) { throw new UserIsNotEligibleToCreateAccountException(); } var account = new Account() { UserId = request.UserId, CreationDate = DateTime.UtcNow, IsActive = true }; await accountDbHandler.AddAccountAsync(account); }
public async Task <IActionResult> AddAccount(AddAccountRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState.ValidationState)); } try { await accountService.AddAccountAsync(request); return(Ok()); } catch (UserNotFountException ex) { return(NotFound(ex.Message)); } catch (Exception e) { return(BadRequest(e.Message)); } }
public CreateAccount(AddAccountRequest request) => Request = request;
public JsonResult Post([FromBody] AddAccountRequest request) { return(Json(_accountService.AddAccount(request))); }
public AccountFactory(AddAccountRequest request) => Request = request;
public static CreateAccount Create(AddAccountRequest req) => new CreateAccount(req);