Пример #1
0
 public NewPostViewModel(Post post)
 {
     DatePosted = post.EntryAddedDate;
     Title = post.Title;
     Post = post.Body;
     Id = post.Id;
 }
Пример #2
0
 public void GivenABlogEntry_WhenThenFileIsCreated_AndTheFileIsNotFound_ThenTheCorrectExceptionIsThrown()
 {
     _fileInfo.Setup(f => f.Create()).Throws(new IOException());
     var entry = new Post { Title = "title", EntryAddedDate = new DateTime(1990, 1, 1) };
     var repository = new PostRepository(Path, _fileInfoFactory.Object, null);
     Assert.Throws<RepositoryException>(() => repository.Create(entry));
 }
Пример #3
0
        public void WhenAnEntityIsCreated_AndAnIoExceptionIsThrown_ThenAnItsaExceptionIsRethrown()
        {
            _fileInfoFactory.Setup(f => f.CreateFileInfo(It.IsAny<string>())).Throws<IOException>();
            Post post = new Post { Id = Guid.NewGuid() };
            Action act = () => _repository.Create(post);

            act.ShouldThrow<RepositoryException>();
        }
Пример #4
0
 public void GivenABlogEntry_WhenTheEntryIsDeleted_ThenTheEntryIsRemovedFromTheRepository()
 {
     var id = Guid.NewGuid();
     var post = new Post{Id = id};
     _blogRepository.Setup(b => b.Entities).Returns(new List<Post> { post }.AsQueryable());
     _adminService.DeleteBlogPost(id);
     _blogRepository.Verify(s => s.Delete(It.Is<Post>(p => p.Id == id)), Times.Once());
 }
Пример #5
0
        public void GivenAnExistingFile_WhenAFileWithTheSameNameIsCreated_ThenAnExceptionIsThrown()
        {
            var entry = new Post { Id = new Guid(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) };

            _file = new FileInfo(GenerateFileName(entry));
            using (_file.Create()) ;
            Action act = () => _postRepository.Create(entry);
            act.ShouldThrow<Exception>();
        }
Пример #6
0
        public void WhenAnEntityIsCreated_ThenANewEntityIsWritenToFile()
        {
            var stream = new Mock<Stream>();
            _fileInfo.Setup(f => f.Create()).Returns(stream.Object);
            _fileInfoFactory.Setup(f => f.CreateFileInfo(It.IsAny<string>())).Returns(_fileInfo.Object);
            var post = new Post { Id = Guid.NewGuid() };
            _repository.Create(post);

            stream.Verify(s => s.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()), Times.AtLeastOnce());
        }
Пример #7
0
 public void WhenAPostIsSentToTheHub_ThenTheCorrectDetailsAreSaved()
 {
     var service = new Mock<IAdminService>();
     var hub = new TestableAdminHub(service.Object, "Kevin");
     var post = new Post { Title = "title", Body = "body" };
     service.Setup(s => s.AddBlogPost(It.IsAny<Post>())).Returns(post);
     hub.AddBlogPost(new NewPostViewModel { Title = "title", Post = "body" });
     service.Verify(s => s.AddBlogPost(It.Is<Post>(p => p.Title == post.Title && p.Body == post.Body)),
                    Times.Once());
 }
Пример #8
0
        public void GivenTheNameOfAFileThatDoesNotExist_WhenThatFileIsCreated_ThenTheFileDataIsCorrect()
        {
            var entry = new Post { Title = "title", Id = new Guid(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) };

            _postRepository.Create(entry);
            _file = new FileInfo(GenerateFileName(entry));
            using (var stream = new StreamReader(_file.Open(FileMode.Open)))
            {
                var json = stream.ReadToEnd();
                entry = JsonSerializer.Deserialize<Post>(json);
                entry.Title.Should().Be("title");
            }
        }
Пример #9
0
 public async Task<IActionResult> Create(Post post)
 {
     if (ModelState.IsValid)
     {
         _context.Posts.Add(post);
         await _context.SaveChangesAsync();
         return RedirectToAction("Index");
     }
     ViewData["ForumId"] = new SelectList(_context.Forums, "Id", "Forum", post.ForumId);
     ViewData["ReplyToPostId"] = new SelectList(_context.Posts, "Id", "ReplyToPost", post.ReplyToPostId);
     ViewData["UserId"] = new SelectList(_context.Users, "Id", "User", post.UserId);
     return View(post);
 }
