/// <summary>
 /// Deprecated Method for adding a new object to the Definitions EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToDefinitions(Definition definition)
 {
     base.AddObject("Definitions", definition);
 }
 /// <summary>
 /// Check if user is definition author
 /// </summary>
 /// <param name="definition"></param>
 /// <returns></returns>
 public static bool IsUserAuthor(Definition definition)
 {
     return definition.UserId != null && definition.UserId == GetUserGuid();
 }
 /// <summary>
 /// Create a new Definition object.
 /// </summary>
 /// <param name="id">Initial value of the ID property.</param>
 /// <param name="name">Initial value of the Name property.</param>
 /// <param name="displayName">Initial value of the DisplayName property.</param>
 public static Definition CreateDefinition(global::System.Int32 id, global::System.String name, global::System.String displayName)
 {
     Definition definition = new Definition();
     definition.ID = id;
     definition.Name = name;
     definition.DisplayName = displayName;
     return definition;
 }
 /// <summary>
 /// Check if user has edit rights for definition
 /// </summary>
 /// <param name="definition"></param>
 /// <returns></returns>
 public static bool CanUserAccess(Definition definition)
 {
     return IsUserAdmin() || IsUserAuthor(definition);
 }
 public ActionResult CreateAndContinue(Definition definition)
 {
     return Create(definition, "Edit");
 }
 public ActionResult CreateAndBack(Definition definition)
 {
     return Create(definition, "Index");
 }
        private ActionResult Create(Definition definition, string action)
        {
            if (_db.Definitions.FirstOrDefault(d => d.Name.ToLower() == definition.Name.ToLower()) != null)
            {
                ModelState.AddModelError("Name","Полное имя должно быть уникальным. Похожее определение уже есть");
                return View("Create");
            }

            if (ModelState.IsValid)
            {
                _db.Definitions.AddObject(definition);
                _db.SaveChanges();
                return RedirectToAction(action, new { id = definition.ID });
            }

            return View("Create");
        }
        public ActionResult Edit(Definition definition)
        {
            if (!AccountController.CanUserAccess(definition))
                return RedirectToAction("AccessDenied", "Account",
                    new
                    {
                        message = string.Format("У вас нет права редактировать определение {0} ({1}). Обратитесь к автору или администратору",
                            definition.DisplayName, definition.Name)
                    });

            if (ModelState.IsValid)
            {
                _db.Definitions.Attach(definition);
                _db.ObjectStateManager.ChangeObjectState(definition, EntityState.Modified);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(definition);
        }