public async Task <ActionResult> Edit(LaptopModels laptop) { try { if (ModelState.IsValid) { //Save changes await laptopRepository.editLaptop(laptop); var ID = Convert.ToInt32(TempData["id"]); //find last ID and save new Savings and FullPrice await laptopRepository.laptopFindAndSaveChanges(ID); return(RedirectToAction("Index")); } return(View(laptop)); } catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); } return(View()); }
//Create some laptop public async Task <LaptopModels> createLaptop(LaptopModels laptop) { _db.LaptopModels.Add(laptop); await _db.SaveChangesAsync(); //Last input in database var lastInput = await(from k in _db.LaptopModels select k) .OrderByDescending(k => k.ID) .FirstOrDefaultAsync(); //Get new full price lastInput.FullPrice = await(from k in _db.LaptopModels where k.ID == lastInput.ID select k.Price * k.Quantity).FirstAsync(); //Get new saving lastInput.Savings = await(from k in _db.LaptopModels where k.ID == lastInput.ID select k.OldPrice - k.Price).FirstAsync(); lastInput.Date = DateTime.Now; await _db.SaveChangesAsync(); //Create new log LogModels log = new LogModels { Type = "0", Description = "New laptop was inserted with name " + lastInput.Name + " on date " + lastInput.Date + " with quantity of " + lastInput.Quantity + ".", Date = lastInput.Date }; _db.LogModels.Add(log); await _db.SaveChangesAsync(); return(lastInput); }
//Edit some laptop public async Task <LaptopModels> editLaptop(LaptopModels laptop) { _db.Entry(laptop).State = EntityState.Modified; await _db.SaveChangesAsync(); return(laptop); }
//Delete laptop public async Task <LaptopModels> deleteLaptop(int?id) { LaptopModels laptop = await _db.LaptopModels.FindAsync(id); _db.LaptopModels.Remove(laptop); await _db.SaveChangesAsync(); return(laptop); }
public async Task <ActionResult> Create([Bind(Include = "Name, Details, Quantity, Manufacturer, OS, Price, OldPrice")] LaptopModels laptop) { if (ModelState.IsValid) { //Save in database and create new log await laptopRepository.createLaptop(laptop); return(RedirectToAction("Index")); } return(View(laptop)); }
//Search and Paging public async Task <object> pageCount(int pageSize, LaptopModels laptop) { int pageCount = laptop.Child.Count(); int pages = pageCount / pageSize; ViewBag.pageCount = pages; int rest = pageCount % pageSize; if (rest < 10) { pages = pages + 1; ViewBag.pageCount = pages; } return(ViewBag.pageCount); }
public HttpResponseMessage GetLaptopBySN([FromUri] LaptopModels laptop) { string sn = laptop.SN; if (sn == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not found")); } List <LaptopModels> laptops = (from k in _db.LaptopModels where k.SN == sn select k).ToList(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, laptops); return(response); }
public HttpResponseMessage GetLaptopByManufacturer([FromUri] LaptopModels laptop) { string manufacturer = laptop.Manufacturer; if (manufacturer == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not found")); } List <LaptopModels> laptops = (from k in _db.LaptopModels where k.Manufacturer == manufacturer select k).ToList(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, laptops); return(response); }
public HttpResponseMessage GetLaptopByQuantity([FromUri] LaptopModels laptop) { int?quantity = laptop.Quantity; if (quantity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not found")); } List <LaptopModels> laptops = (from k in _db.LaptopModels where k.Quantity == quantity select k).ToList(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, laptops); return(response); }
//Get some laptop by ID public async Task <LaptopModels> getLaptop(int?id) { LaptopModels laptop = await _db.LaptopModels.FindAsync(id); return(laptop); }