Ejemplo n.º 1
0
        public async Task <PagedResultDTO <PostDTO> > GetPageAsync(int id, int page, int pageSize, bool justMyPosts, string srchStr = null)
        {
            var pagedResult = await _db.Posts.GetPageAsync(id, page, pageSize, justMyPosts, srchStr);

            pagedResult.Data.ForEach(p =>
                                     p.PostFiles = p.PostFiles.Count > 0 ?
                                                   new List <PostFile>()
            {
                p.PostFiles.First()
            }
                : new List <PostFile>()
                                     );

            Func <Post, PostDTO> func = post =>
            {
                var postDTO = PostMapper.ConvertToPostDTO(post);
                postDTO.Files       = FileMapper.ConvertToFileInfoDTOCollection(post.PostFiles);
                postDTO.UserDTO     = UserMapper.ConvertToUserDTO(post.User);
                postDTO.CommentsDTO = CommentMapper.ConvertToCommentDTOCollection(post.Comments);
                var usersDTOLikes = new List <UserDTO>();
                post.PostLikes.ToList().ForEach(pl =>
                {
                    var userDTO     = UserMapper.ConvertToUserDTO(pl.User);
                    userDTO.RoleDTO = RoleMapper.ConvertToRoleDTO(pl.User.Role);
                    usersDTOLikes.Add(userDTO);
                });
                postDTO.UsersLikes = usersDTOLikes;
                return(postDTO);
            };

            var pageResultDTO = PagedResultMapper.MapToDTO(pagedResult, func);

            return(pageResultDTO);
        }
Ejemplo n.º 2
0
 public async Task <IEnumerable <CommentDTO> > FindValidAsync(Expression <Func <Comment, bool> > predicate)
 {
     return((await _db.Comments.FindValidAsync(predicate)).Select(c =>
     {
         CommentDTO commentDTO = CommentMapper.ConvertToCommentDTO(c);
         commentDTO.UserDTO = UserMapper.ConvertToUserDTO(c.User);
         return commentDTO;
     }));
 }
Ejemplo n.º 3
0
        public async Task <CommentDTO> GetCommentAsync(int id)
        {
            var comment = await _db.Comments.GetAsync(id);

            var commentDTO = CommentMapper.ConvertToCommentDTO(comment);

            commentDTO.UserDTO = UserMapper.ConvertToUserDTO(comment.User);

            return(commentDTO);
        }
Ejemplo n.º 4
0
        public async Task <List <UserDTO> > GetUsersLikes(int postId)
        {
            var users = await db.UsersLikes.GetUsersLikesForPost(postId);

            var usersDTO = users.Select(u =>
            {
                var userDTO     = UserMapper.ConvertToUserDTO(u);
                userDTO.RoleDTO = RoleMapper.ConvertToRoleDTO(u.Role);
                return(userDTO);
            });

            return(usersDTO.ToList());
        }
Ejemplo n.º 5
0
        public async Task <IEnumerable <CommentDTO> > GetCommentsByPostAsync(int postId)
        {
            var comments = await _db.Comments.GetAllByPost(postId);

            var commentsDTO = comments.Select(c =>
            {
                var commentDTO     = CommentMapper.ConvertToCommentDTO(c);
                commentDTO.UserDTO = UserMapper.ConvertToUserDTO(c.User);
                return(commentDTO);
            });

            return(commentsDTO);
        }
Ejemplo n.º 6
0
        public async Task <IEnumerable <CommentDTO> > GetCommentsAsync()
        {
            var comments = await _db.Comments.GetAllValidAsync();

            var commentsDTO = comments.Select(u =>
            {
                var commentDTO     = CommentMapper.ConvertToCommentDTO(u);
                commentDTO.UserDTO = UserMapper.ConvertToUserDTO(u.User);

                return(commentDTO);
            });

            return(commentsDTO);
        }
Ejemplo n.º 7
0
        public async Task <PostDTO> GetAsync(int id)
        {
            var post = await _db.Posts.GetAsync(id);

            if (post == null)
            {
                throw new NotFoundException("Post is not found!");
            }
            var postDTO = PostMapper.ConvertToPostDTO(post);

            postDTO.UserDTO         = UserMapper.ConvertToUserDTO(post.User);
            postDTO.UserDTO.RoleDTO = RoleMapper.ConvertToRoleDTO(post.User.Role);

            return(postDTO);
        }
Ejemplo n.º 8
0
        public async Task <PostDTO> Update(int postId, EditPostDTO editPost)
        {
            if (editPost.EndDate <= editPost.StartDate)
            {
                throw new InvalidOperationException("EndDateTime should be greater than StartDateTime");
            }

            var currentUser = _userContext.CurrentUser;

            if (currentUser.RoleId != (int)Roles.Moderator)
            {
                var defaultEndDateTime = _postDefaultValuesOptions.Value.DisplayPeriodInDaysForUsers;
                editPost.EndDate = editPost.StartDate.AddDays(defaultEndDateTime);
            }

            var oldEntity = await _db.Posts.GetAsync(postId);

            if (oldEntity == null)
            {
                throw new InvalidOperationException($"Post by id: {postId} not found.");
            }

            if (oldEntity.Priority == PostPriority.Normal && editPost.Priority != oldEntity.Priority && currentUser.RoleId != Role.ModeratorRoleId)
            {
                throw new InvalidOperationException("This user has no rights to set high priority for the post");
            }

            oldEntity.Title             = editPost.Title;
            oldEntity.Content           = editPost.Content;
            oldEntity.DurationInSeconds = editPost.DurationInSeconds;
            oldEntity.VideoUrl          = editPost.VideoUrl;
            oldEntity.ModifiedByUserId  = _userContext.CurrentUser.Id;
            oldEntity.ModifiedDate      = DateTime.Now;
            oldEntity.Priority          = editPost.Priority;
            oldEntity.StartDateTime     = editPost.StartDate;
            oldEntity.EndDateTime       = editPost.EndDate;

            if (currentUser.RoleDTO.Name == "User" && oldEntity.IsPublic)
            {
                oldEntity.IsPublic = false;
            }

            var newFiles      = FileMapper.ConvertToPostFileCollection(editPost.Files);
            var filesToRemove = (await _fileService.GetFilesForPost(oldEntity.Id))
                                .Where(oldfile => !newFiles.Exists(newFile => newFile.File.Id == oldfile.Id)).ToList();

            foreach (var file in filesToRemove)
            {
                await _fileService.DeleteAsync(file.Id);
            }

            oldEntity.PostFiles = newFiles;

            var updatedPost = await _db.Posts.Update(oldEntity);

            await _db.SaveAsync();

            var updatedPostDTO = PostMapper.ConvertToPostDTO(updatedPost);

            updatedPostDTO.UserDTO = UserMapper.ConvertToUserDTO(updatedPost.User);

            return(updatedPostDTO);
        }