// Compare 2 attribute strings, attributes (new attributes) and old_attr (old attributes). public void manageAttributes(Hypster_Entities hyDB, newsManagement_Admin newsManager, int id, string attributes, string old_attr) { if (attributes != old_attr) { string[] attr = attributes.Split(';'); for (int i = 0; i < attr.Length; i++) { if (!old_attr.Contains(attr[i])) { postNewsletter pNewsletter = new postNewsletter(); pNewsletter.post_id = id; pNewsletter.attribute = attr[i]; hyDB.postNewsletters.AddObject(pNewsletter); hyDB.SaveChanges(); } } string[] o_attr = old_attr.Split(';'); for (int j = 0; j < o_attr.Length; j++) { if (!attributes.Contains(o_attr[j])) { newsManager.DeletePostAttribute(id, o_attr[j]); hyDB.SaveChanges(); } } } }
public ActionResult AddNewPost(newsPost p_newPost, HttpPostedFileBase file, string scheduled, string datetimepicker, string postgenres, string attributes) { Hypster_Entities hyDB = new Hypster_Entities(); newsManagement_Admin newsManager_admin = new newsManagement_Admin(); newsManagement newsManager = new newsManagement(); if (p_newPost.post_title != null && p_newPost.post_title != "") { if (p_newPost.post_content == null || p_newPost.post_content == "") { ModelState.AddModelError("", "Please Enter the Post Content; it is REQUIRED!!"); } if (p_newPost.post_short_content == null || p_newPost.post_short_content == "") { ModelState.AddModelError("", "Please Enter the Short Post Content; it is REQUIRED!!"); } p_newPost.post_date = DateTime.Now; p_newPost.post_guid = p_newPost.post_title.Replace("/", "").Replace("\\", "").Replace("&", "").Replace("+", "").Replace(" ", "-").Replace("?", "").Replace("!", "").Replace("*", "").Replace("$", "").Replace("\"", "").Replace("'", "").Replace("{", "").Replace("}", "").Replace(")", "").Replace("(", "").Replace("[", "").Replace("]", "").Replace("|", "").Replace(".", "").Replace(",", "").Replace(":", "").Replace(";", ""); p_newPost.post_status = (int)postStatus.NoActive; // //check if post guid is exist in database newsPost post_check = newsManager_admin.GetPostByGUID(p_newPost.post_guid); if (post_check.post_id != 0 && newsManager_admin.GetPostByGUID(post_check.post_guid).post_guid != "") { ModelState.AddModelError("", "NOT ABLE TO GENERATE POST GUID.Please choose modify title. Post with following title already exist."); } else //if post guid is unique then procceed to finish { hyDB.newsPosts.AddObject(p_newPost); hyDB.SaveChanges(); int id = p_newPost.post_id; UploadImage(file, id); if (scheduled == "Yes") { ScheduledPost sPost = new ScheduledPost(); sPost.post_id = id; sPost.scheduled_date = convertDateTime(datetimepicker); sPost.activated = 0; hyDB.ScheduledPost.AddObject(sPost); hyDB.SaveChanges(); } if (attributes != "") { string[] attribute = attributes.Split(';'); foreach (string attr in attribute) { postNewsletter pNewsletter = new postNewsletter(); pNewsletter.post_id = id; pNewsletter.attribute = attr; hyDB.postNewsletters.AddObject(pNewsletter); hyDB.SaveChanges(); } } if (postgenres.Length != 0) { string[] postgenre = postgenres.Split(';'); for (int i = 0; i < postgenre.Length; i++) { Post_Genre postGenre = new Post_Genre(); postGenre.post_id = id; postGenre.genre_id = Convert.ToInt32(postgenre[i]); hyDB.Post_Genre.AddObject(postGenre); hyDB.SaveChanges(); } } return(RedirectToAction("Index", "managePost")); } } else { ModelState.AddModelError("", "Please enter post title. It must be unique from previous posts!!!"); } // if no success return(View(new newsPost())); }