public async Task <Post> GetByIdAsync(Guid id) { using (var context = new BlogPostDbContext(new DbContextOptions <BlogPostDbContext>())) { var postEntity = await context.Posts .Include(pst => pst.Comments) .SingleOrDefaultAsync(pst => pst.Id == id); return(_mapper.Map <PostEntity, Post>(postEntity)); } }
public static void SeedHostDb(BlogPostDbContext context) { context.SuppressAutoSetTenantId = true; // Host seed new InitialHostDbBuilder(context).Create(); // Default tenant seed (in host database). new DefaultTenantBuilder(context).Create(); new TenantRoleAndUserBuilder(context, 1).Create(); }
public async Task <IEnumerable <Post> > GetAllAsync() { using (var context = new BlogPostDbContext(new DbContextOptions <BlogPostDbContext>())) { var posts = await context.Posts .OrderBy(pst => pst.PostDate) .ToListAsync(); return(_mapper.Map <IEnumerable <PostEntity>, IEnumerable <Post> >(posts)); } }
public async Task <Post> AddAsync(Post post) { using (var context = new BlogPostDbContext(new DbContextOptions <BlogPostDbContext>())) { var postEntity = _mapper.Map <Post, PostEntity>(post); var addedPost = await context.Set <PostEntity>().AddAsync(postEntity); await context.SaveChangesAsync(); return(_mapper.Map <PostEntity, Post>(addedPost.Entity)); } }
public async Task <Comment> AddAsync(Comment entity) { using (var context = new BlogPostDbContext(new DbContextOptions <BlogPostDbContext>())) { var postExists = await context.Set <PostEntity>().AnyAsync(x => x.Id == entity.PostId); if (!postExists) { throw new InvalidOperationException($"Comment can not be added. The Post {entity.PostId} can not be found."); } var commentEntity = _mapper.Map <Comment, CommentEntity>(entity); var addedComment = await context.Set <CommentEntity>().AddAsync(commentEntity); await context.SaveChangesAsync(); return(_mapper.Map <CommentEntity, Comment>(addedComment.Entity)); } }
public async Task <bool> RemoveAsync(Guid id) { using (var context = new BlogPostDbContext(new DbContextOptions <BlogPostDbContext>())) { var postEntity = await context.Posts .Include(pst => pst.Comments) .SingleOrDefaultAsync(pst => pst.Id == id); if (postEntity == null) { throw new InvalidOperationException($"The Post Id {id} can not be found."); } if (postEntity.Comments.Count != 0) { return(false); } context.Posts.Remove(postEntity); await context.SaveChangesAsync(); return(true); } }
public AdminController(BlogPostDbContext context, IMapper mapper) { _context = context; _mapper = mapper; }
public InitialHostDbBuilder(BlogPostDbContext context) { _context = context; }
public UnitOfWork(BlogPostDbContext context) { Context = context; }
public DefaultSettingsCreator(BlogPostDbContext context) { _context = context; }
public DefaultEditionCreator(BlogPostDbContext context) { _context = context; }
public TenantRoleAndUserBuilder(BlogPostDbContext context, int tenantId) { _context = context; _tenantId = tenantId; }
public DefaultLanguagesCreator(BlogPostDbContext context) { _context = context; }
public BlogPostMgmtSvc(BlogPostDbContext context) { _context = context; }
public DefaultTenantBuilder(BlogPostDbContext context) { _context = context; }
public HostRoleAndUserCreator(BlogPostDbContext context) { _context = context; }