public async Task <IActionResult> ClientEditor(int id, ClientViewModel model) { if (ModelState.IsValid) { var oldClient = await _context.Clients .Include(c => c.Address) .Where(c => c.Id == id) .FirstOrDefaultAsync(); if (oldClient != null) { _mapper.Map(model, oldClient); if (await _context.SaveChangesAsync() > 0) { return(RedirectToAction("Index")); } } else { var newClient = _mapper.Map <Client>(model); _context.Add(newClient); if (await _context.SaveChangesAsync() > 0) { return(RedirectToAction("Index")); } } } return(View()); }
public async Task <ActionResult <TimeBillViewModel> > Post([FromBody] TimeBillViewModel model) { var bill = _mapper.Map <TimeBill>(model); var theCase = await _ctx.Cases .Where(c => c.Id == model.CaseId) .FirstOrDefaultAsync(); var theEmployee = await _ctx.Employees .Where(e => e.Id == model.EmployeeId) .FirstOrDefaultAsync(); if (theCase == null || theEmployee == null) { return(BadRequest("Could not find case or employee")); } bill.Case = theCase; bill.Employee = theEmployee; _ctx.Add(bill); if (await _ctx.SaveChangesAsync() > 0) { return(CreatedAtAction("Get", new { id = bill.Id }, _mapper.Map <TimeBillViewModel>(bill))); } return(BadRequest("Failed to save new timebill")); }
public int Create(ReceiptBody t) { int result = 0; try { //Initiate the instance of DBContext using BillingContext context = new BillingContext(); //check specific record for same ReceiptNo is exist or not var receipt = context.ReceiptBodies.FirstOrDefault(c => c.ReceiptNo == t.ReceiptNo); if (receipt == null) { //adding receiptbody object to context context.Add(t); //save changes return(result = context.SaveChanges()); } else { context.Update(t); return(result = context.SaveChanges()); } } catch (Exception ex) { Exc.ErMessage(ex); return(result = 0); } }
public int Create(Customer t) { int result = 0; try { //Initiate the instance of DBContext using BillingContext context = new BillingContext(); //check specific record for same Customer id is exist or not var cust = context.Customers.FirstOrDefault(c => c.CustomerId == t.CustomerId); if (cust == null) { //adding Customer object to context context.Add(t); //save changes return(result = context.SaveChanges()); } else { return(Update(t)); } } catch (Exception ex) { Exc.ErMessage(ex); return(result = 0); } }
private void ProcessCheckout() { if (CurrentCustomer == null) { CurrentCustomer = new Customer { Id = User.FindFirst(ClaimTypes.NameIdentifier)?.Value, CreditCard = Checkout.CreditCardDetails, Invoices = new List <Invoice>() { CreateInvoice() } }; BillingContext.Add(CurrentCustomer); } else { if (Checkout.SaveNewCreditCardDetails) { CurrentCustomer.CreditCard = Checkout.CreditCardDetails; } CurrentCustomer.Invoices.Add(CreateInvoice()); } CurrentItem.Status = ItemStatus.Sold; }
public async Task <ActionResult <TimeBill> > Post([FromBody] TimeBill bill) { var theCase = await _ctx.Cases .Where(c => c.Id == bill.Case.Id) .FirstOrDefaultAsync(); var theEmployee = await _ctx.Employees .Where(e => e.Id == bill.Employee.Id) .FirstOrDefaultAsync(); bill.Case = theCase; bill.Employee = theEmployee; _ctx.Add(bill); if (await _ctx.SaveChangesAsync() > 0) { return(CreatedAtAction("Get", new { id = bill.Id }, bill)); } return(BadRequest("Failed to save new timebill")); }
/// <summary> /// Sets up some test only Db Objects. /// </summary> private void SetupTestingRecords() { using (var context = new BillingContext(options)) { var s1 = new Product() { Id = 8, Name = "Some Other Product", Price = 10, SKU = "somethingelse" }; var potterBooks = new List <Product> { s1 }; context.Add(s1); context.SaveChanges(); } }
/// <summary> /// In memory database seed /// </summary> private void SetupHarryPotterDiscounts() { using (var context = new BillingContext(options)) { // Products var p1 = new Product() { Id = 1, Name = "Harry Potter Book 1", Price = 10, SKU = "potter1", isHarryPotter = true }; var p2 = new Product() { Id = 2, Name = "Harry Potter Book 2", Price = 10, SKU = "potter2", isHarryPotter = true }; var p3 = new Product() { Id = 3, Name = "Harry Potter Book 3", Price = 10, SKU = "potter3", isHarryPotter = true }; var p4 = new Product() { Id = 4, Name = "Harry Potter Book 4", Price = 10, SKU = "potter4", isHarryPotter = true }; var p5 = new Product() { Id = 5, Name = "Harry Potter Book 5", Price = 10, SKU = "potter5", isHarryPotter = true }; var p6 = new Product() { Id = 6, Name = "Harry Potter Book 6", Price = 10, SKU = "potter6", isHarryPotter = true }; var p7 = new Product() { Id = 7, Name = "Harry Potter Book 7", Price = 10, SKU = "potter7", isHarryPotter = true }; var potterBooks = new List <Product> { p1, p2, p3, p4, p5, p6, p7 }; context.AddRange(potterBooks); // Discounts var d1 = new Discount() { Id = 1, MinProductsRequired = 2, Percent = 0.05 }; AddDiscountProduct(d1, p1); AddDiscountProduct(d1, p2); AddDiscountProduct(d1, p3); AddDiscountProduct(d1, p4); AddDiscountProduct(d1, p5); AddDiscountProduct(d1, p6); AddDiscountProduct(d1, p7); var d2 = new Discount() { Id = 2, MinProductsRequired = 3, Percent = 0.10, }; AddDiscountProduct(d2, p1); AddDiscountProduct(d2, p2); AddDiscountProduct(d2, p3); AddDiscountProduct(d2, p4); AddDiscountProduct(d2, p5); AddDiscountProduct(d2, p6); AddDiscountProduct(d2, p7); var d3 = new Discount() { Id = 3, MinProductsRequired = 4, Percent = 0.20 }; AddDiscountProduct(d3, p1); AddDiscountProduct(d3, p2); AddDiscountProduct(d3, p3); AddDiscountProduct(d3, p4); AddDiscountProduct(d3, p5); AddDiscountProduct(d3, p6); AddDiscountProduct(d3, p7); var d4 = new Discount() { Id = 4, MinProductsRequired = 5, Percent = 0.25 }; AddDiscountProduct(d4, p1); AddDiscountProduct(d4, p2); AddDiscountProduct(d4, p3); AddDiscountProduct(d4, p4); AddDiscountProduct(d4, p5); AddDiscountProduct(d4, p6); AddDiscountProduct(d4, p7); context.Add(d1); context.Add(d2); context.Add(d3); context.Add(d4); context.SaveChanges(); } }