Esempio n. 1
0
 public async Task CreateAsync(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);
         await db.SaveChangesAsync();
     }
 }
Esempio n. 2
0
        public async Task DeleteAsync(string id)
        {
            using (var db = new CmsContext())
            {
                var post = db.Posts.FirstOrDefault(p => p.Id == id);
                if (post == null)
                {
                    throw new KeyNotFoundException("The post with the id" + id + " doesnot exist");
                }


                db.Posts.Remove(post);
                await db.SaveChangesAsync();
            }
        }
Esempio n. 3
0
        public async Task EditAsync(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;

                await db.SaveChangesAsync();
            }
        }
Esempio n. 4
0
        public async Task EditAsync(string existingTag, string newTag)
        {
            using (var db = new CmsContext())
            {
                var posts = db.Posts.Where(post => post.CombinedTags.Contains(existingTag)).ToList();

                posts = posts.Where(post =>
                                    post.Tags.Contains(existingTag, StringComparer.CurrentCultureIgnoreCase)).ToList();

                if (!posts.Any())
                {
                    throw new KeyNotFoundException("The tag " + existingTag + "does not exist");
                }
                foreach (var post in posts)
                {
                    post.Tags.Remove(existingTag);
                    post.Tags.Add(newTag);
                }

                await db.SaveChangesAsync();
            }
        }