public ModelAdminViewModel Get(int id) { var record = db.Models .Where(x => x.ModelId == id) .FirstOrDefault(); var model = new ModelAdminViewModel() { ModelNumber = record.ModelNumber, ModelId = record.ModelId, DtCreated = record.DtCreated, Price = record.Price, Status = record.Status, BrandName = (from b in db.Brands where b.BrandId == record.BrandId select b.Name).FirstOrDefault(), ItemName = (from i in db.Items where i.ItemId == record.ItemId select i.Description).FirstOrDefault(), SupplierName = (from s in db.Suppliers where s.SupplierId == record.SupplierId select s.Name).FirstOrDefault(), SupplierEmail = (from s in db.Suppliers where s.SupplierId == record.SupplierId select s.Email).FirstOrDefault(), SupplierId = record.SupplierId, ImageUrl = (from ph in db.Photos join pm in db.PhotoModels on ph.PhotoId equals pm.PhotoId join m in db.Models on pm.ModelId equals m.ModelId where pm.ModelId == record.ModelId select ph.ImageUrl).FirstOrDefault() }; return model; }
public async Task<ActionResult> Update(int id, ModelAdminViewModel model) { try { var item = Session["Model"] as ModelAdminViewModel; string userId = User.Identity.GetUserId(); if (userId == null) { return RedirectToAction("Login", "Account"); } //Check if the "Admin" role exists if not it returns a null value //var role = db.roles.singleordefault(m => m.name == "admin"); if (User.IsInRole("Admin")) { // ViewBag.ItemId = new SelectList(db.Items.Where(x => x.Status == "Active"), "ItemId", "Description", item.ItemId); if (item == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Product cannot be null"); } ////var updatedItem = _repository.Update(id, item); //if (updatedItem == null) //{ // return HttpNotFound(); //} var itemToUpdate = db.Models.Where(x => x.ModelId == item.ModelId).FirstOrDefault(); string itemStatus = itemToUpdate.Status.ToString(); if (itemStatus == "Pending") { itemToUpdate.Status = "Active"; } else if (itemStatus == "Active") { itemToUpdate.Status = "Inactive"; } else if (itemStatus == "Inactive") { itemToUpdate.Status = "Active"; } db.SaveChanges(); if (itemToUpdate.Status == "Active") { var supplierEmail = (from s in db.Suppliers where s.SupplierId == item.SupplierId select s.Email).FirstOrDefault(); var body = "<p>Dear Valued Supplier,</p><p>Your product with reference {0} has been validated</p><p>Best Regards</p><p>The BontoBuy Team</p>"; var message = new MailMessage(); message.To.Add(new MailAddress(supplierEmail)); message.From = new MailAddress("*****@*****.**"); message.Subject = "Register on BontoBuy"; message.Body = string.Format(body, item.ModelNumber); message.IsBodyHtml = true; var smtp = new SmtpClient(); var credential = new NetworkCredential() { UserName = "******", Password = "******" }; smtp.Credentials = credential; smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.EnableSsl = true; await smtp.SendMailAsync(message); } return RedirectToAction("Retrieve", new { message = ManageMessageId.UpdateSuccess }); } return RedirectToAction("Login", "Account"); } catch (Exception ex) { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString()); } }
// GET: Model/Edit/5 public ActionResult Update(int id) { try { string userId = User.Identity.GetUserId(); if (userId == null) { return RedirectToAction("Login", "Account"); } //Check if the "Admin" role exists if not it returns a null value //var role = db.roles.singleordefault(m => m.name == "admin"); if (User.IsInRole("Admin")) { // ViewBag.ItemId = new SelectList(db.Items.Where(x => x.Status == "Active"), "ItemId", "Description"); if (id < 1) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Invalid Identifier"); } var itemToUpdate = db.Models.Where(x => x.ModelId == id).FirstOrDefault(); if (itemToUpdate == null) { return HttpNotFound(); } var model = new ModelAdminViewModel() { ModelNumber = itemToUpdate.ModelNumber, ModelId = itemToUpdate.ModelId, DtCreated = itemToUpdate.DtCreated, Price = itemToUpdate.Price, Status = itemToUpdate.Status, BrandName = (from b in db.Brands where b.BrandId == itemToUpdate.BrandId select b.Name).FirstOrDefault(), ItemName = (from i in db.Items where i.ItemId == itemToUpdate.ItemId select i.Description).FirstOrDefault(), SupplierName = (from s in db.Suppliers where s.SupplierId == itemToUpdate.SupplierId select s.Name).FirstOrDefault(), SupplierEmail = (from s in db.Suppliers where s.SupplierId == itemToUpdate.SupplierId select s.Email).FirstOrDefault(), SupplierId = itemToUpdate.SupplierId, ImageUrl = (from ph in db.Photos join pm in db.PhotoModels on ph.PhotoId equals pm.PhotoId join m in db.Models on pm.ModelId equals m.ModelId where pm.ModelId == itemToUpdate.ModelId select ph.ImageUrl).FirstOrDefault() }; Session["Model"] = model; GetNewSupplierActivation(); GetNewModelsActivation(); return View(model); } return RedirectToAction("Login", "Account"); } catch (Exception ex) { return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, ex.ToString()); } }