public ActionResult Update(UserInfo userInfo)
        {
            try
            {
                var userPermissions = userInfo.Permissions;

                if (userInfo.Id == this.GetCurrentUserId())
                {
                    userPermissions.AllowManageUsers = true;
                }

                using (var db = new CheatNotesContext())
                {
                    var permissions = db.UserPermissions.FirstOrDefault(p => p.Id == userPermissions.Id);

                    if (permissions != null)
                    {
                        permissions.UpdateData(userPermissions);

                        db.Entry(permissions).State = EntityState.Modified;
                        db.SaveChanges();
                    }
                }
            }
            catch
            {
            }

            return RedirectToAction("Index");
        }
 public ActionResult CheatNoteInfo(int cnid, bool? toDetails)
 {
     using (var db = new CheatNotesContext())
     {
         return PartialView(toDetails.HasValue && toDetails.Value ? "_CheatNoteInfoToDetailsControl" : "_CheatNoteInfoToListControl", db.CheatNotes.Find(cnid));
     }
 }
        public static string CreateUserInfo(string userName)
        {
            try
            {
                using (var db = new CheatNotesContext())
                {
                    if (db.UserInfos.Any(x => x.UserName.Equals(userName, StringComparison.InvariantCultureIgnoreCase)))
                    {
                        return null;
                    }

                    var userInfo = new UserInfo
                    {
                        UserName = userName,
                        Permissions = UserPermissions.GetDefaultPermissions()
                    };

                    db.UserInfos.Add(userInfo);
                    db.SaveChanges();
                }

                return null;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }
        public ActionResult Create(CheatNote cheatNote, string questionsPlainText)
        {
            try
            {
                var cheatNoteItems = ParseCheatNoteItems(questionsPlainText);

                if (cheatNoteItems != null && cheatNoteItems.Count > 0)
                {
                    foreach (var item in cheatNoteItems)
                    {
                        item.CreatedById = GetCurrentUserId();
                    }

                    cheatNote.Items = cheatNoteItems;
                }

                using (var db = new CheatNotesContext())
                {
                    cheatNote.CreatedById = GetCurrentUserId();

                    db.CheatNotes.Add(cheatNote);
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View(cheatNote);
            }
        }
        public static UserInfo GetUserInfo(string userName)
        {
            using (var db = new CheatNotesContext())
            {
                var userInfo = db.UserInfos.Include(x => x.Permissions).FirstOrDefault(x => x.UserName.Equals(userName, StringComparison.InvariantCultureIgnoreCase));

                return userInfo;
            }
        }
        public ActionResult Index()
        {
            using (var db = new CheatNotesContext())
            {
                ViewBag.CurrentUserId = GetCurrentUserId();

                return View(db.UserInfos.Include(x => x.Permissions).ToList());
            }
        }
        public ActionResult Details(int id)
        {
            using (var db = new CheatNotesContext())
            {
                ViewBag.EditPermitted = IsPermitted(o => o.AllowEditCheatNoteItems);
                ViewBag.DeletePermitted = IsPermitted(o => o.AllowDeleteCheatNoteItems);

                return View(db.CheatNoteItems.Get(id));
            }
        }
        public ActionResult Details(int id)
        {
            ViewBag.GeneratorPermitted = IsPermitted(p => p.AllowGenerateCheatNoteHtml);
            ViewBag.EditPermitted = IsPermitted(o => o.AllowEditCheatNotes);
            ViewBag.DeletePermitted = IsPermitted(o => o.AllowDeleteCheatNotes);

            using (var db = new CheatNotesContext())
            {
                return View(db.CheatNotes.Get(id));
            }
        }
        public ActionResult GenerateHtmlFiles(GenerateHtmlFilesModel model)
        {
            using (var db = new CheatNotesContext())
            {
                var cheatNote = db.CheatNotes.Get(model.CheatNoteId);
                var partSize = !model.PartSize.HasValue || model.PartSize.Value <= 100 ? null : (long?)(model.PartSize * 1024 / FileSizeMultiplier);

                model.HtmlInfos = UpdateCheatNoteHtmls(db, model.CheatNoteId, CheatNoteHtmlGenerator.GenerateHtmlFiles(model.FileName, cheatNote, partSize));

                return View(model);
            }
        }
        public ActionResult Download(int id)
        {
            using (var db = new CheatNotesContext())
            {
                var html = db.CheatNoteHtmls.FirstOrDefault(x => x.Id == id);

                if (html == null)
                {
                    return null;
                }

                return File(TextToBytes(html.Html), "text/html", html.FileName);
            }
        }
        public ActionResult DownloadZip(int cnid)
        {
            using (var db = new CheatNotesContext())
            {
                var htmls = GetGeneratedHtmls(db, cnid);

                if (!htmls.Any())
                {
                    return null;
                }

                return File(CreateZip(htmls), "application/zip", string.Format("CheatNote_{0}.zip", cnid));
            }
        }
        public ActionResult GenerateHtmlFiles(int id)
        {
            using (var db = new CheatNotesContext())
            {
                var cheatNote = db.CheatNotes.Get(id);

                var model = new GenerateHtmlFilesModel(cheatNote)
                                {
                                    PartSize = 250,
                                    HtmlInfos = GetGeneratedHtmls(db, id)
                                };

                return View(model);
            }
        }
        public ActionResult Create(CheatNoteItem cheatNoteItem)
        {
            try
            {
                using (var db = new CheatNotesContext())
                {
                    cheatNoteItem.CreatedById = GetCurrentUserId();

                    cheatNoteItem.Title = cheatNoteItem.Title.AntiCodeInjection();
                    cheatNoteItem.Content = cheatNoteItem.Content.AntiCodeInjection();

                    db.CheatNoteItems.Add(cheatNoteItem);
                    db.SaveChanges();
                }

                return RedirectToItemList(cheatNoteItem.CheatNoteId, cheatNoteItem.Position);
                //return RedirectToAction("List", new { cnid = cheatNoteItem.CheatNoteId });
            }
            catch
            {
                return View();
            }
        }
 public ActionResult Index(bool? showDeleted)
 {
     using (var db = new CheatNotesContext())
     {
         ViewBag.CreatePermitted = IsPermitted(o => o.AllowCreateCheatNotes);
         //return View(db.CheatNotes.IncludeBase().Where(CheatNotesContextHelper.GetListConditions(new CheatNotesContextOptions(showDeleted ?? false))).OrderByDescending(x => x.DateModified).ToList());
         return View(db.CheatNotes.List(l => l.Where(CheatNotesContextHelper.GetListConditions(new CheatNotesContextOptions(showDeleted ?? false))).OrderByDescending(x => x.DateModified)));
     }
 }
        public ActionResult Edit(int id, CheatNote cheatNote)
        {
            try
            {
                using (var db = new CheatNotesContext())
                {
                    var instance = db.CheatNotes.Get(id);

                    if (instance != null)
                    {
                        instance.Name = cheatNote.Name;
                        instance.Description = cheatNote.Description;
                        instance.DateModified = DateTime.Now;
                        instance.ModifiedById = GetCurrentUserId();
                    }

                    db.Entry(instance).State = EntityState.Modified;
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        private static int GetDefaultItemPosition(int cnid)
        {
            using (var db = new CheatNotesContext())
            {
                var maxPos = db.CheatNoteItems.Where(x => !x.DateDeleted.HasValue && x.CheatNoteId == cnid).Max(x => (int?)x.Position);

                return (maxPos.HasValue && maxPos.Value > 0) ? maxPos.Value + 1 : 1;
            }
        }
        private ActionResult Save(int id, CheatNoteItem cheatNoteItem, int? nextItemId)
        {
            try
            {
                using (var db = new CheatNotesContext())
                {
                    var instance = db.CheatNoteItems.Get(id);

                    instance.Position = cheatNoteItem.Position;
                    instance.Title = cheatNoteItem.Title.AntiCodeInjection();
                    instance.Content = cheatNoteItem.Content.AntiCodeInjection();
                    instance.DateModified = DateTime.Now;
                    instance.ModifiedById = GetCurrentUserId();

                    db.SaveChanges();
                }

                return nextItemId.HasValue
                    ? RedirectToAction("Edit", new { id = nextItemId.Value })
                    : RedirectToItemList(cheatNoteItem.CheatNoteId, cheatNoteItem.Position);
                //return RedirectToAction("List", new { cnid = cheatNoteItem.CheatNoteId });
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Edit(int id)
        {
            using (var db = new CheatNotesContext())
            {
                var item = db.CheatNoteItems.Get(id);

                var nextItem = db.CheatNoteItems.OrderBy(x => x.Position).FirstOrDefault(x => x.CheatNoteId == item.CheatNoteId && x.Position > item.Position);

                ViewBag.NextItemId = nextItem == null ? null : (int?)nextItem.Id;

                return View(item);
            }
        }
 private static IEnumerable<CheatNoteGeneratedHtml> GetGeneratedHtmls(CheatNotesContext db, int cheatNoteId)
 {
     return db.CheatNoteHtmls.Where(html => html.CheatNoteId == cheatNoteId).ToList();
 }
        private static IEnumerable<CheatNoteGeneratedHtml> UpdateCheatNoteHtmls(CheatNotesContext db, int cheatNoteId, IEnumerable<CheatNoteGeneratedHtml> htmls)
        {
            foreach (var html in htmls)
            {
                html.CheatNoteId = cheatNoteId;
                html.Size = TextToBytes(html.Html).Length;
            }

            var existing = GetGeneratedHtmls(db, cheatNoteId);

            if (existing.Any())
            {
                db.CheatNoteHtmls.RemoveRange(existing);
            }

            var result = db.CheatNoteHtmls.AddRange(htmls);

            db.SaveChanges();

            return result;
        }
 public ActionResult Edit(int id)
 {
     using (var db = new CheatNotesContext())
     {
         return View(db.CheatNotes.Get(id));
     }
 }
        public ActionResult List(int? cnid, bool? showDeleted)
        {
            if (!cnid.HasValue)
            {
                return RedirectToAction("Error", "Home");
            }

            using (var db = new CheatNotesContext())
            {
                if (!db.CheatNotes.Any(x => x.Id == cnid.Value))
                {
                    return RedirectToAction("Error", "Home");
                }

                ViewBag.CreatePermitted = IsPermitted(o => o.AllowCreateCheatNoteItems);
                ViewBag.EditPermitted = IsPermitted(o => o.AllowEditCheatNotes);

                return View(db.CheatNoteItems.List( l => l.Where(CheatNotesContextHelper.GetListConditions(cnid.Value, new CheatNotesContextOptions(showDeleted ?? false))).OrderBy(x => x.Position)));
            }
        }