/// <summary> /// Adds or updates the given model in the database /// depending on its state. /// </summary> /// <param name="model">The model</param> public void Save(Models.PostType model) { var type = _db.PostTypes .FirstOrDefault(t => t.Id == model.Id); if (type == null) { type = new Data.PostType { Id = model.Id, Created = DateTime.Now }; _db.PostTypes.Add(type); } type.CLRType = model.CLRType; type.Body = JsonConvert.SerializeObject(model); type.LastModified = DateTime.Now; _db.SaveChanges(); lock (_typesMutex) { Load(); } }
/// <summary> /// Adds or updates the given model in the database /// depending on its state. /// </summary> /// <param name="model">The model</param> public void Save(Models.PostType model) { var type = db.PostTypes .FirstOrDefault(t => t.Id == model.Id); if (type == null) { type = new Data.PostType() { Id = model.Id, Created = DateTime.Now }; db.PostTypes.Add(type); } type.CLRType = model.CLRType; type.Body = JsonConvert.SerializeObject(model); type.LastModified = DateTime.Now; db.SaveChanges(); if (cache != null) { cache.Remove(model.Id.ToString()); } }
/// <summary> /// Builds the page type in the data store. /// </summary> /// <param name="api">The current api</param> internal void Build(Api api) { bool create = false; // Ensure slug if (String.IsNullOrWhiteSpace(Slug)) { Slug = Utils.GenerateSlug(Name); } // Create or update post type var type = api.PostTypes.GetSingle(t => t.Slug == Slug); if (type == null) { type = new Models.PostType() { Id = Guid.NewGuid(), Slug = Slug, }; create = true; } // Map post type type.Name = Name; type.Description = Description; type.Route = Route; type.View = View; type.CommentRoute = CommentRoute; type.ArchiveRoute = ArchiveRoute; type.ArchiveView = ArchiveRoute; // If we're creating the post type, seed // the default data. if (create) { type.EnableArchive = Defaults.EnableArchive; type.IncludeInRss = Defaults.IncludeInRss; type.ArchiveTitle = Defaults.ArchiveTitle; type.MetaKeywords = Defaults.MetKeywords; type.MetaDescription = Defaults.MetaDescription; } // Add the post type if its new if (create) { api.PostTypes.Add(type); } }
/// <summary> /// Test the post type repository. /// </summary> protected void Run() { using (var api = new Api()) { // Add new model var model = new Models.PostType() { Name = "Standard post", Route = "post" }; api.PostTypes.Add(model); api.SaveChanges(); } using (var api = new Api()) { // Get model var model = api.PostTypes.GetSingle(where : t => t.Slug == "standard-post"); Assert.IsNotNull(model); Assert.AreEqual("Standard post", model.Name); Assert.AreEqual("post", model.Route); // Update model model.Name = "Updated post"; api.SaveChanges(); } using (var api = new Api()) { // Verify update var model = api.PostTypes.GetSingle(where : t => t.Slug == "standard-post"); Assert.IsNotNull(model); Assert.AreEqual("Updated post", model.Name); Assert.AreEqual("post", model.Route); // Remove model api.PostTypes.Remove(model); api.SaveChanges(); } using (var api = new Api()) { // Verify remove var model = api.PostTypes.GetSingle(where : t => t.Slug == "standard-post"); Assert.IsNull(model); } }
/// <summary> /// Test the post type repository. /// </summary> protected void Run() { using (var api = new Api()) { // Add new model var model = new Models.PostType() { Name = "Standard post", Route = "post" }; api.PostTypes.Add(model); api.SaveChanges(); } using (var api = new Api()) { // Get model var model = api.PostTypes.GetSingle(where: t => t.Slug == "standard-post"); Assert.IsNotNull(model); Assert.AreEqual("Standard post", model.Name); Assert.AreEqual("post", model.Route); // Update model model.Name = "Updated post"; api.SaveChanges(); } using (var api = new Api()) { // Verify update var model = api.PostTypes.GetSingle(where: t => t.Slug == "standard-post"); Assert.IsNotNull(model); Assert.AreEqual("Updated post", model.Name); Assert.AreEqual("post", model.Route); // Remove model api.PostTypes.Remove(model); api.SaveChanges(); } using (var api = new Api()) { // Verify remove var model = api.PostTypes.GetSingle(where: t => t.Slug == "standard-post"); Assert.IsNull(model); } }
/// <summary> /// Gets the model with the specified id. /// </summary> /// <param name="id">The unique id</param> /// <returns></returns> public Models.PostType GetById(string id) { Models.PostType model = cache != null?cache.Get <Models.PostType>(id) : null; if (model == null) { var type = db.PostTypes .AsNoTracking() .FirstOrDefault(t => t.Id == id); if (type != null) { model = JsonConvert.DeserializeObject <Models.PostType>(type.Body); } if (cache != null && model != null) { cache.Set(model.Id, model); } } return(model); }
/// <summary> /// Test the rating repository. /// </summary> protected void Run() { var userId = Guid.NewGuid().ToString(); Models.PostType type = null; Models.Author author = null; Models.Post post = null; using (var api = new Api()) { // Add new post type type = new Models.PostType() { Name = "Rating post", Route = "post" }; api.PostTypes.Add(type); api.SaveChanges(); // Add new author author = new Models.Author() { Name = "Jim Doe", Email = "*****@*****.**" }; api.Authors.Add(author); api.SaveChanges(); // Add new post post = new Models.Post() { TypeId = type.Id, AuthorId = author.Id, Title = "My rated post", Excerpt = "Read my first post.", Body = "<p>Lorem ipsum</p>", Published = DateTime.Now }; api.Posts.Add(post); api.SaveChanges(); } using (var api = new Api()) { // Add ratings api.Ratings.AddRating(Models.RatingType.Star, post.Id, userId); api.Ratings.AddRating(Models.RatingType.Like, post.Id, userId); api.SaveChanges(); } using (var api = new Api()) { // Verify save var model = Client.Models.PostModel.GetById(post.Id).WithRatings(); Assert.AreEqual(1, model.Ratings.Stars.Count); Assert.AreEqual(1, model.Ratings.Likes.Count); // Remove ratings api.Ratings.RemoveRating(Models.RatingType.Star, post.Id, userId); api.Ratings.RemoveRating(Models.RatingType.Like, post.Id, userId); api.SaveChanges(); } using (var api = new Api()) { // Verify remove var model = Client.Models.PostModel.GetById(post.Id).WithRatings(); Assert.AreEqual(0, model.Ratings.Stars.Count); Assert.AreEqual(0, model.Ratings.Likes.Count); // Remove api.Posts.Remove(post.Id); api.PostTypes.Remove(type.Id); api.Authors.Remove(author.Id); api.SaveChanges(); } using (var api = new Api()) { // Verify remove post = api.Posts.GetSingle(where : p => p.Slug == "my-rated-post"); type = api.PostTypes.GetSingle(type.Id); author = api.Authors.GetSingle(author.Id); Assert.IsNull(post); Assert.IsNull(type); Assert.IsNull(author); } }
/// <summary> /// Test the rating repository. /// </summary> protected void Run() { var userId = Guid.NewGuid().ToString(); Models.PostType type = null; Models.Author author = null; Models.Post post = null; using (var api = new Api()) { // Add new post type type = new Models.PostType() { Name = "Rating post", Route = "post" }; api.PostTypes.Add(type); api.SaveChanges(); // Add new author author = new Models.Author() { Name = "Jim Doe", Email = "*****@*****.**" }; api.Authors.Add(author); api.SaveChanges(); // Add new post post = new Models.Post() { TypeId = type.Id, AuthorId = author.Id, Title = "My rated post", Excerpt = "Read my first post.", Body = "<p>Lorem ipsum</p>", Published = DateTime.Now }; api.Posts.Add(post); api.SaveChanges(); } using (var api = new Api()) { // Add ratings api.Ratings.AddRating(Models.RatingType.Star, post.Id, userId); api.Ratings.AddRating(Models.RatingType.Like, post.Id, userId); api.SaveChanges(); } using (var api = new Api()) { // Verify save var model = Client.Models.PostModel.GetById(post.Id).WithRatings(); Assert.AreEqual(1, model.Ratings.Stars.Count); Assert.AreEqual(1, model.Ratings.Likes.Count); // Remove ratings api.Ratings.RemoveRating(Models.RatingType.Star, post.Id, userId); api.Ratings.RemoveRating(Models.RatingType.Like, post.Id, userId); api.SaveChanges(); } using (var api = new Api()) { // Verify remove var model = Client.Models.PostModel.GetById(post.Id).WithRatings(); Assert.AreEqual(0, model.Ratings.Stars.Count); Assert.AreEqual(0, model.Ratings.Likes.Count); // Remove api.Posts.Remove(post.Id); api.PostTypes.Remove(type.Id); api.Authors.Remove(author.Id); api.SaveChanges(); } using (var api = new Api()) { // Verify remove post = api.Posts.GetSingle(where: p => p.Slug == "my-rated-post"); type = api.PostTypes.GetSingle(type.Id); author = api.Authors.GetSingle(author.Id); Assert.IsNull(post); Assert.IsNull(type); Assert.IsNull(author); } }
/// <summary> /// Builds the page type in the data store. /// </summary> /// <param name="api">The current api</param> internal void Build(Api api) { bool create = false; // Ensure slug if (String.IsNullOrWhiteSpace(Slug)) Slug = Utils.GenerateSlug(Name); // Create or update post type var type = api.PostTypes.GetSingle(t => t.Slug == Slug); if (type == null) { type = new Models.PostType() { Id = Guid.NewGuid(), Slug = Slug, }; create = true; } // Map post type type.Name = Name; type.Description = Description; type.Route = Route; type.View = View; type.CommentRoute = CommentRoute; type.ArchiveRoute = ArchiveRoute; type.ArchiveView = ArchiveRoute; // If we're creating the post type, seed // the default data. if (create) { type.EnableArchive = Defaults.EnableArchive; type.IncludeInRss = Defaults.IncludeInRss; type.ArchiveTitle = Defaults.ArchiveTitle; type.MetaKeywords = Defaults.MetKeywords; type.MetaDescription = Defaults.MetaDescription; } // Add the post type if its new if (create) api.PostTypes.Add(type); }
/// <summary> /// Deletes the given model. /// </summary> /// <param name="model">The model</param> public void Delete(Models.PostType model) { Delete(model.Id); }
/// <summary> /// Test the post repository. /// </summary> protected void Run() { Models.PostType type = null; Models.Author author = null; Models.Post post = null; using (var api = new Api()) { // Add new post type type = new Models.PostType() { Name = "Test post", Route = "post" }; api.PostTypes.Add(type); api.SaveChanges(); // Add new author author = new Models.Author() { Name = "Jane Doe", Email = "*****@*****.**" }; api.Authors.Add(author); api.SaveChanges(); // Add new post post = new Models.Post() { TypeId = type.Id, AuthorId = author.Id, Title = "My test post", Excerpt = "Read my first post.", Body = "<p>Lorem ipsum</p>", Published = DateTime.Now }; api.Posts.Add(post); api.SaveChanges(); } using (var api = new Api()) { // Get model var model = api.Posts.GetSingle(where : p => p.Slug == "my-test-post"); Assert.IsNotNull(model); Assert.AreEqual("Read my first post.", model.Excerpt); Assert.AreEqual("<p>Lorem ipsum</p>", model.Body); // Update model model.Excerpt = "Updated post"; api.SaveChanges(); } using (var api = new Api()) { // Verify update var model = api.Posts.GetSingle(where : p => p.Slug == "my-test-post"); Assert.IsNotNull(model); Assert.AreEqual("Updated post", model.Excerpt); Assert.AreEqual("<p>Lorem ipsum</p>", model.Body); // Remove api.Posts.Remove(model); api.PostTypes.Remove(type.Id); api.Authors.Remove(author.Id); api.SaveChanges(); } using (var api = new Api()) { // Verify remove post = api.Posts.GetSingle(where : p => p.Slug == "my-test-post"); type = api.PostTypes.GetSingle(type.Id); author = api.Authors.GetSingle(author.Id); Assert.IsNull(post); Assert.IsNull(type); Assert.IsNull(author); } }
/// <summary> /// Test the post repository. /// </summary> protected void Run() { Models.PostType type = null; Models.Author author = null; Models.Post post = null; using (var api = new Api()) { // Add new post type type = new Models.PostType() { Name = "Test post", Route = "post" }; api.PostTypes.Add(type); api.SaveChanges(); // Add new author author = new Models.Author() { Name = "Jane Doe", Email = "*****@*****.**" }; api.Authors.Add(author); api.SaveChanges(); // Add new post post = new Models.Post() { TypeId = type.Id, AuthorId = author.Id, Title = "My test post", Excerpt = "Read my first post.", Body = "<p>Lorem ipsum</p>", Published = DateTime.Now }; api.Posts.Add(post); api.SaveChanges(); } using (var api = new Api()) { // Get model var model = api.Posts.GetSingle(where: p => p.Slug == "my-test-post"); Assert.IsNotNull(model); Assert.AreEqual("Read my first post.", model.Excerpt); Assert.AreEqual("<p>Lorem ipsum</p>", model.Body); // Update model model.Excerpt = "Updated post"; api.SaveChanges(); } using (var api = new Api()) { // Verify update var model = api.Posts.GetSingle(where: p => p.Slug == "my-test-post"); Assert.IsNotNull(model); Assert.AreEqual("Updated post", model.Excerpt); Assert.AreEqual("<p>Lorem ipsum</p>", model.Body); // Remove api.Posts.Remove(model); api.PostTypes.Remove(type.Id); api.Authors.Remove(author.Id); api.SaveChanges(); } using (var api = new Api()) { // Verify remove post = api.Posts.GetSingle(where: p => p.Slug == "my-test-post"); type = api.PostTypes.GetSingle(type.Id); author = api.Authors.GetSingle(author.Id); Assert.IsNull(post); Assert.IsNull(type); Assert.IsNull(author); } }