Пример #1
0
        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();
        }
Пример #3
0
        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));
            }
        }
Пример #4
0
        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));
            }
        }
Пример #5
0
        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));
            }
        }
Пример #6
0
        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;
 }
Пример #8
0
 public InitialHostDbBuilder(BlogPostDbContext context)
 {
     _context = context;
 }
 public UnitOfWork(BlogPostDbContext context)
 {
     Context = context;
 }
 public DefaultSettingsCreator(BlogPostDbContext context)
 {
     _context = context;
 }
Пример #11
0
 public DefaultEditionCreator(BlogPostDbContext context)
 {
     _context = context;
 }
Пример #12
0
 public TenantRoleAndUserBuilder(BlogPostDbContext context, int tenantId)
 {
     _context  = context;
     _tenantId = tenantId;
 }
Пример #13
0
 public DefaultLanguagesCreator(BlogPostDbContext context)
 {
     _context = context;
 }
Пример #14
0
 public BlogPostMgmtSvc(BlogPostDbContext context)
 {
     _context = context;
 }
Пример #15
0
 public DefaultTenantBuilder(BlogPostDbContext context)
 {
     _context = context;
 }
 public HostRoleAndUserCreator(BlogPostDbContext context)
 {
     _context = context;
 }