public static MemoryRepository<Post> GetPostsRepository()
        {
            var postsRepository = new MemoryRepository<Post>();
            var users = GetUsersRepository();
            var threads = GetThreadsRepository();

            for (int i = 0; i < 20; i++)
            {
                var thread = threads.GetById(i % 5);
                var user = users.GetById("id" + i % 10);
                var post = new Post
                {
                    Content = "content" + i,
                    Id = i,
                    PostDate = new DateTime(2015, 11, i + 1),
                    ThreadId = thread.Id,
                    Thread = thread,
                    UserId = user.Id,
                    User = user
                };

                postsRepository.Add(post);
            }

            return postsRepository;
        }
        public static IPostsService GetPostsService()
        {
            var post = new Post
            {
                Content = "test post",
                Id = 1,
                PostDate = new DateTime(2015, 11, 1),
                ThreadId = 1,
                UserId = "id1",
                User = new User { Nickname = "testuser" },
                Thread = new Thread { Title = "testTitle" },
                Comments = new List<Comment>(),
                Votes = new List<Vote>()
            };
            var posts = new List<Post>()
            {
                post
            };

            var postsService = new Mock<IPostsService>();
            postsService.Setup(p => p.Add(It.IsAny<string>(), It.Is<int>(t => t > 100), It.IsAny<string>())).Throws<ArgumentException>();
            postsService.Setup(p => p.Add(It.IsAny<string>(), It.Is<int>(t => t < 100), It.IsAny<string>())).Returns(1);
            postsService.Setup(p => p.GetById(It.IsAny<int>())).Returns(post);
            postsService.Setup(p => p.GetByThread(It.IsAny<int>())).Returns(posts.AsQueryable());
            postsService.Setup(p => p.GetByUser(It.Is<string>(s => s != "not exist"))).Returns(posts.AsQueryable());
            postsService.Setup(p => p.GetByUser(It.Is<string>(s => s == "not exist"))).Throws<ArgumentException>();
            postsService.Setup(p => p.Update(It.IsAny<int>(), It.IsAny<string>())).Verifiable();

            return postsService.Object;
        }
예제 #3
0
        public ActionResult Ask(AskInputModel input)
        {
            if (ModelState.IsValid)
            {
                var userId = this.User.Identity.GetUserId();

                var posts = new Post
                {
                    Title = input.Title,
                    Content = sanitizer.Sanitize(input.Content),
                    AuthorId = userId,
                    // TODO: Tags
                };
                this.posts.Add(posts);
                this.posts.SaveChanges();
                return this.RedirectToAction("Display", new {id = posts.Id, url = "new" });
            }
            return this.View(input);
        }
        public int Add(string content, int threadId, string username)
        {
            var thread = this.threadsRepository.GetById(threadId);
            if (thread == null)
            {
                throw new ArgumentException("Thread not found");
            }

            var user = this.FindUser(username);
            var newPost = new Post
            {
                Content = content,
                PostDate = DateTime.Now,
                UserId = user.Id,
                ThreadId = threadId
            };

            this.postsRepository.Add(newPost);
            this.postsRepository.SaveChanges();

            var threadCreator = thread.User;
            if (user.Id != threadCreator.Id)
            {
                var threadUserNotification = new Notification
                {
                    Message = user.Nickname + NotificationMessage,
                    DateCreated = newPost.PostDate
                };

                threadCreator.Notifications.Add(threadUserNotification);
                this.usersRepository.SaveChanges();

                // Implement notifications functionality or message queues
                Client client = new Client(MessageQueueProjectId, MessageQueueToken);
                Queue queue = client.Queue(
                    MessageQueueName);
                queue.Push(user.Nickname + NotificationMessage);
            }

            return newPost.Id;
        }