Esempio n. 1
0
        public async Task <List <PostDto> > GetByRubricAsync(string rubric)
        {
            var posts = PostConverter.Convert(
                await _context.Posts.ToListAsync());

            foreach (var p in posts)
            {
                var tags     = new List <TagsDto>();
                var postTags = _context.PostTags.
                               Where(t => t.PostId == p.Id).ToList();
                foreach (var t in postTags)
                {
                    tags.Add(TagConverter.Convert(await _context.Tags.
                                                  FirstOrDefaultAsync(u => u.Id == t.TagId)));
                }
                p.Tags = tags;

                var collaborators     = new List <CollaboratorDto>();
                var postCollaborators = _context.PostCollaborators.
                                        Where(c => c.PostId == p.Id).ToList();
                foreach (var c in postCollaborators)
                {
                    collaborators.Add(CollaboratorConverter.Convert(await _context.Collaborators.
                                                                    FirstOrDefaultAsync(u => u.Id == c.CollaboratorId)));
                }
                p.Collaborators = collaborators;
            }

            posts = posts.Where(p => p.Rubric == rubric).ToList();

            return(posts);
        }
Esempio n. 2
0
        public async Task <PostDto> GetByIdAsync(Guid id)
        {
            var post = PostConverter.Convert(
                await _context.Posts.FindAsync(id));

            var tags     = new List <TagsDto>();
            var postTags = _context.PostTags.
                           Where(t => t.PostId == post.Id).ToList();

            foreach (var t in postTags)
            {
                tags.Add(TagConverter.Convert(await _context.Tags.
                                              FirstOrDefaultAsync(u => u.Id == t.TagId)));
            }
            post.Tags = tags;

            var collaborators     = new List <CollaboratorDto>();
            var postCollaborators = _context.PostCollaborators.
                                    Where(c => c.PostId == post.Id).ToList();

            foreach (var c in postCollaborators)
            {
                collaborators.Add(CollaboratorConverter.Convert(_context.Collaborators.
                                                                FirstOrDefault(u => u.Id == c.CollaboratorId)));
            }
            post.Collaborators = collaborators;

            return(post);
        }
        public async Task <CollaboratorDto> CreateAsync(CollaboratorDto item)
        {
            var collaborator = _context.Collaborators.Add(
                CollaboratorConverter.Convert(item));
            await _context.SaveChangesAsync();

            return(CollaboratorConverter.Convert(collaborator.Entity));
        }
Esempio n. 4
0
 public async Task <CollaboratorDto> GetByIdAsync(int id)
 {
     return(CollaboratorConverter.Convert(
                await _context.Collaborators
                // .Include(c => c.PostCollaborator)
                // .ThenInclude(pc => pc.Post)
                .FirstOrDefaultAsync(c => c.Id == id)));
 }
        public async Task <bool> UpdateAsync(CollaboratorDto item)
        {
            if (item == null)
            {
                return(false);
            }
            _context.Collaborators.Update(CollaboratorConverter.Convert(item));
            await _context.SaveChangesAsync();

            return(true);
        }
        public async Task <CollaboratorDto> GetByIdAsync(Guid id)
        {
            var collaborator = CollaboratorConverter.Convert(
                await _context.Collaborators.FindAsync(id));

            var posts             = new List <PostDto>();
            var postCollaborators = _context.PostCollaborators.
                                    Where(p => p.CollaboratorId == collaborator.Id).ToList();

            foreach (var p in postCollaborators)
            {
                posts.Add(PostConverter.Convert(await _context.Posts.
                                                FirstOrDefaultAsync(u => u.Id == p.PostId)));
            }
            collaborator.Posts = posts;

            return(collaborator);
        }
Esempio n. 7
0
        public async Task <List <PostDto> > GetByTagAsync(string tagName)
        {
            var posts = PostConverter.Convert(
                await _context.Posts.ToListAsync());

            foreach (var p in posts)
            {
                var tags     = new List <TagsDto>();
                var postTags = _context.PostTags.
                               Where(t => t.PostId == p.Id).ToList();
                foreach (var t in postTags)
                {
                    tags.Add(TagConverter.Convert(await _context.Tags.
                                                  FirstOrDefaultAsync(u => u.Id == t.TagId)));
                }
                p.Tags = tags;

                var collaborators     = new List <CollaboratorDto>();
                var postCollaborators = _context.PostCollaborators.
                                        Where(c => c.PostId == p.Id).ToList();
                foreach (var c in postCollaborators)
                {
                    collaborators.Add(CollaboratorConverter.Convert(await _context.Collaborators.
                                                                    FirstOrDefaultAsync(u => u.Id == c.CollaboratorId)));
                }
                p.Collaborators = collaborators;
            }

            var tagPosts = new List <PostDto>();

            foreach (var post in posts)
            {
                foreach (var collaborator in post.Tags)
                {
                    if (collaborator.Name == tagName)
                    {
                        tagPosts.Add(post);
                    }
                }
            }

            return(tagPosts);
        }
        public async Task <List <CollaboratorDto> > GetAllAsync()
        {
            var collaborators = CollaboratorConverter.Convert(
                await _context.Collaborators.ToListAsync());

            foreach (var c in collaborators)
            {
                var posts             = new List <PostDto>();
                var postCollaborators = _context.PostCollaborators.
                                        Where(p => p.CollaboratorId == c.Id).ToList();
                foreach (var p in postCollaborators)
                {
                    posts.Add(PostConverter.Convert(await _context.Posts.
                                                    FirstOrDefaultAsync(u => u.Id == p.PostId)));
                }
                c.Posts = posts;
            }

            return(collaborators);
        }
Esempio n. 9
0
        public async Task <CollaboratorList> GetAllAsync(int page, int count)
        {
            var collabCount = _context.Collaborators.Count();
            var take        = 0;
            var skip        = 0;

            if (count * page < collabCount)
            {
                skip = collabCount - count * page;
                take = count;
            }
            else if (count * page > collabCount && count * (page - 1) < collabCount)
            {
                skip = 0;
                take = collabCount - count * (page - 1);
            }
            else
            {
                return new CollaboratorList
                       {
                           Collaborators = new List <CollaboratorDto>(),
                           Count         = 0
                       }
            };
            var collaborators = CollaboratorConverter.Convert(
                await _context.Collaborators
                .Skip(skip)
                .Take(take)
                // .Include(c => c.PostCollaborator)
                // .ThenInclude(pc => pc.Post)
                .OrderByDescending(p => p.Id)
                .ToListAsync());


            return(new CollaboratorList
            {
                Collaborators = collaborators,
                Count = collabCount
            });
        }