Esempio n. 1
0
 public void Update(PostModel model)
 {
     using (var dbContext = dbContextFactory.CreateDbContext())
     {
         var postMapper = new PostMapper();
         var entity     = postMapper.ModelToEntity(model);
         dbContext.Posts.Update(entity);
         dbContext.SaveChanges();
     }
 }
Esempio n. 2
0
 public PostModel Create(PostModel model)
 {
     using (var dbContext = dbContextFactory.CreateDbContext())
     {
         var postMapper = new PostMapper();
         var entity     = postMapper.ModelToEntity(model);
         dbContext.Posts.Add(entity);
         dbContext.SaveChanges();
         return(postMapper.EntityToModel(entity));
     }
 }
Esempio n. 3
0
        public void PostEntityModelTest()
        {
            var postMapper = new PostMapper();
            var userMapper = new UserMapper();

            Assert.Null(postMapper.EntityToModel(null));
            Assert.Null(postMapper.ModelToEntity(null));

            var UserEntityObject = new UserEntity()
            {
                Id       = 4,
                Name     = "Anton",
                Password = "******",
                Email    = "*****@*****.**",
            };

            var TeamEntityObject = new TeamEntity()
            {
                Id      = 2,
                Leader  = UserEntityObject.Id,
                Members = new Collection <TeamUserEntity>(),
                Name    = "Team1"
            };

            var PostEntityObject = new PostEntity()
            {
                Id         = 4,
                Author     = UserEntityObject.Id,
                AuthorName = UserEntityObject.Name,
                Date       = new DateTime(2019, 4, 1),
                Team       = TeamEntityObject.Id,
                Text       = "Toto je testovaci prispevok",
                Title      = "Titulok"
            };

            var CommentEntity1 = new CommentEntity()
            {
                Author     = UserEntityObject.Id,
                AuthorName = UserEntityObject.Name,
                Date       = new DateTime(2019, 1, 4),
                Id         = 1,
                Text       = "Testovaci koment"
            };
            var CommentEntity2 = new CommentEntity()
            {
                Author     = UserEntityObject.Id,
                AuthorName = UserEntityObject.Name,
                Date       = new DateTime(2019, 1, 4),
                Id         = 1,
                Text       = "Testovaci koment cislo 2"
            };



            var UserModelObject = new UserDetailModel()
            {
                Id       = 4,
                Name     = "Anton",
                Password = "******",
                Email    = "*****@*****.**",
            };

            var TeamModelObject = new TeamDetailModel()
            {
                Id      = 2,
                Leader  = UserModelObject.Id,
                Members = new Collection <UserListModel>(),
                Name    = "Team1"
            };

            var PostModelObject = new PostModel()
            {
                Id         = 4,
                Author     = UserModelObject.Id,
                AuthorName = UserModelObject.Name,
                Comments   = new Collection <CommentModel>(),
                Date       = new DateTime(2019, 4, 1),
                Team       = TeamModelObject.Id,
                Text       = "Toto je testovaci prispevok",
                Title      = "Titulok"
            };

            Assert.Equal(PostModelObject, postMapper.EntityToModel(PostEntityObject));

            PostEntityObject.Comments = new Collection <CommentEntity>();

            var Comment1 = new CommentModel()
            {
                Author     = UserModelObject.Id,
                AuthorName = UserModelObject.Name,
                Date       = new DateTime(2019, 1, 4),
                Id         = 1,
                Text       = "Testovaci koment"
            };
            var Comment2 = new CommentModel()
            {
                Author     = UserModelObject.Id,
                AuthorName = UserModelObject.Name,
                Date       = new DateTime(2019, 1, 4),
                Id         = 1,
                Text       = "Testovaci koment cislo 2"
            };

            PostModelObject.Comments = null;

            Assert.Equal(PostEntityObject, postMapper.ModelToEntity(PostModelObject));

            PostModelObject.Comments = new Collection <CommentModel>();

            TeamEntityObject.Members.Add(new TeamUserEntity()
            {
                UserId = UserEntityObject.Id, UserName = UserEntityObject.Name
            });
            PostEntityObject.Comments.Add(CommentEntity1);
            PostEntityObject.Comments.Add(CommentEntity2);
            TeamModelObject.Members.Add(userMapper.DetailModelToListModel(UserModelObject));
            PostModelObject.Comments.Add(Comment1);
            PostModelObject.Comments.Add(Comment2);

            Assert.Equal(PostModelObject, postMapper.EntityToModel(PostEntityObject));
            Assert.Equal(PostEntityObject, postMapper.ModelToEntity(PostModelObject));
        }