Example #1
0
        public ActionResult Save(CommentSaveModel m)
        {
            if (ModelState.IsValid && m.SessionId == Session.SessionID) {
                using (var db = new DataContext()) {
                    // Check if the comment belongs to a standard entity.
                    var isPage = db.Pages.Where(p => p.Id == m.ParentId).SingleOrDefault() != null ;
                    var isPost = !isPage && db.Posts.Where(p => p.Id == m.ParentId).SingleOrDefault() != null ;
                    var isMedia = !isPage && !isPost && db.Posts.Where(c => c.Id == m.ParentId).SingleOrDefault() != null ;
                    var isUload = !isPage && !isPost && !isMedia && db.Uploads.Where(u => u.Id == m.ParentId).SingleOrDefault() != null ;

                    // Now get the comment settings
                    var sett = Areas.Manager.Models.CommentSettingsModel.Get() ;

                    // Check comment settings according to type
                    if (isPage && (!sett.EnablePages || (!User.Identity.IsAuthenticated && !sett.EnableAnonymous)))
                        return Redirect("~/") ;
                    else if (isPost && (!sett.EnablePosts || (!User.Identity.IsAuthenticated && !sett.EnableAnonymous)))
                        return Redirect("~/") ;
                    else if (isMedia && (!sett.EnableMedia || (!User.Identity.IsAuthenticated && !sett.EnableAnonymous)))
                        return Redirect("~/") ;
                    else if (isUload && (!sett.EnableUploads || (!User.Identity.IsAuthenticated && !sett.EnableAnonymous)))
                        return Redirect("~/") ;

                    Comment comment = null ;

                    // Try to load the comment if this is an update
                    if (m.Id != null)
                        comment = db.Comments.Where(c => c.Id == m.Id).SingleOrDefault() ;

                    // If no existing comment was found, create a new
                    if (comment == null) {
                        comment = new Comment() ;
                        comment.Attach(db, EntityState.Added) ;
                    }

                    // If the user isn't authenticated, add user info
                    if (!User.Identity.IsAuthenticated) {
                        comment.AuthorName = m.AuthorName ;
                        comment.AuthorEmail = m.AuthorEmail ;
                    }

                    // Update standard properties
                    comment.Title = m.Title ;
                    comment.Body = m.Body ;

                    db.SaveChanges() ;
                }
            }
            return new RedirectResult("~/") ;
        }
Example #2
0
        /// <summary>
        /// Saves the current edit model.
        /// </summary>
        /// <returns>Whether the entity was updated or not</returns>
        public bool Save()
        {
            using (var db = new DataContext()) {
                var comment = db.Comments.Where(c => c.Id == Id).SingleOrDefault() ;
                if (comment == null) {
                    comment = new Comment() ;
                    comment.Attach(db, EntityState.Added) ;

                    comment.ParentId = ParentId ;
                    comment.Title = Title ;
                    comment.Body = Body ;
                }
                comment.Status = Status ;

                return db.SaveChanges() > 0 ;
            }
        }