public ActionResult Save(List<string> categories, int id = 0, int userid = 0, bool publish = false, string title = "", int contentID = 0, string content = "", string meta_title = "", string meta_description = "", string keywords = "") { try { if (title.Length == 0) { throw new Exception("You must enter a title for the post"); } CurtDevDataContext db = new CurtDevDataContext(); if (id == 0) { BlogPost post = new BlogPost { userID = userid, post_title = title, slug = UDF.GenerateSlug(title), post_text = content, createdDate = DateTime.Now, lastModified = DateTime.Now, meta_title = meta_title.Trim(), meta_description = meta_description.Trim(), keywords = keywords, active = true }; if (publish) post.publishedDate = DateTime.Now; db.BlogPosts.InsertOnSubmit(post); db.SubmitChanges(); if (categories.Count() > 0) { foreach (string cat in categories) { if (cat != "") { try { BlogPost_BlogCategory postcat = new BlogPost_BlogCategory { blogPostID = post.blogPostID, blogCategoryID = Convert.ToInt32(cat) }; db.BlogPost_BlogCategories.InsertOnSubmit(postcat); } catch { } } } } } else { BlogPost post = db.BlogPosts.Where(x => x.blogPostID == id).FirstOrDefault<BlogPost>(); post.meta_title = meta_title.Trim(); post.meta_description = meta_description.Trim(); post.keywords = keywords.Trim(); post.userID = userid; post.lastModified = DateTime.Now; post.post_title = title.Trim(); post.slug = UDF.GenerateSlug(title.Trim()); post.post_text = content.Trim(); if (publish) { if (post.publishedDate == null) { post.publishedDate = DateTime.Now; } } else { post.publishedDate = null; } List<BlogPost_BlogCategory> postcats = db.BlogPost_BlogCategories.Where(x => x.blogPostID == post.blogPostID).ToList<BlogPost_BlogCategory>(); db.BlogPost_BlogCategories.DeleteAllOnSubmit<BlogPost_BlogCategory>(postcats); db.SubmitChanges(); if (categories.Count() > 0) { foreach (string cat in categories) { if (cat != "") { try { BlogPost_BlogCategory postcat = new BlogPost_BlogCategory { blogPostID = post.blogPostID, blogCategoryID = Convert.ToInt32(cat) }; db.BlogPost_BlogCategories.InsertOnSubmit(postcat); } catch { } } } } } db.SubmitChanges(); return RedirectToAction("Index"); } catch (Exception e) { ViewBag.err = e.Message; ViewBag.content = content; ViewBag.title = title; ViewBag.meta_title = meta_title; ViewBag.meta_description = meta_description; ViewBag.keywords = keywords; ViewBag.userid = userid; if (id == 0) { return RedirectToAction("Add", new { err = e.Message }); } else { return RedirectToAction("Edit", new { id = id, err = e.Message }); } } }
public static int Save(PostWithCategories p, bool publish = false) { EcommercePlatformDataContext db = new EcommercePlatformDataContext(); BlogPost post = new BlogPost(); if (p.blogPostID == 0) { post = new BlogPost { profileID = p.profileID, post_title = p.post_title, slug = p.slug, post_text = p.post_text, createdDate = DateTime.Now, lastModified = DateTime.Now, meta_title = p.meta_title, meta_description = p.meta_description, keywords = p.keywords, active = true }; if (publish) post.publishedDate = DateTime.Now; db.BlogPosts.InsertOnSubmit(post); db.SubmitChanges(); if (p.categories.Count() > 0) { foreach (BlogCategory cat in p.categories) { if (cat.blogCategoryID != 0) { try { BlogPost_BlogCategory postcat = new BlogPost_BlogCategory { blogPostID = post.blogPostID, blogCategoryID = cat.blogCategoryID }; db.BlogPost_BlogCategories.InsertOnSubmit(postcat); } catch { } } } } } else { post = db.BlogPosts.Where(x => x.blogPostID == p.blogPostID).FirstOrDefault<BlogPost>(); post.meta_title = p.meta_title; post.meta_description = p.meta_description; post.keywords = p.keywords; post.profileID = p.profileID; post.lastModified = DateTime.Now; post.post_title = p.post_title; post.slug = p.slug; post.post_text = p.post_text; if (publish) { if (post.publishedDate == null) { post.publishedDate = DateTime.Now; } } else { post.publishedDate = null; } List<BlogPost_BlogCategory> postcats = db.BlogPost_BlogCategories.Where(x => x.blogPostID == post.blogPostID).ToList<BlogPost_BlogCategory>(); db.BlogPost_BlogCategories.DeleteAllOnSubmit<BlogPost_BlogCategory>(postcats); db.SubmitChanges(); if (p.categories.Count() > 0) { foreach (BlogCategory cat in p.categories) { if (cat.blogCategoryID != 0) { try { BlogPost_BlogCategory postcat = new BlogPost_BlogCategory { blogPostID = post.blogPostID, blogCategoryID = cat.blogCategoryID }; db.BlogPost_BlogCategories.InsertOnSubmit(postcat); } catch { } } } } } db.SubmitChanges(); return post.blogPostID; }