Esempio n. 1
0
 // GET: /Account/AddItem
 // Add an item. Just returns the view.
 public ActionResult AddItem()
 {
     AddItemModel model = new AddItemModel();
     Dictionary<string, int> itemClassGateway = new Dictionary<string, int>();
     using (var db = new CopiosisEntities())
     {
         itemClassGateway = FetchItemClassTemplates(db);
     }
     model.ItemClassTemplates = itemClassGateway;
     return View(model);
 }
Esempio n. 2
0
        public ActionResult AddItem(AddItemModel m)
        {
            if (ModelState.IsValid)
            {
                //ValidateItemModel(model);
                if (!m.ItemType.Equals("Product", StringComparison.OrdinalIgnoreCase) && !m.ItemType.Equals("Service", StringComparison.OrdinalIgnoreCase))
                {
                    ACCOUNTERROR.ErrorSubject = "Error while trying to add an item";
                    throw new ArgumentException("Items can only be of type Product or Service");
                }
                product p = new product();
                using (var db = new CopiosisEntities())
                {
                    int? itemClassId = db.itemClasses.Where(ic => ic.name == m.ItemClass).Select(i => i.classID).FirstOrDefault();
                    if (itemClassId == null)
                    {
                        ACCOUNTERROR.ErrorSubject = "Error while trying to add an item";
                        throw new ArgumentException("Product item class not found");
                    }

                    int existing = db.products.Where(i => i.name == m.Name && i.ownerID == WebSecurity.CurrentUserId).Count();
                    if (existing > 0)
                    {
                        m.ItemClassTemplates = FetchItemClassTemplates(db);
                        ModelState.AddModelError("name", "There is already an item of this name. Please try again.");
                        return View(m);
                    }

                    p.name = m.Name;
                    p.ownerID = WebSecurity.CurrentUserId;
                    p.guid = Guid.NewGuid();
                    p.gateway = m.Gateway;
                    p.description = m.Description;
                    p.createdDate = DateTime.Now;
                    p.itemClass = (int)itemClassId;
                    p.type = m.ItemType;
                    db.products.Add(p);
                    db.SaveChanges();
                    TempData["currentItem"] = p.name;
                    TempData["addSuccess"] = true;
                }
                return RedirectToAction("Items");
            }
            else
            {
                return View(m);
            }
        }
Esempio n. 3
0
        public ActionResult EditItem(AddItemModel model, Guid itemId)
        {
            using (var db = new CopiosisEntities())
            {
                var item = db.products.Where(p => p.guid == itemId && p.ownerID == WebSecurity.CurrentUserId).FirstOrDefault();
                int itemClassId = db.itemClasses.Where(ic => ic.name == model.ItemClass).Select(i => i.classID).First();
                int existing = db.products.Where(i => i.name == model.Name && i.ownerID == WebSecurity.CurrentUserId).Count();
                if (item == null)
                {
                    ACCOUNTERROR.ErrorSubject = "Error while trying to edit an item";
                    throw new ArgumentException(string.Format("Product with ID {0} not found", itemId));
                }
                else if (existing > 0 && model.Name != item.name)
                {
                    model.ItemClassTemplates = FetchItemClassTemplates(db);
                    ModelState.AddModelError("name", "There is already an item of this name. Please try again.");
                    return View(model);
                }
                else
                {
                    item.name = model.Name;
                    item.description = model.Description;
                    item.gateway = model.Gateway;
                    item.itemClass = itemClassId;
                    item.type = model.ItemType;
                    db.SaveChanges();
                    TempData["currentItem"] = item.name;
                    TempData["editSuccessful"] = true;

                }
            }
            return RedirectToAction("Items");
        }
Esempio n. 4
0
        public ActionResult EditItem(Guid itemId)
        {
            AddItemModel model = new AddItemModel();

            using (var db = new CopiosisEntities())
            {
                var item = db.products.Where(p => p.guid == itemId && p.ownerID == WebSecurity.CurrentUserId).FirstOrDefault();
                if (item == null)
                {
                    ACCOUNTERROR.ErrorSubject = "Error while trying to edit an item";
                    throw new ArgumentException(string.Format("Product with ID {0} not found", itemId));
                }
                else
                {
                    model.Name = item.name;
                    model.ItemClass = item.itemClass1.name;
                    model.Description = item.description;
                    model.Gateway = item.gateway;
                    model.ItemClassTemplates = FetchItemClassTemplates(db);
                    model.ItemType = item.type;
                }
            }

            return View(model);
        }