public ActionResult ToggleSticky(int submissionId)
        {
            // get model for selected submission
            var submissionModel = _db.Messages.Find(submissionId);

            if (submissionModel == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            // check if caller is subverse moderator, if not, deny change
            if (!Utils.User.IsUserSubverseModerator(User.Identity.Name, submissionModel.Subverse) &&
                !Utils.User.IsUserSubverseAdmin(User.Identity.Name, submissionModel.Subverse))
            {
                return(new HttpUnauthorizedResult());
            }
            try
            {
                // find and clear current sticky if toggling
                var existingSticky = _db.Stickiedsubmissions.FirstOrDefault(s => s.Submission_id == submissionId);
                if (existingSticky != null)
                {
                    _db.Stickiedsubmissions.Remove(existingSticky);
                    _db.SaveChanges();
                    return(new HttpStatusCodeResult(HttpStatusCode.OK));
                }

                // remove all stickies for subverse matching submission subverse
                _db.Stickiedsubmissions.RemoveRange(_db.Stickiedsubmissions.Where(s => s.Subversename == submissionModel.Subverse));

                // set new submission as sticky
                var stickyModel = new Stickiedsubmission
                {
                    Submission_id = submissionId,
                    Stickied_by   = User.Identity.Name,
                    Stickied_date = DateTime.Now,
                    Subversename  = submissionModel.Subverse
                };

                _db.Stickiedsubmissions.Add(stickyModel);
                _db.SaveChanges();

                return(new HttpStatusCodeResult(HttpStatusCode.OK));
            }
            catch (Exception)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
            }
        }
Exemple #2
0
        public ActionResult ToggleSticky(int submissionId)
        {
            // get model for selected submission
            var submissionModel = _db.Messages.Find(submissionId);

            if (submissionModel == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            // check if caller is subverse moderator, if not, deny change
            if (!UserHelper.IsUserSubverseModerator(User.Identity.Name, submissionModel.Subverse)) return new HttpUnauthorizedResult();
            try
            {
                // find and clear current sticky if toggling
                var existingSticky = _db.Stickiedsubmissions.FirstOrDefault(s => s.Submission_id == submissionId);
                if (existingSticky != null)
                {
                    _db.Stickiedsubmissions.Remove(existingSticky);
                    _db.SaveChanges();
                    return new HttpStatusCodeResult(HttpStatusCode.OK);
                }

                // remove all stickies for subverse matching submission subverse
                _db.Stickiedsubmissions.RemoveRange(_db.Stickiedsubmissions.Where(s => s.Subversename == submissionModel.Subverse));

                // set new submission as sticky
                var stickyModel = new Stickiedsubmission
                {
                    Submission_id = submissionId,
                    Stickied_by = User.Identity.Name,
                    Stickied_date = DateTime.Now,
                    Subversename = submissionModel.Subverse
                };

                _db.Stickiedsubmissions.Add(stickyModel);
                _db.SaveChanges();

                return new HttpStatusCodeResult(HttpStatusCode.OK);
            }
            catch (Exception)
            {
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
            }
        }