Exemple #1
0
    public int SavePost(HttpRequestBase Request, ModelStateDictionary ms)
    {
        int postId, userId;
        string Title = Request.Unvalidated("Title"),
               Content = Request.Unvalidated("Content");

        int.TryParse(Request.Unvalidated().Form["id"], out postId);
        int.TryParse(Request.Unvalidated().Form["UserId"], out userId);

        if(string.IsNullOrWhiteSpace(Title))
            ms.AddError("Title", "Title cannot be empty!");
        if(Title.Length > 100)
            ms.AddError("Title", "Title is too long!");
        if(string.IsNullOrWhiteSpace(Content))
            ms.AddError("Content", "You have to post something!");

        if(!ms.IsValid)
            return 0;

        // tiny_mce bug?
        Content = Content.Replace("<p>&nbsp;</p>", "");

        if(postId <= 0) {
            // Insert
            string Slug = string.Empty;
            foreach(var c in Title)
                if(char.IsLetterOrDigit(c))
                    Slug += c;
                else if(char.IsWhiteSpace(c))
                    Slug += "-";

            db.Execute("INSERT INTO Posts (UserId, Published, Title, Content, Slug) VALUES (@0, @1, @2, @3, @4)", userId, DateTime.Now, Title, Content, Slug);
            postId = (int)db.GetLastInsertId();
        } else {
            // Update
            db.Execute("UPDATE Posts SET Title=@0, Content=@1, Modified=@2 WHERE id=@3", Title, Content, DateTime.Now, postId);
        }

        return postId;
    }
 private static string GetQueryStringValue(HttpRequestBase request, string key)
 {
     return request.Unvalidated().Form[key] ?? request.QueryString[key];
 }