public async Task <String> Register_Post(AccountNewDTO account) { try { if (ModelState.IsValid) { return("ok" + (await handler.Create(account, Request)).acct_AccountID); } else { return("no"); } } catch (Exception ex) { if (ex.Message == ConstantHelper.ALREADY_REQUEST_LOAN || ex.Message == ConstantHelper.KEY_IN_REQUIRED_FIELD || ex.Message == ConstantHelper.INVALID_PIN || ex.Message == ConstantHelper.PIN_EXPIRED || ex.Message == ConstantHelper.PHONE_EXIST || ex.Message == ConstantHelper.EMAIL_EXIST) { Response.StatusCode = 400; } else { Response.StatusCode = 500; } return(ex.Message.ToString()); } }
//-> Create First Time : no account created yet public async Task <AccountViewDTO> Create(AccountNewDTO accountDTO, HttpRequestBase Request) { using (var transaction = db.Database.BeginTransaction()) { try { var tblPin = db.tblPins.FirstOrDefault(x => x.pins_Phone == accountDTO.acct_PhoneNumber && x.pins_isUsed == null && x.pins_Name == accountDTO.pin); if (tblPin == null) { throw new HttpException(_ErrorCode, ConstantHelper.INVALID_PIN); } else { if (DateTime.Now.Subtract(tblPin.pins_Date.Value).Minutes > 3) { throw new HttpException(_ErrorCode, ConstantHelper.PIN_EXPIRED); } } tblAccount eAcc = db.tblAccounts.FirstOrDefault(x => x.acct_PhoneNumber.Trim() == accountDTO.acct_PhoneNumber.Trim()); if (eAcc != null) { throw new HttpException(_ErrorCode, ConstantHelper.PHONE_EXIST); } eAcc = db.tblAccounts.FirstOrDefault(x => x.acct_Email.Trim() == accountDTO.acct_Email.Trim()); if (eAcc != null) { throw new HttpException(_ErrorCode, ConstantHelper.EMAIL_EXIST); } tblPin.pins_isUsed = "Y"; accountDTO = StringHelper.TrimStringProperties(accountDTO); var account = (tblAccount)MappingHelper.MapDTOToDBClass <AccountNewDTO, tblAccount>(accountDTO, new tblAccount()); account.acct_CreatedDate = DateTime.Now; account.acct_Status = "Pending"; db.tblAccounts.Add(account); await db.SaveChangesAsync(); List <sm_doc> documents = await DocumentHelper.SaveUploadFiles(db, ConstantHelper.TABLE_ACCOUNT_ID, account.acct_AccountID, Request);// tmp not useful , just reserve data for using in the furture accountDTO.acct_AccountID = account.acct_AccountID; var loanRequest = await SaveToLoanRequest(accountDTO); transaction.Commit(); await db.Entry(loanRequest).ReloadAsync(); sendmail(loanRequest, account); return(await SelectByID(account.acct_AccountID)); } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } } }
public async Task <IHttpActionResult> Create(AccountNewDTO newDTO) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } return(Ok(await handler.Create(newDTO))); } catch (HttpException ex) { return(BadRequest(ex.Message)); } }
private async Task <tblLoanRequest> SaveToLoanRequest(AccountNewDTO accountDTO) { var loanRequest = new tblLoanRequest(); //db.tblLoanRequests.Add(loanRequest); loanRequest.loan_CreatedDate = DateTime.Now; loanRequest.loan_PayDate = DateTime.Now; var interestRate = 0; switch (accountDTO.payDay) { case 10: loanRequest.loan_PayDate = DateTime.Now.AddDays(10); interestRate = 10; break; case 20: loanRequest.loan_PayDate = DateTime.Now.AddDays(20); interestRate = 15; break; case 30: loanRequest.loan_PayDate = DateTime.Now.AddDays(30); interestRate = 20; break; default: interestRate = 0; break; } loanRequest.loan_AccountID = accountDTO.acct_AccountID; loanRequest.loan_PayDay = accountDTO.payDay; loanRequest.loan_Amount = Decimal.Parse(accountDTO.amount.ToString()); loanRequest.loan_InterestRate = interestRate; loanRequest.loan_InterestAmount = Decimal.Parse((accountDTO.amount * interestRate / 100).ToString()); loanRequest.loan_LoanAmount = Decimal.Parse((Decimal.Parse(accountDTO.amount.ToString()) + loanRequest.loan_InterestAmount).ToString()); loanRequest.loan_Purpose = accountDTO.loan_Purpose; loanRequest.loan_Status = "Pending"; loanRequest.loan_Balance = loanRequest.loan_LoanAmount; loanRequest.loan_PaidAmount = 0; db.tblLoanRequests.Add(loanRequest); await db.SaveChangesAsync(); return(loanRequest); }
//-> Create public async Task <AccountViewDTO> Create(AccountNewDTO newDTO) { /* * using (var transaction = db.Database.BeginTransaction()) * { * var checkRecord = await db.tblAccounts.FirstOrDefaultAsync(x => x.deleted == null && x.status == "Pending" && x.phoneNumber == newDTO.phoneNumber); // check whether itemgroup name exist or not * if (checkRecord != null) * throw new HttpException((int)HttpStatusCode.BadRequest, ConstantHelper.ALREADY_REQUEST_LOAN); * * try * { * newDTO = StringHelper.TrimStringProperties(newDTO); * var record = (tblAccount)MappingHelper.MapDTOToDBClass<AccountNewDTO, tblAccount>(newDTO, new tblAccount()); * record.createdDate = DateTime.Now; * record.status = "Pending"; * db.tblAccounts.Add(record); * await db.SaveChangesAsync(); * * var base64s = new List<string>(); * base64s.Add(newDTO.idCardBase64); * base64s.Add(newDTO.employmentLetterBase64); * base64s.Add(newDTO.bankAccountBase64); * * var doc = await DocumentHelper.SaveUploadImage(db, ConstantHelper.document_ItemTableID, record.id, base64s); * * await SaveToLoanRequest(newDTO.loanRequest, record.id); * * transaction.Commit(); * return await SelectByID(record.id); * } * catch (Exception ex) * { * transaction.Rollback(); * throw new Exception(ex.Message); * } * } */ using (var transaction = db.Database.BeginTransaction()) { var tblPin = db.tblPins.FirstOrDefault(x => x.phoneNumber == newDTO.phoneNumber && x.isUsed == null && x.name == newDTO.pin); if (tblPin == null) { throw new HttpException((int)HttpStatusCode.BadRequest, ConstantHelper.INVALID_PIN); } else { if (DateTime.Now.Subtract(tblPin.date.Value).Minutes > 3) { throw new HttpException((int)HttpStatusCode.BadRequest, ConstantHelper.PIN_EXPIRED); } } try { /* * var tblPin = db.tblPins.FirstOrDefault(x => x.phoneNumber == newDTO.phoneNumber && * x.isUsed == null && x.name == newDTO.pin); * if (tblPin == null) * { * throw new HttpException((int)HttpStatusCode.BadRequest, ConstantHelper.INVALID_PIN); * } * else * { * if (DateTime.Now.Subtract(tblPin.date.Value).Minutes > 3) * { * throw new HttpException((int)HttpStatusCode.BadRequest, ConstantHelper.PIN_EXPIRED); * } * } */ tblPin.isUsed = "Y"; newDTO = StringHelper.TrimStringProperties(newDTO); var account = (tblAccount)MappingHelper.MapDTOToDBClass <AccountNewDTO, tblAccount>(newDTO, new tblAccount()); account.createdDate = DateTime.Now; account.status = "Pending"; db.tblAccounts.Add(account); await db.SaveChangesAsync(); var base64s = new List <string>(); base64s.Add(newDTO.idCardBase64); base64s.Add(newDTO.employmentLetterBase64); base64s.Add(newDTO.bankAccountBase64); var doc = await DocumentHelper.SaveUploadImage(db, ConstantHelper.TABLE_ACCOUNT_ID, account.id, base64s); await SaveToLoanRequest(newDTO.loanRequest, account.id); transaction.Commit(); return(await SelectByID(account.id)); } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } } }