Пример #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);
        }
Пример #2
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);
        }
Пример #3
0
        public async Task <PostDTO> CreateAsync(PostDTO post)
        {
            if (post.EndDateTime <= post.StartDateTime)
            {
                throw new InvalidOperationException("EndDateTime should be greater than StartDateTime");
            }

            var currentUser = _userContext.CurrentUser;

            if (currentUser.RoleId != (int)Roles.Moderator)
            {
                var defaultEndDateTime = _postDefaultValuesOptions.Value.DisplayPeriodInDaysForUsers;
                post.EndDateTime = post.StartDateTime.AddDays(defaultEndDateTime);
            }

            if (post.CreatedDate == default(DateTime))
            {
                post.CreatedDate = DateTime.Now;
            }
            if (post.PostType == PostType.None)
            {
                post.PostType = PostType.InTouch;
            }
            post.IsDeleted = false;


            post.UserId = currentUser.Id;

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

            post.IsPublic = false;

            var postToSave = PostMapper.ConvertToPost(post);

            postToSave.PostFiles = FileMapper.ConvertToPostFileCollection(post.Files);

            var createdPost = await _db.Posts.CreateAsync(postToSave);

            await _db.SaveAsync();

            var createdPostDTO = PostMapper.ConvertToPostDTO(createdPost);

            createdPostDTO.UserDTO = currentUser;
            return(createdPostDTO);
        }
Пример #4
0
        public async Task <IActionResult> CreateAsync([FromBody] PostCreateViewModel postCreateModel)
        {
            if (postCreateModel == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            List <FileInfoDTO> filesDTO = new List <FileInfoDTO>();

            postCreateModel.VideoUrl = string.IsNullOrEmpty(postCreateModel.VideoUrl) ? null : postCreateModel.VideoUrl;

            var offset = TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow);

            postCreateModel.StartDate = postCreateModel.StartDate.AddHours(offset.TotalHours);
            postCreateModel.EndDate   = postCreateModel.EndDate.AddHours(offset.TotalHours);

            foreach (var file in postCreateModel.Files)
            {
                var createRequestFile = FileMapper.ConvertToCreateFileRequest(file);

                using (var stream = _fileManager.GetFileStream(file))
                {
                    var createdPostFileDTO = await _fileService.CreateAsync(createRequestFile, stream);

                    filesDTO.Add(createdPostFileDTO);
                }
            }

            var post = PostMapper.ConvertToPostDTO(postCreateModel);

            post.Files = filesDTO;
            var createdPostDTO = await _postService.CreateAsync(post);

            var currentUser   = _userContext.CurrentUser;
            var postViewModel = PostMapper.ConvertToPostViewModel(createdPostDTO);

            if (currentUser.RoleId != (int)Roles.Moderator)
            {
                _emailService.PostCreatedNotificationAsync(postViewModel);
            }
            return(Ok(createdPostDTO));
        }
Пример #5
0
        public async Task <IEnumerable <PostDTO> > GetPostsAsync()
        {
            var posts = await _db.Posts.GetAllAsync();

            var postsDTO = new List <PostDTO>();

            //var currentDateTime = DateTime.Now;

            posts.ForEach(p =>
            {
                var postDTO = PostMapper.ConvertToPostDTO(p);

                postDTO.Files = FileMapper.ConvertToFileInfoDTOCollection(p.PostFiles);
                postsDTO.Add(postDTO);
            });
            return(postsDTO);
        }
Пример #6
0
 public void Mapper_ConvertToPostDTO_should_throw_argument_null_exception_when_input_argument_is_null()
 {
     Assert.Throws <ArgumentNullException>(() => PostMapper.ConvertToPostDTO(null));
 }
Пример #7
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);
        }