/* * public async Task<AccountViewDTO> reupload(int id, HttpRequestBase Request) * { * using (var transaction = db.Database.BeginTransaction()) * { * try * { * * List<sm_doc> documents = await DocumentHelper.SaveUploadFiles(db, ConstantHelper.TABLE_ACCOUNT_ID, id, Request);// tmp not useful , just reserve data for using in the furture * return null; * } * catch (Exception ex) * { * transaction.Rollback(); * throw new Exception(ex.Message); * } * } * } */ public async Task <AccountViewDTO> reupload(AccountReuploadDocumentDTO reuploadDoc, HttpRequestBase Request) { using (var transaction = db.Database.BeginTransaction()) { try { List <sm_doc> documents = await DocumentHelper.SaveUploadFiles(db, ConstantHelper.TABLE_ACCOUNT_ID, reuploadDoc.id, Request);// tmp not useful , just reserve data for using in the furture IQueryable <sm_doc> query = from d in db.sm_doc where d.docs_Deleted == null && d.docs_TableID == ConstantHelper.TABLE_ACCOUNT_ID && d.docs_Value == reuploadDoc.id.ToString() orderby d.docs_docID select d; List <sm_doc> records = await query.Take(3).ToListAsync(); int id = 0; if (reuploadDoc.idCard_change == 1) { id = records[0].docs_docID; var tmp = await db.sm_doc.FirstOrDefaultAsync(x => x.docs_Deleted == null && x.docs_docID == id); tmp.docs_Deleted = "1"; await db.SaveChangesAsync(); } if (reuploadDoc.employmentLetter_change == 1) { id = records[1].docs_docID; var tmp = await db.sm_doc.FirstOrDefaultAsync(x => x.docs_Deleted == null && x.docs_docID == id); tmp.docs_Deleted = "1"; await db.SaveChangesAsync(); } if (reuploadDoc.bankAccount_change == 1) { id = records[2].docs_docID; var tmp = await db.sm_doc.FirstOrDefaultAsync(x => x.docs_Deleted == null && x.docs_docID == id); tmp.docs_Deleted = "1"; await db.SaveChangesAsync(); } transaction.Commit(); return(null); } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } } }
//-> Create public async Task <LoanRequestViewDTO> Create(LoanRequestNewDTO loanRequestDTO) { IQueryable <tblLoanRequest> loanRequestQuery = from l in db.tblLoanRequests where l.loan_Deleted == null select l; int countLoanRequest = await loanRequestQuery.CountAsync(); if (countLoanRequest > 1) { throw new HttpException((int)HttpStatusCode.BadRequest, ConstantHelper.ALREADY_REQUEST_LOAN); } loanRequestDTO = StringHelper.TrimStringProperties(loanRequestDTO); var loanRequest = (tblLoanRequest)MappingHelper.MapDTOToDBClass <LoanRequestNewDTO, tblLoanRequest>(loanRequestDTO, new tblLoanRequest()); loanRequest.loan_CreatedDate = DateTime.Now; loanRequest.loan_PayDate = DateTime.Now; loanRequest.loan_Status = "Pending"; //loanRequest.loanAmount = Decimal.Parse((LoanRequestCalculation(loanRequestDTO)).ToString()); loanRequest = LoanRequestCalculation(loanRequestDTO, loanRequest); db.tblLoanRequests.Add(loanRequest); await db.SaveChangesAsync(); db.Entry(loanRequest).Reload(); return(await SelectByID(loanRequest.loan_LoanRequestID)); }
//-> SaveUploadFiles public static async Task <List <sm_doc> > SaveUploadFiles(LoanEntities db, int tableID, int recordID, HttpRequestBase Request) { var files = Request.Files; List <sm_doc> documents = new List <sm_doc>(); if (files != null) { for (int i = 0; i < files.Count; i++) { if (files[i].ContentLength == 0) { continue; } string pathForSavingToDB = "", imageNameForSavingToDB = ""; string path = ""; string uploadFolderName = "uploads"; path = HttpContext.Current.Server.MapPath(@"~\" + uploadFolderName); path = System.Configuration.ConfigurationManager.AppSettings["mediaURL"] + @"\" + uploadFolderName; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string currentYear = DateTime.Now.Year.ToString(); path += @"\" + currentYear; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string currentMonth = DateTime.Now.Month > 9 ? DateTime.Now.Month.ToString() : "0" + DateTime.Now.Month; path += @"\" + currentMonth; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var createImageUniqueName = tableID + "_" + recordID + "_" + DateTime.Now.ToString("yyyy-MM-dd_HHmmssfff") + "_" + i + ".jpg";//in order to avoid redundant image path , use i (variable) files[i].SaveAs(path + @"\" + createImageUniqueName); pathForSavingToDB = uploadFolderName + "/" + currentYear + "/" + currentMonth + "/" + createImageUniqueName; var document = new sm_doc(); document.docs_Name = createImageUniqueName; document.docs_TableID = tableID; document.docs_CreatedDate = DateTime.Now; document.docs_Value = recordID.ToString(); document.docs_FilePath = pathForSavingToDB; db.sm_doc.Add(document); await db.SaveChangesAsync(); documents.Add(document); } } return(documents); }
//-> Save public async Task <CheckLoanRequestViewDTO> Save(CheckLoanRequestEditDTO checkLoanRequest) { checkLoanRequest = StringHelper.TrimStringProperties(checkLoanRequest); tblAccount account = await db.tblAccounts.FirstOrDefaultAsync(a => a.acct_Deleted == null && a.acct_AccountID == checkLoanRequest.accountID); if (account == null) { throw new HttpException((int)HttpStatusCode.NotFound, "This record has been deleted"); } /*string tmp = Helpers.StringHelper.str2Date(checkLoanRequest.acct_DOB.ToString()); * if(tmp != "") * checkLoanRequest.acct_DOB = DateTime.Parse(tmp);*/ account = (tblAccount)MappingHelper.MapDTOToDBClass <CheckLoanRequestEditDTO, tblAccount>(checkLoanRequest, account); account.acct_UpdatedDate = DateTime.Now; await db.SaveChangesAsync(); return(await SelectByID(account.acct_AccountID)); }