コード例 #1
0
ファイル: TagService.cs プロジェクト: BrianMikinski/JustBlog
        public bool Save(TagViewModel tag)
        {
            JustBlogContext currentContext  = (JustBlogContext)_Context;
            Tag             currentCategory = new Tag()
            {
                Description = tag.Description,
                Id          = tag.Id.HasValue ? tag.Id.Value : -1,
                Modified    = DateTime.UtcNow,
                Name        = tag.Name,
                Posts       = null,
                UrlSlug     = tag.UrlSlug
            };

            //Edit an existing tag
            if (tag.Id.HasValue)
            {
                currentContext.Tags.Attach(currentCategory);
                currentContext.Entry(currentCategory).State = EntityState.Modified;
                currentContext.SaveChanges();

                return(true);
            }
            else //Add a new tag
            {
                currentContext.Tags.Add(currentCategory);
                return(currentContext.SaveChanges() > 0 ? true : false);
            }
        }
コード例 #2
0
        public void ConfigureInMemoryDatabase()
        {
            // must setup in this order! postSet depends on category and tag set!
            var categoryData = ConfigureCategoryDbSetMock();

            tagData  = ConfigureTagDbSetMock();
            postData = ConfigurePostsDbSetMock();

            // configure in memory database
            var options = new DbContextOptionsBuilder <JustBlogContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .EnableSensitiveDataLogging(true)
                          //.ReplaceService<IValueGeneratorCache, InMemoryValueGeneratorCache>() // used to get around generated key values
                          .Options;

            justBlogContext = new JustBlogContext(options);

            // save in memory data
            justBlogContext.AddRange(categoryData);
            justBlogContext.AddRange(postData);
            justBlogContext.AddRange(tagData);

            justBlogContext.SaveChanges();

            _categoryService = new CategoryService(justBlogContext);
            _postService     = new PostService(justBlogContext);
            _tagService      = new TagService(justBlogContext);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: dickdzaii/FA.JustBlog
        static void Main(string[] args)
        {
            JustBlogContext abc = new JustBlogContext();
            var             x   = abc.Posts.ToList();

            Console.WriteLine("HVD");
            Console.ReadKey();
        }
コード例 #4
0
        /// <summary>
        /// Load and edited post
        /// </summary>
        /// <returns></returns>
        private PostViewModel LoadEditedPost(JustBlogContext currentContext, Post currentPost)
        {
            currentContext.Posts.Attach(currentPost);

            currentContext.Entry(currentPost).Reference(p => p.Category).Load();
            currentContext.Entry(currentPost).Collection(p => p.PostTags).Load();

            PostViewModel editedPost = new PostViewModel()
            {
                Category = new CategoryViewModel()
                {
                    Description = currentPost.Category.Description,
                    Id          = currentPost.Category.Id,
                    Modified    = currentPost.Category.Modified,
                    Name        = currentPost.Category.Name,
                    PostCount   = currentPost.Category.Posts.Count,
                    Posts       = null,
                    UrlSlug     = currentPost.Category.UrlSlug
                },
                Description      = currentPost.Content,
                Id               = currentPost.Id,
                Meta             = currentPost.Meta,
                Modified         = currentPost.Modified,
                PostedOn         = currentPost.PostedOn,
                Published        = currentPost.Published,
                Tags             = currentPost?.Tags.ToList(),
                Title            = currentPost.Title,
                UrlSlug          = currentPost.UrlSlug,
                ShortDescription = currentPost.ShortDescription
            };

            List <Tag> newTags = new List <Tag>();

            foreach (Tag currentTag in editedPost.Tags)
            {
                newTags.Add(new Tag()
                {
                    Description = currentTag.Description,
                    Id          = currentTag.Id,
                    Modified    = currentTag.Modified,
                    Name        = currentTag.Name,
                    Posts       = new List <Post>(),
                    UrlSlug     = currentTag.UrlSlug
                });
            }

            editedPost.Tags = newTags;

            return(editedPost);
        }
コード例 #5
0
        /// <summary>
        /// Save an edited or new category
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public CategoryViewModel Save(CategoryViewModel category)
        {
            JustBlogContext currentContext  = (JustBlogContext)_Context;
            bool            saveSuccess     = false;
            Category        currentCategory = new Category()
            {
                Description = category.Description,
                Id          = category.Id.HasValue ? category.Id.Value : -1,
                Modified    = DateTime.UtcNow,
                Name        = category.Name,
                Posts       = null,
                UrlSlug     = category.UrlSlug
            };

            //Edit an existing category
            if (category.Id.HasValue)
            {
                currentContext.Categories.Attach(currentCategory);
                currentContext.Entry(currentCategory).State = EntityState.Modified;
                saveSuccess = currentContext.SaveChanges() > 0;
            }
            else //Add a new category
            {
                currentContext.Categories.Add(currentCategory);
                saveSuccess = currentContext.SaveChanges() > 0;
            }

            if (saveSuccess)
            {
                //Load posts after we save them
                currentContext.Entry(currentCategory).Collection(p => p.Posts).Load();

                return(new CategoryViewModel()
                {
                    Description = currentCategory.Description,
                    Id = currentCategory.Id,
                    Modified = currentCategory.Modified,
                    Name = currentCategory.Name,
                    PostCount = currentCategory != null ? currentCategory.Posts.Count : 0,
                    Posts = null,
                    UrlSlug = currentCategory.UrlSlug
                });
            }
            else
            {
                throw new JustBlogException($"Error saving changes to {currentCategory.Name}");
            }
        }
コード例 #6
0
        public UnitOfWork(JustBlogContext justBlogContext,
                          IRepository <Category> categoryRepository,
                          IRepository <Comment> commentRepository,
                          IRepository <Post> postRepository,
                          IRepository <Tag> tagRepository)
        {
            JustBlogContext    = justBlogContext;
            CategoryRepository = categoryRepository;
            CommentRepository  = commentRepository;
            PostRepository     = postRepository;
            TagRepository      = tagRepository;

            CategoryRepository.DbContext = JustBlogContext;
            CommentRepository.DbContext  = JustBlogContext;
            PostRepository.DbContext     = JustBlogContext;
            TagRepository.DbContext      = JustBlogContext;
        }
コード例 #7
0
        public PostViewModel Publish(int id)
        {
            JustBlogContext currentContext = (JustBlogContext)_Context;
            Post            currentPost    = currentContext.Posts.Attach(_Context.Posts.Find(id)).Entity;

            currentPost.Published = true;
            currentPost.PostedOn  = DateTime.UtcNow;
            currentPost.Modified  = DateTime.UtcNow;

            bool saveSuccessful = currentContext.SaveChanges() > 0;

            if (saveSuccessful)
            {
                return(LoadEditedPost(currentContext, currentPost));
            }
            else
            {
                throw new JustBlogException($"Post with Id: {id} could not be published");
            }
        }
コード例 #8
0
        public PostViewModel Unpublish(int postId)
        {
            JustBlogContext currentContext = (JustBlogContext)_Context;
            Post            currentPost    = currentContext.Posts.Attach(_Context.Posts.Find(postId)).Entity;

            currentPost.Published = false;
            currentPost.PostedOn  = null;
            currentPost.Modified  = DateTime.UtcNow;

            bool saveSuccessful = currentContext.SaveChanges() > 0;

            if (saveSuccessful)
            {
                return(LoadEditedPost(currentContext, currentPost));
            }
            else
            {
                throw new InvalidOperationException($"Post with Id: {postId} could not be unpublished");
            }
        }
コード例 #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PostRepository"/> class.
 /// </summary>
 public PostRepository()
 {
     this.blogContext = new JustBlogContext();
 }
コード例 #10
0
 public CommentRepository()
 {
     db = new JustBlogContext();
 }
コード例 #11
0
ファイル: GenericRepository.cs プロジェクト: naa13/JustBlog
 protected GenericRepository()
 {
     Context = new JustBlogContext();
     _dbSet  = Context.Set <TEntity>();
 }
コード例 #12
0
        public PostViewModel Save(PostViewModel currentPost, bool publishPost)
        {
            JustBlogContext currentContext  = (JustBlogContext)_Context;
            Post            newPost         = new Post();
            List <Tag>      postTags        = new List <Tag>();
            DateTime        currentDateTime = DateTime.UtcNow;
            bool            saveSuccess     = false;

            if (currentPost != null)
            {
                //Post
                newPost = new Post()
                {
                    CategoryId       = currentPost.Category.Id.Value,
                    Content          = currentPost.Description,
                    ShortDescription = currentPost.ShortDescription,
                    Title            = currentPost.Title,
                    UrlSlug          = currentPost.UrlSlug,
                    Published        = currentPost.Published || publishPost,
                    Meta             = currentPost.Meta == "" || currentPost.Meta == null ? "No meta data" : currentPost.Meta,
                    PostedOn         = currentPost.PostedOn,
                    Modified         = currentDateTime
                };

                //Tags
                foreach (Tag currentTag in currentPost?.Tags)
                {
                    postTags.Add(new Tag()
                    {
                        Description = currentTag.Description,
                        Id          = currentTag.Id,
                        Name        = currentTag.Name,
                        UrlSlug     = currentTag.UrlSlug
                    });
                }

                if (currentPost.PostedOn == null && publishPost)
                {
                    newPost.PostedOn = currentDateTime;
                }

                /**
                 * Note that you must track all entities
                 * from the Post side of the Post - PostTagMap - Tag database schema.
                 * If you incorrectly track entites you will add new tags as opposed to
                 * maintaining the many-to-many relationship.
                 **/
                if (currentPost?.Id == null)
                {
                    //Add a new post
                    //Attach tags from the database to post
                    foreach (Tag clientTag in postTags)
                    {
                        if (currentContext.Entry(clientTag).State == EntityState.Detached)
                        {
                            currentContext.Tags.Attach(clientTag);
                        }
                    }

                    newPost.Tags = postTags;
                    currentContext.Posts.Add(newPost);
                    saveSuccess = currentContext.SaveChanges() > 0;
                }
                else
                {
                    //Modify and existing post.
                    bool tagsModified = false;
                    newPost.Id = currentPost.Id.Value;
                    currentContext.Posts.Attach(newPost);
                    currentContext.Entry(newPost).State = EntityState.Modified;

                    saveSuccess = currentContext.SaveChanges() > 0 ? true : false;
                    List <Tag> dataTags = currentContext.Posts.Include(post => post.Tags).FirstOrDefault(p => p.Id == newPost.Id).Tags.ToList();

                    //Remove old tags
                    foreach (Tag tag in dataTags)
                    {
                        if (!postTags.Select(p => p.Id).ToList().Contains(tag.Id))
                        {
                            tagsModified = true;
                            newPost.Tags.Remove(tag);
                        }
                    }

                    if (postTags.Count > 0)
                    {
                        //Add new tags
                        foreach (Tag clientTag in postTags)
                        {
                            //Attach each tag because it comes from the client, not the database
                            if (!dataTags.Select(p => p.Id).ToList().Contains(clientTag.Id))
                            {
                                currentContext.Tags.Attach(clientTag);
                            }

                            if (!dataTags.Select(p => p.Id).ToList().Contains(clientTag.Id))
                            {
                                tagsModified = true;
                                newPost.Tags.Add(currentContext.Tags.Find(clientTag.Id));
                            }
                        }

                        //Only save changes if we modified the tags
                        if (tagsModified)
                        {
                            saveSuccess = currentContext.SaveChanges() > 0;
                        }
                    }
                }
            }

            if (saveSuccess)
            {
                return(LoadEditedPost(currentContext, newPost));
            }
            else
            {
                throw new JustBlogException($"Error saving changes to {newPost.Title}");
            }
        }
コード例 #13
0
 public CategoryRepository()
 {
     db = new JustBlogContext();
 }
コード例 #14
0
 public int SaveChanges()
 {
     return(JustBlogContext.SaveChanges());
 }
コード例 #15
0
 public AccountController(JustBlogContext context)
 {
     _context = context;
 }
コード例 #16
0
 public TagRepository()
 {
     db = new JustBlogContext();
 }
コード例 #17
0
 public void Dispose()
 {
     JustBlogContext.Dispose();
 }
コード例 #18
0
 public PostCategoryController(JustBlogContext context)
 {
     _context = context;
 }
コード例 #19
0
 protected GenericRepository()
 {
     MyDbContext = new JustBlogContext();
     MyDbSet     = MyDbContext.Set <TEntity>();
 }
コード例 #20
0
 public PostRepository()
 {
     db = new JustBlogContext();
 }
コード例 #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CategoryRepository"/> class.
 /// </summary>
 public CategoryRepository()
 {
     this.blogContext = new JustBlogContext();
 }