Beispiel #1
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);
        }
Beispiel #2
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);
        }