//public ActionResult AddTheLoan(LoansClientsInventoryDTO loanSubmission) //(LoanSubmission loanSubmission) public ActionResult AddTheLoan(LoanSubmission loanSubmission) { var services = new LoanMasterServices(); services.CreateLoan(loanSubmission); return(RedirectToAction("Index", "Loan")); }
public void CreateLoan(LoanSubmission loanSubmission) { //have client id using (var context = new SingularityDBContext()) { var loanNumIncrement = LoanIncrement(); var newLoan = new LoanMaster { ClientId = loanSubmission.ClientId, DateCreated = DateTime.Now, //can go into the constructor of LoanMaster -- haven't done this yet //IsActive = loanSubmission.IsActive //can probably be defaulted LoanNumber = loanNumIncrement, //we can create a utility class that auto increments this - see above IsActive = true }; context.LoanMasters.Add(newLoan); context.SaveChanges(); //this line actually writes the record to the db //after writing the record the newLoan object will have a populated Id that matches the db //in this way we can add LoanDetails that reference the correct LoanMaster //have item Ids //we can map over the itemIds enumerable and map a new list of LoanDetails -- functional! //LINQ query syntax //Create new LoanDetail for each item in InventoryItemIds list IEnumerable <LoanDetail> query = from itemId in loanSubmission.InventoryItemIds select new LoanDetail { InventoryItemId = itemId, LoanMasterId = newLoan.LoanMasterId, Purpose = loanSubmission.Purpose, PurposeType = loanSubmission.PurposeType }; //LINQ method syntax - would be same outcome as above query //IEnumerable<LoanDetail> methodQuery = // loanSubmission.InventoryItemIds.Select(id => new LoanDetail // { // InventoryItemId = id, // LoanMasterId = newLoan.LoanMasterId // }); //both are viable, sometimes query syntax feels more natural on joins and such //while method syntax can output some nice oneliners. //the point is that we use each item in a collection to create new LoanDetail //entities, mapping the properties we want over to the newly created object. //regardless, these queries have only returned something respects IEnumerable contract //we need concrete objects List <LoanDetail> loanDetailsList = query.ToList(); //now we can add a range of loan details to the loan details table context.LoanDetails.AddRange(loanDetailsList); context.SaveChanges(); ////Update Inventory Items' Availability var itemIds = GetInventoryItemIdsByLoanNumber(newLoan.LoanNumber); MarkInventoryItemsAsNotAvailable(context, itemIds); //or does this need to be like context.LoanDetails.AddRange(loanDetailsList); } }