public HttpResponseMessage PostAccount(NewAccountDetailEntity account) { if (!ModelState.IsValid) { Log.Error("Model is in-valid for entry POST api/account"); throw WebExceptionFactory.GetBadRequestError("Model is in valid please check your payload"); } var newAccount = _accountService.CreateAccount(account); Log.Info("Account has been created"); // send response return Request.CreateResponse(HttpStatusCode.Created, newAccount); }
public HttpResponseMessage PostAccount(NewAccountDetailEntity account) { if (!ModelState.IsValid) { Log.Error("Model is in-valid for entry POST api/account"); throw WebExceptionFactory.GetBadRequestError("Model is in valid please check your payload"); } var newAccount = _accountService.CreateAccount(account); Log.Info("Account has been created"); // send response return(Request.CreateResponse(HttpStatusCode.Created, newAccount)); }
public void PostTransactionTest() { var newAccountDetails = new NewAccountDetailEntity { AccountType = "Fixed Deposit", CustomerId = "3", DepositAmount = "1000" }; var accountServiceMock = new Mock <IAccountService>(); accountServiceMock.Setup(ser => ser.CreateAccount(newAccountDetails)).Returns(new AccountEntity { Account_Id = 101, Account_Balance = 1000, Account_Number = "1234-12345", Account_Type = "Fixed Deposit", Customer_Id = 3 }); var controller = new AccountController(accountServiceMock.Object) { Configuration = new HttpConfiguration(), Request = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri($"http://{Localhost}/api/account") } }; controller.Configuration.MapHttpAttributeRoutes(); controller.Configuration.EnsureInitialized(); controller.RequestContext.RouteData = new HttpRouteData(new HttpRoute(), new HttpRouteValueDictionary { { "controller", "Account" } }); var httpResponse = controller.PostAccount(newAccountDetails); var createdAccount = httpResponse.Content.ReadAsAsync <AccountEntity>().Result; Assert.IsNotNull(createdAccount, "Created account was null or empty"); Assert.AreEqual(createdAccount.Account_Id, 101, "Account ID are not same"); Assert.AreEqual(createdAccount.Account_Balance, 1000, "Account balance are not same"); Assert.AreEqual(createdAccount.Customer_Id, 3, "Customer ID are not same"); Assert.AreSame(createdAccount.Account_Number, "1234-12345", "Account number are not same"); Assert.AreSame(createdAccount.Account_Type, "Fixed Deposit", "Account type are not same"); }
/// <summary> /// Create a Account /// </summary> /// <param name="account">Account Object</param> public AccountEntity CreateAccount(NewAccountDetailEntity account) { Mapper.CreateMap<Account, AccountEntity>(); var newAccount = new Account { Account_Balance = decimal.Parse(account.DepositAmount), Customer_Id = CheckCustomer(account.CustomerId), Account_Type = account.AccountType, Account_Number = GetNextAccountNumber() }; try { var createdAccount = _instance.DataContext.Account.Add(newAccount); Commit(); Log.Debug($"New account has been created, account number {newAccount.Account_Number}"); Mapper.CreateMap<Account, AccountEntity>(); return Mapper.Map<AccountEntity>(createdAccount); } catch (InvalidOperationException e) { const string msg = "Error occured while creating account"; throw WebExceptionFactory.GetBadRequestError(msg, e); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Log.Error( $"Entity of type \"{eve.Entry.Entity.GetType().Name}\" in state \"{eve.Entry.State}\" has the following validation errors:"); foreach (var ve in eve.ValidationErrors) { Log.Error($"- Property: \"{ve.PropertyName}\", Error: \"{ve.ErrorMessage}\""); } } throw WebExceptionFactory.GetBadRequestError("Database error", e); } catch (HttpResponseException) { throw; } catch (Exception ex) { throw WebExceptionFactory.GetServerError(ex.Message); } }
/// <summary> /// Create a Account /// </summary> /// <param name="account">Account Object</param> public AccountEntity CreateAccount(NewAccountDetailEntity account) { Mapper.CreateMap <Account, AccountEntity>(); var newAccount = new Account { Account_Balance = decimal.Parse(account.DepositAmount), Customer_Id = CheckCustomer(account.CustomerId), Account_Type = account.AccountType, Account_Number = GetNextAccountNumber() }; try { var createdAccount = _instance.DataContext.Account.Add(newAccount); Commit(); Log.Debug($"New account has been created, account number {newAccount.Account_Number}"); Mapper.CreateMap <Account, AccountEntity>(); return(Mapper.Map <AccountEntity>(createdAccount)); } catch (InvalidOperationException e) { const string msg = "Error occured while creating account"; throw WebExceptionFactory.GetBadRequestError(msg, e); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Log.Error( $"Entity of type \"{eve.Entry.Entity.GetType().Name}\" in state \"{eve.Entry.State}\" has the following validation errors:"); foreach (var ve in eve.ValidationErrors) { Log.Error($"- Property: \"{ve.PropertyName}\", Error: \"{ve.ErrorMessage}\""); } } throw WebExceptionFactory.GetBadRequestError("Database error", e); } catch (HttpResponseException) { throw; } catch (Exception ex) { throw WebExceptionFactory.GetServerError(ex.Message); } }
public void PostTransactionTest() { var newAccountDetails = new NewAccountDetailEntity { AccountType = "Fixed Deposit", CustomerId = "3", DepositAmount = "1000" }; var accountServiceMock = new Mock<IAccountService>(); accountServiceMock.Setup(ser => ser.CreateAccount(newAccountDetails)).Returns(new AccountEntity { Account_Id = 101, Account_Balance = 1000, Account_Number = "1234-12345", Account_Type = "Fixed Deposit", Customer_Id = 3 }); var controller = new AccountController(accountServiceMock.Object) { Configuration = new HttpConfiguration(), Request = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri($"http://{Localhost}/api/account") } }; controller.Configuration.MapHttpAttributeRoutes(); controller.Configuration.EnsureInitialized(); controller.RequestContext.RouteData = new HttpRouteData(new HttpRoute(), new HttpRouteValueDictionary { {"controller", "Account"} }); var httpResponse = controller.PostAccount(newAccountDetails); var createdAccount = httpResponse.Content.ReadAsAsync<AccountEntity>().Result; Assert.IsNotNull(createdAccount, "Created account was null or empty"); Assert.AreEqual(createdAccount.Account_Id, 101, "Account ID are not same"); Assert.AreEqual(createdAccount.Account_Balance, 1000, "Account balance are not same"); Assert.AreEqual(createdAccount.Customer_Id, 3, "Customer ID are not same"); Assert.AreSame(createdAccount.Account_Number, "1234-12345", "Account number are not same"); Assert.AreSame(createdAccount.Account_Type, "Fixed Deposit", "Account type are not same"); }