public void UpdateContent(BlogPostModelBinding Model) { this.Title = Model.Title; this.Published = Model.Published; this.Content = Model.Content; this.ContentPreview = Model.ContentPreview; this.FriendlyUrl = Model.FriendlyUrl; }
public int Save(BlogPostModelBinding Model, string UserID) { using (var _c = db) { Can(Model.SiteID, UserID, _c); var _BlogPost = new BlogPost(Model.SiteID); if (string.IsNullOrEmpty(Model.FriendlyUrl)) Model.FriendlyUrl = Model.Title.CleanUrl(); if (Model.BlogPostID == 0) { var _BlogPostCulture = new BlogPostCulture(Model); _BlogPostCulture.DateCreated = DateTime.Now; //ADD TAGS if (Model.Tags != null) { _BlogPostCulture.BlogPostTags = new List<BlogPostTag>(); foreach (var item in Model.Tags) { var _BTID = GetTagID(item.BlogTagName); _BlogPostCulture.BlogPostTags.Add(new BlogPostTag { BlogTagID = GetTagID(item.BlogTagName), CultureID = Model.CultureID }); } } _BlogPost.BlogPostCultures = new List<BlogPostCulture>(); _BlogPost.BlogPostCultures.Add(_BlogPostCulture); _c.BlogPosts.Add(_BlogPost); //CATEGORIES if (Model.Categories != null) { _BlogPost.BlogPostCategories = new List<BlogPostCategory>(); foreach (var item in Model.Categories) { _BlogPost.BlogPostCategories.Add(new BlogPostCategory { BlogCategoryID = item.BlogCategoryID }); } } _c.SaveChanges(); return _BlogPost.BlogPostID; } else { var _BP = _c.BlogPostCultures .Where(m => m.CultureID == Model.CultureID && m.BlogPostID == Model.BlogPostID) .FirstOrDefault(); _BP.UpdateContent(Model); //CATEGORIES if (Model.Categories != null) { var _C = Model.Categories .Where(m => m.Adding) .Select(m => new BlogPostCategory { BlogCategoryID = m.BlogCategoryID, BlogPostID = Model.BlogPostID }); foreach (var item in _C.ToList()) { if (!_c.BlogPostCategories .Where(m => m.BlogCategoryID == item.BlogCategoryID && m.BlogPostID == item.BlogPostID).Any()) { _BP.BlogPost.BlogPostCategories.Add(item); } } var _CToDelete = Model.Categories .Where(m => m.Deleting).Select(m => m.BlogCategoryID); _c.BlogPostCategories.RemoveRange(_c.BlogPostCategories .Where(m => _CToDelete.Contains(m.BlogCategoryID)) .ToList()); } //ADD TAGS if (Model.Tags != null) { foreach (var item in Model.Tags) { var _BTID = GetTagID(item.BlogTagName); var _BPT = _c.BlogPostTags .Where(m => m.BlogTagID == _BTID && m.BlogPostID == Model.BlogPostID && m.CultureID == Model.CultureID) .FirstOrDefault(); if (item.Adding && _BPT == null) { _BPT = new BlogPostTag(item, _BTID, Model.CultureID); _BP.BlogPostTags.Add(_BPT); } else if (item.Deleting && _BPT != null) { _c.BlogPostTags.Remove(_BPT); } } } _c.SaveChanges(); int _BlogPostID = Model.BlogPostID != 0 ? Model.BlogPostID : _BlogPost.BlogPostID; //ADD FEATURE IMAGE if (Model.FeatureImageFileRead != null) { if (Model.FeatureImageFileRead.Deleting) { if (Model.FeatureImageFileRead.MediaID != 0) { new MediaBLL().DeleteMedia(Model.FeatureImageFileRead.MediaID, Model.SiteID, UserID); } } else { if (Model.FeatureImageFileRead != null && !string.IsNullOrEmpty(Model.FeatureImageFileRead.FileContent)) { new MediaBLL().DeleteMedia(Model.FeatureImageFileRead.MediaID, Model.SiteID, UserID); var _Media = new MediaBLL().SaveImage(Model.FeatureImageFileRead, Model.SiteID, UserID); _c.BlogPostFeatureImages.Add(new BlogPostFeatureImage { BlogPostID = _BlogPostID, CultureID = Model.CultureID, MediaID = _Media.MediaID }); _c.SaveChanges(); } } } //ADD IMAGES if (Model.ImagesFileRead != null) { foreach (var item in Model.ImagesFileRead) { var _BPI = _c.BlogPostImages .Where(m => m.BlogPostID == _BlogPostID && m.CultureID == Model.CultureID && m.MediaID == item.MediaID).FirstOrDefault(); if (item.Adding && _BPI == null) { _c.BlogPostImages.Add(new BlogPostImages { BlogPostID = _BlogPostID, CultureID = Model.CultureID, MediaID = item.MediaID }); } else if (item.Deleting && _BPI != null) { new MediaBLL().DeleteMedia(item.MediaID, Model.SiteID, UserID); } } _c.SaveChanges(); } return _BP.BlogPostID; } } }
private void SettingUp() { //CREATING CATEGORIES var _Categories = new List<BlogCategoryCultureBase>(); _Categories.Add(new BlogCategoryCultureBase { BlogCategoryName = "c1-es", CultureID = _Cultures[0].CultureID, }); _Categories.Add(new BlogCategoryCultureBase { BlogCategoryName = "c1-en", CultureID = _Cultures[1].CultureID }); _Category = new BlogCategoryModelBinding { SiteID = _SiteID, CategoryCultures = _Categories }; _Category.BlogCategoryID = new BlogBLL().SaveCategory(_Category, _CustomerUser.Id); //CREATING TAGS var _Tags = new List<BlogTagModelBinding>(); _Tags.Add(new BlogTagModelBinding { BlogTagName = "t1" }); _Tags.Add(new BlogTagModelBinding { BlogTagName = "t2" }); //CREATING POSTS _BlogPost = new BlogPostModelBinding { SiteID = _SiteID, CultureID = _Cultures[0].CultureID, Title = "b1", Tags = _Tags, Published = true }; _BlogPost.BlogPostID = new BlogBLL().Save(_BlogPost, _CustomerUser.Id); Assert.IsTrue(_BlogPost.BlogPostID != 0); _BlogPost2 = new BlogPostModelBinding { SiteID = _SiteID, CultureID = _Cultures[0].CultureID, Title = "b2", Published = true }; _BlogPost2.BlogPostID = new BlogBLL().Save(_BlogPost2, _CustomerUser.Id); Assert.IsTrue(_BlogPost2.BlogPostID != 0); _BlogPost3 = new BlogPostModelBinding { SiteID = _SiteID, CultureID = _Cultures[1].CultureID, Title = "b3", Published = true, FriendlyUrl="b3" }; _BlogPost3.BlogPostID = new BlogBLL().Save(_BlogPost3, _CustomerUser.Id); Assert.IsTrue(_BlogPost3.BlogPostID != 0); }
public IHttpActionResult PostSave(BlogPostModelBinding Model) { return Ok(_BlogService.Save(Model, User.Identity.GetUserId())); }
public BlogPostCulture(BlogPostModelBinding Model) : this() { this.CultureID = Model.CultureID; UpdateContent(Model); }