Beispiel #1
0
        public static ThreadModel CreateFromThreadEntity(Thread threadEntity)
        {
            var postModels   = new List <PostModel>();
            var postEntities = threadEntity.Posts;

            foreach (var postEntity in postEntities)
            {
                postModels.Add(PostModel.CreateFromPostEnitity(postEntity));
            }

            ThreadModel threadModel = new ThreadModel()
            {
                Id          = threadEntity.Id,
                Title       = threadEntity.Title,
                DateCreated = threadEntity.DateCreated,
                Content     = threadEntity.Content,
                CreatedBy   = threadEntity.Creator.Nickname,
                Categories  =
                    from cat in threadEntity.Categories
                    select cat.Name,
                Posts = postModels
            };

            return(threadModel);
        }
        public HttpResponseMessage CreateThread(ThreadModel threadModel, string sessionKey)
        {
            var response = this.PerformOperationAndHandleExceptions(() =>
            {
                var user = this.userRepository.GetAll()
                    .Where(usr => usr.SessionKey == sessionKey).FirstOrDefault();

                if (user == null)
                {
                    throw new InvalidOperationException("User is not logged in or invalid session key.");
                }

                if (threadModel.Title == null)
                {
                    throw new ArgumentException("The title can't be null.");
                }

                if (threadModel.Content == null)
                {
                    throw new ArgumentException("The content can't be null.");
                }

                var newThread = new Thread()
                {
                    Title = threadModel.Title,
                    Content = threadModel.Content,
                    Creator = user,
                    DateCreated = threadModel.DateCreated,
                };

                foreach (var category in threadModel.Categories)
                {
                    var categoryEntity = this.categoryRepository.GetAll()
                        .Where(cat => cat.Name.ToLower() == category.ToLower()).FirstOrDefault();

                    if (categoryEntity == null)
                    {
                        categoryEntity = new Category();
                        categoryEntity.Name = category;
                        this.categoryRepository.Add(categoryEntity);
                    }

                    categoryEntity.Threads.Add(newThread);
                    newThread.Categories.Add(categoryEntity);
                }

                this.threadRepository.Add(newThread);

                var responseMessage = this.Request.CreateResponse(HttpStatusCode.Created,
                    new
                    {
                        id = newThread.Id,
                        title = newThread.Title
                    });

                return responseMessage;
            });

            return response;
        }
        public static ThreadModel CreateFromThreadEntity(Thread entity)
        {
            var postEntities = entity.Posts.ToList();
            var currentThreadPostModels = new List<PostModel>();

            foreach (var postEntity in postEntities)
            {
                currentThreadPostModels.Add(PostModel.CreateFromPostEntity(postEntity));
            }

            var threadModel = new ThreadModel()
            {
                Id = entity.Id,
                Title = entity.Title,
                DateCreated = entity.DateCreated,
                Content = entity.Content,
                CreatedBy = entity.Creator.Nickname,
                Categories = from cat in entity.Categories
                             select cat.Name,
                Posts = currentThreadPostModels
            };

            return threadModel;
        }
        public void Post_CreateThreadTest_ValidCreation()
        {
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Category> categoryFakeRepository = new FakeRepository<Category>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            userFakeRepository.entities.Add(user);

            Category category1 = new Category()
            {
                Name = "Test category 1"
            };

            categoryFakeRepository.entities.Add(category1);

            ThreadsController threadsController = new ThreadsController(
                categoryFakeRepository, threadFakeRepository, userFakeRepository);

            SetupController(threadsController);

            ThreadModel threadModel = new ThreadModel()
            {
                Title = "Test title",
                Content = "Test content",
                CreatedBy = "Test User",
                DateCreated = DateTime.Now,
                Categories = new List<string>()
                {
                    "Test category 1",
                    "Test category 2"
                }
            };

            threadsController.CreateThread(threadModel, user.SessionKey);

            int expectedThreadCount = 1;
            Thread addedThread = threadFakeRepository.entities.FirstOrDefault();
            List<Category> addedThreadCategories = addedThread.Categories.ToList<Category>();

            Assert.AreEqual(expectedThreadCount, threadFakeRepository.entities.Count);
            Assert.AreEqual("Test category 1", addedThreadCategories[0].Name);
            Assert.AreEqual("Test category 2", addedThreadCategories[1].Name);
        }
        public void Post_CreateThreadTest_InvalidCreation_ThreadTitleIsNull()
        {
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Category> categoryFakeRepository = new FakeRepository<Category>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();

            ThreadsController threadsController = new ThreadsController(
                categoryFakeRepository, threadFakeRepository, userFakeRepository);

            SetupController(threadsController);

            User user = new User()
            {
                AuthCode = "0123456789012345678901234567890123456789",
                Username = "******",
                Nickname = "TestNickname",
                SessionKey = "0SuGqVGqRwitYtijDvHlVfHGotklitbwHdYFkgwIRcIQjRASPQ"
            };

            userFakeRepository.entities.Add(user);

            ThreadModel threadModel = new ThreadModel()
            {
                Title = null,
                Content = "Test content",
                CreatedBy = "Test User",
                DateCreated = DateTime.Now
            };

            threadsController.CreateThread(threadModel, user.SessionKey);
        }
        public void Post_CreateThreadTest_InvalidCreation_SessionKeyIsNull()
        {
            FakeRepository<Thread> threadFakeRepository = new FakeRepository<Thread>();
            FakeRepository<Category> categoryFakeRepository = new FakeRepository<Category>();
            FakeRepository<User> userFakeRepository = new FakeRepository<User>();

            ThreadsController threadsController = new ThreadsController(
                categoryFakeRepository, threadFakeRepository, userFakeRepository);

            SetupController(threadsController);

            ThreadModel threadModel = new ThreadModel()
            {
                Title = "Test title",
                Content = "Test content",
                CreatedBy = "Test User",
                DateCreated = DateTime.Now
            };

            string userSessionKey = null;
            threadsController.CreateThread(threadModel, userSessionKey);
        }