async Task <Option <ThreadOverViewSet> > IThreadService.GetOrderedThreads(string boardKey, Option <string> filter, int pageSize, int pageNumber, CancellationToken cancellationToken) { var board = await this.boardRepository.GetByKey(boardKey, cancellationToken); return(await board.MapToTask(async some => { var threadIds = this.GetOrderedThreads(some.Id, filter); var latestThreads = threadIds.Skip(pageSize * (pageNumber - 1)).Take(pageSize); var threads = await this.threadRepository.GetAll().Where(a => latestThreads.Contains(a.Id)).ToListAsync(cancellationToken); var l = await Task.WhenAll(threads.Select(async thread => { var posts = this.postRepository.GetAll().Where(p => p.ThreadId == thread.Id); var firstPost = await this.GetFirstPostAsync(posts, cancellationToken); var lastPosts = (await Task.WhenAll(posts.Skip(1).OrderByDescending(a => a.Created).Take(5).ToArray().Select(async p => { var file = await this.fileRepository.GetPostFile(p.Id, cancellationToken); return PostMapper.Map(p, file); }))).OrderBy(a => a.Created).ToList(); var shownPosts = lastPosts.Concat(new[] { firstPost }).ToList(); var stats = await this.GetOverviewStats(thread.Id, posts, shownPosts, cancellationToken); return new ThreadOverView(thread.Id, thread.Subject, firstPost, lastPosts, stats); }).ToArray()); var numberOfPages = (threadIds.Count() / pageSize) + 1; return new ThreadOverViewSet(some, l, new PageData(pageNumber, numberOfPages)); })); }
private async Task <PostOverView> GetFirstPostAsync(IQueryable <Domain.Post> posts, CancellationToken cancellationToken) { var firstPost = await posts.OrderBy(a => a.Created).FirstAsync(cancellationToken); var file = await this.fileRepository.GetPostFile(firstPost.Id, cancellationToken); return(PostMapper.Map(firstPost, file)); }
public void Create(PostViewModel entity) { PostMapper _postMapper = new PostMapper(); Post post = _postMapper.Map(entity); _blogpostContext.Post.Add(post); _blogpostContext.SaveChanges(); }
static public TopicDisplayViewModel Map(TopicModel topic) { return(new TopicDisplayViewModel { Name = topic.Name, Posts = topic.Posts.OrderByDescending(p => p.PostedAt).Select(p => PostMapper.Map(p)).ToList(), SubscriptionsCount = topic.Subscriptions.Count }); }
static public UserTopicViewModel Map(TopicModel topic, string userId, ICollection <PostModel> posts) { return(new UserTopicViewModel { IsSubscribed = topic.Subscriptions.Any(s => s.UserId == userId), Name = topic.Name, Posts = posts.Select(p => PostMapper.Map(p)).ToList(), SubscriptionsCount = topic.Subscriptions.Count }); }
public void MapCommonConditionSuccessTest() { var post = new Post(2, 1, "My test post", "My test Body"); var postMapper = new PostMapper(); Entities.Post result = postMapper.Map(post); Assert.AreEqual(2, result.Id); Assert.AreEqual("My test post", result.Title); Assert.AreEqual("My test Body", result.Body); Assert.AreEqual(1, result.UserId); }
public ActionResult Index() { var userId = User.Identity.GetUserId(); var posts = uow.PostRepository .Get( p => p.Topic.Subscriptions.Any(s => s.UserId == userId), includeProperties: "Author", orderBy: p => p.OrderByDescending(post => post.PostedAt) ) .Select(p => PostMapper.Map(p)) .ToList(); return(View(posts)); }
public void MapCommonConditionSuccessTest() { var user = new User(1, "John", "*****@*****.**", "url"); var post = new Post(2, 1, "My test post", "My test Body"); var userMapper = new UserMapper(); var postMapper = new PostMapper(userMapper); Core.Models.Post result = postMapper.Map(post, user); Assert.AreEqual(2, result.Id); Assert.AreEqual("My test post", result.Title); Assert.AreEqual("My test Body", result.Body); Assert.AreNotEqual(null, result.User); Assert.AreEqual(1, result.User.UserId); Assert.AreEqual("John", result.User.Name); Assert.AreEqual("*****@*****.**", result.User.Email); Assert.AreEqual("url", result.User.ImageUrl); }
async Task <Option <ThreadDetailView> > IThreadService.GetThread(Guid threadId, int pageSize, CancellationToken cancellationToken) { var thread = await this.threadRepository.GetById(threadId, cancellationToken); return((await thread.MapToTask(async t => { var posts = this.postRepository.GetAll().Where(a => a.ThreadId == threadId); var b = await this.boardRepository.GetById(t.BoardId, cancellationToken); return await b.MapToTask(async some => { var postsMapped = await Task.WhenAll(posts .OrderBy(a => a.Created).ToList() .Select(async p => PostMapper.Map(p, await this.fileRepository.GetPostFile(p.Id, cancellationToken)))); var stats = await this.GetStats(postsMapped, some.Id, t.Id, pageSize); return new ThreadDetailView(threadId, t.Subject, stats, new BoardOverView(some.Id, some.BoardName, some.BoardKey), postsMapped.ToList()); }); })).Flatten()); }
public void Can_Map_From_Posts_Collection_To_PostDtos_Collection() { //ARRANGE var postsList = new List <Post> { new Post { Id = 1, Op = new User { Username = "******" }, CreationDate = new DateTime(2015, 1, 1), Image = "image", Rating = 1, Title = "title", DeletionDate = new DateTime(2016, 1, 1) } }; //ACT var sut = new PostMapper(); var result = sut.Map(postsList); //ASSERT var expectedResult = new List <PostDto> { new PostDto { Id = 1, Username = "******", CreationDate = new DateTime(2015, 1, 1), Image = "image", Rating = 1, Title = "title", DeletionDate = new DateTime(2016, 1, 1) } }; result.Should().BeEquivalentTo(expectedResult); }
async Task <Option <PostContextView> > IPostService.GetById(Guid id, CancellationToken cancellationToken) { var post = await this.postRepository.GetById(id, cancellationToken); return((await post.MapToTask(async some => { var t = await this.threadRepository.GetById(some.ThreadId, cancellationToken); return await t.MapToTask(async thread => { var b = await this.boardRepository.GetById(thread.BoardId, cancellationToken); var file = await this.fileRepository.GetPostFile(some.Id, cancellationToken); return b.Map( board => new PostContextView(thread.Id, thread.Subject, new BoardOverView(board.Id, board.BoardName, board.BoardKey), PostMapper.Map(some, file))); }); })).Flatten().Flatten()); }