public void Can_Create_Blog_With_Mapper()
        {
            var connectionString = ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString;
            using (var connection = new SqlConnection(connectionString))
            { 
                connection.Open();
                using (var transaction = connection.BeginTransaction())
                { 
                    var metaDataStore = new MetaDataStore();
                    metaDataStore.BuildTableInfoFor<Blog>();
                    var identityMap = new IdentityMap();
                    var hydrater = new EntityHydrater(metaDataStore, identityMap);
                    BlogMapper blogMapper = new BlogMapper(connection, transaction, metaDataStore, hydrater, identityMap);

                    Blog blog = new Blog { Name = "PROMPT", Description = "Módulo 5 - Plataformas e modelos de acesso a dados" };
                    blog = blogMapper.Insert(blog);

                    Assert.IsNotNull(blog);
                    Assert.IsTrue(blog.Id > 0);
                    Assert.AreEqual("PROMPT", blog.Name);
                    Assert.AreEqual("Módulo 5 - Plataformas e modelos de acesso a dados", blog.Description);

                    transaction.Commit();
                }
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Put(int id, [FromBody] BlogDTO blogDTO)
        {
            if (id != blogDTO.Id)
            {
                return(BadRequest());
            }

            Blog blog = BlogMapper.mapSingleToBlog(blogDTO);

            try
            {
                await _blogService.UpdateAsync(blog);
            }
            catch (DBConcurrencyException e)
            {
                if (await _blogService.GetByIdAsync(id) == null)
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
 void dummy()
 {
     BlogMapper m = new BlogMapper();
     Blog b = new Blog();
     m.Insert(b);
     b = m.Get(1);
 }
Ejemplo n.º 4
0
        public async Task <IActionResult> Post([FromBody] BlogDTO blogDTO)
        {
            Blog blog = BlogMapper.mapSingleToBlog(blogDTO);
            await _blogService.AddAsync(blog);

            return(CreatedAtAction(nameof(Get), new { id = blog.Id }, blogDTO));
        }
 public void TestMethod1()
 {
     BlogMapper m = new BlogMapper();
     Blog b = new Blog();
     m.Insert(b);
     b = m.Get(1);
 }
Ejemplo n.º 6
0
        public async Task <IActionResult> Get()
        {
            IEnumerable <Blog> blog = await _blogService.GetAsync();

            IEnumerable <BlogDTO> blogDTOs = BlogMapper.mapManyToDTO(blog);

            return(Ok(blogDTOs));
        }
Ejemplo n.º 7
0
        public async Task <IEnumerable <BlogDTO> > GetAllBlogsByUserId(string id)
        {
            var userEntity = await _userManager.FindByIdAsync(id);

            if (userEntity == null)
            {
                throw new ArgumentNullException(nameof(userEntity), "Couldn't find user with this id");
            }
            var blogs = _unitOfWork.BlogRepository.Get(b => b.OwnerId == id);

            return(BlogMapper.Map(blogs));
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> Get(int id)
        {
            Blog blog = await _blogService.GetByIdAsync(id);

            if (blog == null)
            {
                return(NotFound());
            }

            BlogDTO blogDTO = BlogMapper.mapSingleToDTO(blog);

            return(Ok(blogDTO));
        }
Ejemplo n.º 9
0
        private BlogUserDto MapReceivedUser(BlogUser userMapFrom)
        {
            var         userMapper = new BlogUserMapper();
            var         blogMapper = new BlogMapper();
            BlogUserDto userDTO    = userMapper.MapToBlogUserDto(userMapFrom);

            if (userMapFrom.UserBlog != null)
            {
                userDTO.UserBlog = blogMapper.MapToBlogDto(userMapFrom.UserBlog);
            }

            return(userDTO);
        }
        public void Can_FindAll_Blogs_With_Mapper()
        {
            // Prepare
            var connectionString = ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString;
            using (var connection = new SqlConnection(connectionString))
            { 
                connection.Open();
                using (var transaction = connection.BeginTransaction())
                { 
                    var metaDataStore = new MetaDataStore();
                    metaDataStore.BuildTableInfoFor<Blog>();
                    var identityMap = new IdentityMap();
                    var hydrater = new EntityHydrater(metaDataStore, identityMap);
                    BlogMapper blogMapper = new BlogMapper(connection, transaction, metaDataStore, hydrater, identityMap);

                    for (int i = 0; i < 10; i++)
                    {
                        Blog blog = new Blog { Name = "PROMPT " + i, Description = "Módulo " + i + " - Plataformas e modelos de acesso a dados" };
                        blogMapper.Insert(blog);
                    }
                    transaction.Commit();
                }
            }

            // Test with whole new references
            using (var connection = new SqlConnection(connectionString))
            { 
                connection.Open();
                using (var transaction = connection.BeginTransaction())
                { 
                    var metaDataStore = new MetaDataStore();
                    metaDataStore.BuildTableInfoFor<Blog>();
                    var identityMap = new IdentityMap();
                    var hydrater = new EntityHydrater(metaDataStore, identityMap);
                    BlogMapper blogMapper = new BlogMapper(connection, transaction, metaDataStore, hydrater, identityMap);
                    
                    var allBlogs = blogMapper.FindAll();
                    
                    Assert.AreEqual(10, allBlogs.Count());
                    for (int i = 0; i < 10; i++)
                    {
                        int blogi = i;
                        var blog = allBlogs.SingleOrDefault(b => b.Name == "PROMPT " + blogi);
                        Assert.IsNotNull(blog);
                    }

                    transaction.Commit();
                }
            }
        }
Ejemplo n.º 11
0
        public async Task <BlogDTO> AddAsync(BlogDTO dtoModel)
        {
            await this.ValidateAsync(dtoModel);

            Blog blog = new Blog();

            dtoModel.DateCreated      = DateTime.Now;
            dtoModel.LastDateModified = DateTime.Now;
            BlogMapper.MapBlogFromBlogDto(ref blog, ref dtoModel);
            this.repository.Add(blog);
            await this.repository.SaveChangesAsync();

            dtoModel.Id = blog.Id;
            return(dtoModel);
        }
Ejemplo n.º 12
0
        public async Task <BlogDTO> UpdateAsync(BlogDTO dtoModel)
        {
            await this.ValidateAsync(dtoModel);

            var blogEntity = await this.repository.Get(dtoModel.Id).FirstOrDefaultAsync();

            if (blogEntity == null)
            {
                throw new Exception("Blog for update not found");
            }
            dtoModel.LastDateModified = DateTime.Now;
            BlogMapper.MapBlogFromBlogDto(ref blogEntity, ref dtoModel);
            this.repository.Update(blogEntity);
            await this.repository.SaveChangesAsync();

            return(dtoModel);
        }
Ejemplo n.º 13
0
        public BlogDTO GetBlogById(int id)
        {
            var blog = _unitOfWork.BlogRepository.Get((b => b.Id == id), includeProperties: "Articles,Owner").FirstOrDefault();

            if (blog == null)
            {
                throw new ArgumentNullException(nameof(blog), "Couldn't find blog by id");
            }
            var dto = BlogMapper.Map(blog);

            if (blog.Articles.Count > 0)
            {
                dto.Articles = ArticleMapper.Map(blog.Articles);
            }
            if (blog.Owner != null)
            {
                dto.OwnerUsername = blog.Owner.UserName;
            }
            return(dto);
        }
Ejemplo n.º 14
0
        private PostDto MapReceivedPost(Post postMapFrom)
        {
            var postMapper = new PostMapper();

            PostDto postDTO = postMapper.MapToPostDto(postMapFrom);

            if (postMapFrom.RelatedTo != null)
            {
                var blogMapper = new BlogMapper();
                postDTO.RelatedTo = blogMapper.MapToBlogDto(postMapFrom.RelatedTo);
            }

            if (postMapFrom.PostCategories != null)
            {
                var categoryMapper = new CategoryMapper();
                postDTO.PostCategories = categoryMapper.MapToCategoryDtoList(postMapFrom.PostCategories);
            }

            if (postMapFrom.PostComments != null)
            {
                var commentMapper  = new CommentMapper();
                var userMapper     = new BlogUserMapper();
                var commentDTOList = new List <CommentDto>();
                foreach (var comment in postMapFrom.PostComments)
                {
                    var commentDTOToAdd = commentMapper.MapToCommentDto(comment);
                    if (comment.CreatedBy != null)
                    {
                        commentDTOToAdd.CreatedBy = userMapper.MapToBlogUserDto(comment.CreatedBy);
                    }
                    commentDTOList.Add(commentDTOToAdd);
                }
                postDTO.PostComments = commentDTOList;
            }

            return(postDTO);
        }
Ejemplo n.º 15
0
        private BlogDto MapReceivedBlog(Blog blogMapFrom)
        {
            var blogMapper     = new BlogMapper();
            var categoryMapper = new CategoryMapper();

            BlogDto blogDTO = blogMapper.MapToBlogDto(blogMapFrom);

            if (blogMapFrom.BlogCategories != null)
            {
                blogDTO.BlogCategories = categoryMapper.MapToCategoryDtoList(blogMapFrom.BlogCategories);
            }

            if (blogMapFrom.BlogPosts != null)
            {
                var postMapper  = new PostMapper();
                var postDTOList = new List <PostDto>();
                foreach (var post in blogMapFrom.BlogPosts)
                {
                    var postDTOToAdd = postMapper.MapToPostDto(post);
                    if (post.PostCategories != null)
                    {
                        postDTOToAdd.PostCategories = categoryMapper.MapToCategoryDtoList(post.PostCategories);
                    }
                    postDTOList.Add(postDTOToAdd);
                }

                blogDTO.BlogPosts = postDTOList;
            }

            if (blogMapFrom.CreatedBy != null)
            {
                var userMapper = new BlogUserMapper();
                blogDTO.CreatedBy = userMapper.MapToBlogUserDto(blogMapFrom.CreatedBy);
            }

            return(blogDTO);
        }
Ejemplo n.º 16
0
        public async Task <BlogDTO> CreateBlog(BlogDTO blog, string token)
        {
            if (blog == null)
            {
                throw new ArgumentNullException(nameof(blog));
            }
            string claimsId   = _jwtFactory.GetUserIdClaim(token);
            var    blogEntity = BlogMapper.Map(blog);

            blogEntity.OwnerId = claimsId;

            _unitOfWork.BlogRepository.Insert(blogEntity);
            await _unitOfWork.SaveAsync();

            blogEntity = _unitOfWork.BlogRepository.Get(b => b.Name == blog.Name, includeProperties: "Owner").FirstOrDefault();
            if (blogEntity == null)
            {
                throw new ArgumentNullException(nameof(blogEntity));
            }
            var result = BlogMapper.Map(blogEntity);

            result.OwnerUsername = blogEntity.Owner.UserName;
            return(BlogMapper.Map(blogEntity));
        }
Ejemplo n.º 17
0
 public IEnumerable <BlogDTO> GetAllBlogs()
 {
     return(BlogMapper.Map(_unitOfWork.BlogRepository.Get()));
 }
        public void TestInitializer()
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.DataSource = @".\SQLEXPRESS";
            builder.IntegratedSecurity = true;
            builder.InitialCatalog = "DBBlogs";
            SqlConnection connection1 = new SqlConnection(builder.ConnectionString);
            connection1.Open();
            _currentTransaction1 = connection1.BeginTransaction();
            SqlConnection connection2 = new SqlConnection(builder.ConnectionString);
            connection2.Open();
            _currentTransaction2 = connection2.BeginTransaction();

            MetaDataStore metaDataStore = new MetaDataStore();
            metaDataStore.BuildTableInfoFor<Blog>();

            var identityMap1 = new IdentityMap();
            var identityMap2 = new IdentityMap();

            _blogMapper1 = new BlogMapper(connection1,
                _currentTransaction1,
                metaDataStore,
                new EntityHydrater(metaDataStore, identityMap1),
                identityMap1);
            _blogMapper2 = new BlogMapper(connection2,
                _currentTransaction2,
                metaDataStore,
                new EntityHydrater(metaDataStore, identityMap2),
                identityMap2);
        }