Пример #10
0
        public void GivenAnExistingFile_WhenTheFileIsDeleted_ThenTheFileIsRemovedFromDisk()
        {
            var entry = new Post { Title = "title", Id = new Guid(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) };

            _file = new FileInfo(GenerateFileName(entry));
            using (_file.Create()){};

            _postRepository.Delete(entry);

            _file = new FileInfo(GenerateFileName(entry));

            _file.Exists.Should().BeFalse();
        }
Пример #11
0
 public void GivenABlogEntry_WhenTheEntryIsWrittenToAFile_ThenTheCorrectJsonIsWritten()
 {
     var stream = new MemoryStream();
     _fileInfo.Setup(f => f.Create()).Returns(stream);
     var entry = new Post { Title = "title", EntryAddedDate = new DateTime(1990, 1, 1), EntryUpdateDate = new DateTime(1991, 2, 2), Body = "post" };
     var repository = new PostRepository(Path, _fileInfoFactory.Object, null);
     repository.Create(entry);
     var data = stream.ToArray();
     var json = Encoding.UTF8.GetString(data);
     var savedentry = JsonSerializer.Deserialize<Post>(json);
     savedentry.Title.Should().Be("title");
     savedentry.Body.Should().Be("post");
     savedentry.EntryAddedDate.Should().Be(new DateTime(1990, 1, 1));
     savedentry.EntryUpdateDate.Should().Be(new DateTime(1991, 2, 2));
 }
Пример #12
0
        public bool CreatePost(Post post)
        {
            post.Rating = 0;
            post.PostId = Guid.NewGuid();
            var atrAdd = "";
            post.CreatedTime = DateTime.Now;
            var tagsLength = post.Tags.Length;

            //Конвертация меточек в строку для бд
            for (int i = 0; i < tagsLength; i++)
            {
                if (i < tagsLength - 1)
                    atrAdd += post.Tags[i] + ",";
                else
                    atrAdd += post.Tags[i];
            }
            post.tmp = atrAdd;
            return _data.CreatePost(post);
        }
Пример #13
0
 public virtual ActionResult Create()
 {
     var atomXMl = XDocument.Load(new StreamReader(Request.InputStream));
     XNamespace ns = "http://www.w3.org/2005/Atom";
     var title = (from node in atomXMl.Descendants(ns + "title")
                  select node.Value).FirstOrDefault();
     var content = (from node in atomXMl.Descendants(ns + "content")
                    select node.Value).FirstOrDefault();
     Post post = new Post
     {
         Id = Guid.NewGuid(),
         Body = content,
         Title = title,
         EntryUpdateDate = DateTime.UtcNow,
         EntryAddedDate = DateTime.UtcNow,
         CommentsEnabled = true,
     };
     _postService.CreatePost(post);
     return View(new NewPostViewModel(post));
 }
Пример #14
0
 public static bool CreatePost(Post post)
     => _logic.CreatePost(post);
Пример #15
0
 public void GivenACollectionOfSerializedBlogEntries_WhenTheCollectionIsRetrieved_ThenAllTheEntriesAreRetrieved()
 {
     MemoryStream stream1 = new MemoryStream();
     MemoryStream stream2 = new MemoryStream();
     var entry = new Post();
     var json = entry.SerializeToString();
     var data = Encoding.UTF8.GetBytes(json);
     stream1.Write(data, 0, data.Length);
     stream2.Write(data, 0, data.Length);
     var fileInfo1 = new Mock<IFileInfo>();
     fileInfo1.Setup(f => f.Open(FileMode.Open)).Returns(stream1);
     var fileInfo2 = new Mock<IFileInfo>();
     fileInfo2.Setup(f => f.Open(FileMode.Open)).Returns(stream2);
     var fileInfos = new List<IFileInfo> { fileInfo1.Object, fileInfo2.Object, };
     _directoryInfo.Setup(d => d.EnumerateFiles(It.IsAny<string>(), It.IsAny<string>())).Returns(fileInfos);
     var repository = new PostRepository(Path, _fileInfoFactory.Object, _directoryInfo.Object);
     var entities = repository.Entities;
     entities.Should().HaveCount(2);
 }
Пример #16
0
        public void GivenABlogEntry_WhenThenFileIsUdated_ThenTheCorrectFileIsFound()
        {
            string fileName = string.Format("{0}/{1}-{2}-{3}-{4}-{5}.json", Path, "title", 1990, 1, 1, new DateTime(1990, 1, 1).Ticks);
            _fileInfo.Setup(f => f.Open(FileMode.Truncate)).Returns(new MemoryStream());

            var entry = new Post { Title = "title", EntryAddedDate = new DateTime(1990, 1, 1) };
            var repository = new PostRepository(Path, _fileInfoFactory.Object, null);
            repository.Update(entry);
            _fileInfo.Verify(f => f.Open(FileMode.Truncate), Times.Once());
        }
Пример #17
0
        public void GivenABlogEntry_WhenThenFileIsDeleted_ThenTheCorrectFileIsFound()
        {
            string fileName = string.Format("{0}/{1}-{2}-{3}-{4}-{5}.json", Path, "title", 1990, 1, 1, new DateTime(1990, 1, 1).Ticks);

            var entry = new Post { Title = "title", EntryAddedDate = new DateTime(1990, 1, 1) };
            var repository = new PostRepository(Path, _fileInfoFactory.Object, null);
            repository.Delete(entry);
            _fileInfo.Verify(f => f.Delete(), Times.Once());
        }
Пример #18
0
 public Post AddBlogPost(Post entry)
 {
     return _repository.Create(entry);
 }
Пример #19
0
 public void CreatePost(Post post)
 {
     _postRepository.Create(post);
 }
Пример #20
0
        public bool CreatePost(Post post)
        {
            string queryString =
                "INSERT INTO [dbo].posts ([dbo].posts.postid, postname, source, createdtime, mimetype, accountid, text, rating) " +
                "VALUES (@postid, @postname, @source, @createdtime, @mimetype, @accountid, @text, @rating); " +
                "INSERT INTO tags ([dbo].tags.postid, tag) " +
                "VALUES(@postid, @tag)";

            using (SqlConnection connection = new SqlConnection(_connectionString))
            {
                var command = new SqlCommand(queryString, connection);
                command.Parameters.AddWithValue("postid", post.PostId);
                command.Parameters.AddWithValue("postname", post.NamePost);
                command.Parameters.AddWithValue("source", post.Image);
                command.Parameters.AddWithValue("createdtime", post.CreatedTime);
                command.Parameters.AddWithValue("mimetype", post.MimeType);
                command.Parameters.AddWithValue("accountid", post.AccountId);
                command.Parameters.AddWithValue("text", post.Text);
                command.Parameters.AddWithValue("rating", post.Rating);
                command.Parameters.AddWithValue("tag", post.tmp);

                try
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    var stack = e.StackTrace;
                    throw new Exception();
                }
            }

            return true;
        }
Пример #21
0
        public void WhenEntitiesAreRetrievedFromTheRepository_ThenAllEntitiesAreReturned()
        {
            var streams = new MemoryStream[] { new MemoryStream(), new MemoryStream(), new MemoryStream()};
            foreach (var stream in streams)
            {
                var p = new Post();
                var json = p.SerializeToString();
                var bytes = Encoding.UTF8.GetBytes(json);
                stream.Write(bytes, 0, bytes.Length);
            }
            _fileInfo.Setup(f => f.Open(FileMode.Open)).Returns(streams[0]);
            var fileInfo1 = new Mock<IFileInfo>();
            fileInfo1.Setup(f => f.Open(FileMode.Open)).Returns(streams[1]);
            var fileInfo2 = new Mock<IFileInfo>();
            fileInfo2.Setup(f => f.Open(FileMode.Open)).Returns(streams[2]);

            _directoryInfo.Setup(d => d.EnumerateFiles(It.IsAny<string>(), It.IsAny<string>()))
                          .Returns(new List<IFileInfo> { _fileInfo.Object, fileInfo1.Object, fileInfo2.Object });
            _fileInfoFactory.Setup(f => f.CreateFileInfo(It.IsAny<string>())).Returns(_fileInfo.Object);
            var posts = _repository.Entities;

            posts.Count().Should().Be(3);
            _repository.Dispose();
        }
Пример #22
0
        public void GivenABlogEntry_WhenThenFileIsCreated_ThenTheCorrectFileNameIsUsed()
        {
            _fileInfo.Setup(f => f.Create()).Returns(new MemoryStream());

            var entry = new Post { Title = "title", EntryAddedDate = new DateTime(1990, 1, 1) };
            var repository = new PostRepository(Path, _fileInfoFactory.Object, null);
            repository.Create(entry);
            _fileInfo.Verify(f => f.Create(), Times.Once());
        }
