Esempio n. 1
0
        public static bool Save(Post p)
        {
            if (!p)
            {
                return false;
            }

            IDataAccess da = DataAccess.CreateInstance();
            p.Id = da.SavePost(p);
            return p.Id > 0;
        }
Esempio n. 2
0
        public static Collection<Post> CreatePostsFromReader(SqlDataReader reader)
        {
            // First result set is the postcategories. 
            Dictionary<int, Category> postcats = new Dictionary<int, Category>();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    // Oops - key can't be PostId as key has to be unique. Need to use a different collection..
                    // .. for now, make a hash of the post and category IDs.
                    int categoryId = Convert.ToInt32(reader["categoryId"]);
                    int postHash = (Convert.ToInt32(reader["PostId"]) * 10000) + categoryId;
                    postcats.Add(postHash, new Category(categoryId, Convert.ToString(reader["Name"]), Convert.ToString(reader["Slug"])));
                }
            }

            // Second resultset is the post(s)
            reader.NextResult();

            Collection<Post> postlist = new Collection<Post>();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Post p = new Post();

                    p.Id = Convert.ToInt32(reader["Id"]);
                    p.Title = Convert.ToString(reader["Title"]);
                    p.Postdate = Convert.ToDateTime(reader["PostDate"]);
                    p.Body = Convert.ToString(reader["Body"]);
                    p.Slug = Convert.ToString(reader["Slug"]);
                    p.CommentCount = Convert.ToInt32(reader["CommentCount"]);
                    p.Published = Convert.ToBoolean(reader["Published"]);
                    p.Categories = new Collection<Category>();

                    // .. then the categories.
                    IEnumerable<Category> cats =
                        from entry in postcats
                        where ((entry.Key / 10000) == p.Id)
                        select entry.Value;

                    foreach (Category cat in cats)
                    {
                        p.Categories.Add(cat);
                    }

                    postlist.Add(p);
                }
            }

            return postlist;
        }
Esempio n. 3
0
        public void SavePost(object sender, CommandEventArgs e)
        {
            if (KMAuthentication.IsUserAdmin(User) == false)
            {
                return;
            }

            Page.Validate();

            bool pageIsValid = Page.IsValid;

            DateTime dt = this.GetPostDate();
            if (dt == DateTime.MinValue)
            {
                string errors = this.GetPostDateErrors();
                date_error.Text = errors;
                pageIsValid = false;
            }
            else
            {
                date_error.Text = String.Empty;
            }

            if (pageIsValid == false)
            {
                if (slugValidator.IsValid == false)
                {
                    ClientScript.RegisterClientScriptBlock(
                        this.GetType(),
                        "jquery_start",
                        " $(document).ready(function(){$('#editslug').hide(0);$('#slugdiv').show(0);return false;});",
                        true);
                }

                return;
            }

            Post p = new Post();

            int postId;
            if (Int32.TryParse(hiddenPostId.Value, out postId) == false)
            {
                postId = 0;
                p.Slug = this.GetSlug(posttitle.Text);
            }
            else
            {
                p.Slug = postslug.Text;
                if (String.IsNullOrEmpty(p.Slug))
                {
                    p.Slug = this.GetSlug(posttitle.Text);
                }
            }

            p.Id = postId;

            p.Title = posttitle.Text;
            p.Body = blogpost.Value;
            p.Postdate = dt;

            p.Published = (e.CommandName == "Publish");

            if (Post.Save(p))
            {
                this.SyncCategories(p.Id);

                if (p.Published)
                {
                    Response.Redirect("admin.aspx");
                }
                else if (postId == 0)
                {
                    Response.Redirect("post-edit.aspx?p=" + Convert.ToString(p.Id));
                }
            }
        }
Esempio n. 4
0
        public int SavePost(Post p)
        {
            using (SqlConnection connection = this.GetConnection())
            {
                connection.Open();

                SqlCommand sc = new SqlCommand();

                if (p.Id == 0)
                {
                    sc.CommandText = "CreatePost";
                    SqlParameter sp = new SqlParameter("NewPostId", 0);
                    sp.Direction = ParameterDirection.Output;
                    sc.Parameters.Add(sp);
                }
                else
                {
                    sc.CommandText = "EditPost";
                    sc.Parameters.AddWithValue("PostId", p.Id);
                }

                sc.Parameters.AddWithValue("slug", p.Slug);
                sc.Parameters.AddWithValue("title", p.Title);
                sc.Parameters.AddWithValue("body", p.Body);
                sc.Parameters.AddWithValue("postdate", p.Postdate);
                sc.Parameters.AddWithValue("published", p.Published);

                sc.Connection = connection;
                sc.CommandType = CommandType.StoredProcedure;

                int result = sc.ExecuteNonQuery();

                if (p.Id == 0)
                {
                    return Convert.ToInt32(sc.Parameters["NewPostId"].Value, CultureInfo.InvariantCulture);
                }
                else
                {
                    return p.Id;
                }
            }
        }