public void Create(Post model) { using (var db = new CmsContext()) { var post = db.Posts.SingleOrDefault(p => p.Id == model.Id); if (post != null) { throw new ArgumentException("A post with the id of " + model.Id + " already exists."); } db.Posts.Add(model); db.SaveChanges(); } }
public void Delete(string id) { using (var db = new CmsContext()) { var post = db.Posts.SingleOrDefault(p => p.Id == id); if (post == null) { throw new KeyNotFoundException("The post with the id of " + id + " does not exist"); } db.Posts.Remove(post); db.SaveChanges(); } }
public void Edit(string id, Post updatedItem) { using (var db = new CmsContext()) { var post = db.Posts.SingleOrDefault(p => p.Id == id); if (post == null) { throw new KeyNotFoundException("A post with the id of " + id + " does not exist in the data store."); } post.Id = updatedItem.Id; post.Title = updatedItem.Title; post.Content = updatedItem.Content; post.Published = updatedItem.Published; post.Tags = updatedItem.Tags; db.SaveChanges(); } }
public void Delete(string tag) { using (var db = new CmsContext()) { var posts = db.Posts.Where(post => post.Tags.Contains(tag, StringComparer.CurrentCultureIgnoreCase)) .ToList(); if (!posts.Any()) { throw new KeyNotFoundException("The tag " + tag + " does not exist."); } foreach (var post in posts) { post.Tags.Remove(tag); } db.SaveChanges(); } }
public void Edit(string existingTag, string newTag) { using (var db = new CmsContext()) { var posts = db.Posts.Where(p => p.CombinedTags.Contains(existingTag)).ToList(); posts = posts .Where(t => t.Tags.Contains(existingTag, StringComparer.CurrentCultureIgnoreCase)) .ToList(); if (!posts.Any()) { throw new KeyNotFoundException("The tag " + existingTag + " does not exists."); } foreach (var post in posts) { post.Tags.Remove(existingTag); post.Tags.Add(newTag); } db.SaveChanges(); } }