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); } }
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); }
static void Main(string[] args) { JustBlogContext abc = new JustBlogContext(); var x = abc.Posts.ToList(); Console.WriteLine("HVD"); Console.ReadKey(); }
/// <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); }
/// <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}"); } }
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; }
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"); } }
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"); } }
/// <summary> /// Initializes a new instance of the <see cref="PostRepository"/> class. /// </summary> public PostRepository() { this.blogContext = new JustBlogContext(); }
public CommentRepository() { db = new JustBlogContext(); }
protected GenericRepository() { Context = new JustBlogContext(); _dbSet = Context.Set <TEntity>(); }
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}"); } }
public CategoryRepository() { db = new JustBlogContext(); }
public int SaveChanges() { return(JustBlogContext.SaveChanges()); }
public AccountController(JustBlogContext context) { _context = context; }
public TagRepository() { db = new JustBlogContext(); }
public void Dispose() { JustBlogContext.Dispose(); }
public PostCategoryController(JustBlogContext context) { _context = context; }
protected GenericRepository() { MyDbContext = new JustBlogContext(); MyDbSet = MyDbContext.Set <TEntity>(); }
public PostRepository() { db = new JustBlogContext(); }
/// <summary> /// Initializes a new instance of the <see cref="CategoryRepository"/> class. /// </summary> public CategoryRepository() { this.blogContext = new JustBlogContext(); }