Пример #23
0
        private void InitPosts()
        {
            var firstUser = _context.Users.First();
            var secondUser = _context.Users.Skip(1).First();
            var var = "סקופים";
            var scoops = _context.Forums.Single(x => x.Name == var);

            for (var i = 1; i <= 30; i++)
            {
                var firstPost = new Post
                                    {
                                        Forum = scoops,
                                        Title = "Root level " + i,
                                        User = firstUser,
                                        Body = "Bla bla bla",
                                        PostType = PostType.News | PostType.Foreign
                                    };


                _context.AddRange(Enumerable.Range(1, 100).Select(x =>
                    new Post
                        {
                            Forum = scoops,
                            Title = "LLLLLLLLLOOOOOOOOOOOOOOOOOPPPPPPPPPPP! " + x,
                            Body =
                                "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO NNNNNNNNNNNNNNNNNNNNNNN GGGGGGGGGGGGGGGGGGGGGG TEXT LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO NNNNNNNNNNNNNNNNNNNNNNN GGGGGGGGGGGGGGGGGGGGGG TEXT LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO NNNNNNNNNNNNNNNNNNNNNNN GGGGGGGGGGGGGGGGGGGGGG TEXT LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO NNNNNNNNNNNNNNNNNNNNNNN GGGGGGGGGGGGGGGGGGGGGG TEXT LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO NNNNNNNNNNNNNNNNNNNNNNN GGGGGGGGGGGGGGGGGGGGGG TEXT LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO NNNNNNNNNNNNNNNNNNNNNNN GGGGGGGGGGGGGGGGGGGGGG TEXT LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO NNNNNNNNNNNNNNNNNNNNNNN GGGGGGGGGGGGGGGGGGGGGG TEXT LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO NNNNNNNNNNNNNNNNNNNNNNN GGGGGGGGGGGGGGGGGGGGGG TEXT ",
                            ReplyToPost = firstPost,
                            User = secondUser
                        }).ToList());

                var reply1 = new Post
                                 {
                                     Forum = scoops,
                                     Title = "FirstReply " + i,
                                     ReplyToPost = firstPost,
                                     User = secondUser
                                 };

                var reply2 = new Post
                                 {
                                     Forum = scoops,
                                     Title = "Second Reply " + i,
                                     ReplyToPost = firstPost,
                                     User = firstUser
                                 };

                var replytoReply2 = new Post
                                        {
                                            Forum = scoops,
                                            Title = "Reply to second Reply " + i,
                                            ReplyToPost = firstPost,
                                            User = firstUser
                                        };

                var reply3 = new Post
                                 {
                                     Forum = scoops,
                                     Title = "Third reply " + i,
                                     ReplyToPost = firstPost,
                                     User = secondUser,
                                     IsImportantReply = true
                                 };


                var reply4 = new Post
                                 {
                                     Forum = scoops,
                                     Title = "fourth reply " + i,
                                     User = secondUser,
                                     ReplyToPost = reply2
                                 };

                var closedPost = new Post
                                     {
                                         Forum = scoops,
                                         Title = "Closed post " + i,
                                         User = secondUser,
                                         IsLocked = true,
                                         LockingUser = firstUser,
                                         LockReason = "Becuase I hate you, go kill yourself",
                                         Score = 4,
                                         Votes = new List<Vote>()
                                     };

                for (var j = 0; j < 5; j++)
                {
                    closedPost.Votes.Add(new Vote
                                             {
                                                 Post = closedPost,
                                                 User = firstUser,
                                                 VoteType = VoteType.Up
                                             });
                }

                closedPost.Votes.Add(new Vote
                                         {
                                             Post = closedPost,
                                             User = firstUser,
                                             VoteType = VoteType.Down
                                         });

                _context.Posts.AddRange(firstPost, reply1, reply2, reply3, reply4, replytoReply2, closedPost);
            }
        }
Пример #24
0
 private string GenerateFileName(Post entity)
 {
     return string.Format("{0}/{1}.json", PostsDirectory, entity.Id);
 }
Пример #25
0
        public void GivenTheNameOfAFileThatDoesNotExist_WhenThatFileIsCreated_ThenTheFileExistsOnDisk()
        {
            var entry = new Post { Id = new Guid(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) };

            _postRepository.Create(entry);
            _file = new FileInfo(GenerateFileName(entry));
            _file.Exists.Should().BeTrue();
        }
Пример #26
0
        public void WhenAnEntityIsDeleted_ThenTheEntityIsDeletedFromTheFile()
        {
            _fileInfoFactory.Setup(f => f.CreateFileInfo(It.IsAny<string>())).Returns(_fileInfo.Object);
            var post = new Post { Id = Guid.NewGuid() };
            _repository.Delete(post);

            _fileInfo.Verify(s => s.Delete(), Times.AtLeastOnce());
        }