示例#1
0
        public async Task UpdatePostAsync(UpdatePostDTO dto)
        {
            var post = await FindPostAsync(dto.Post.Id);

            var updatedPost = _mapper.Map <Post>(dto.Post);

            //Update the values for the post
            _mapper.Map(updatedPost, post);

            //Update the users set to the given post.
            foreach (var username in dto.UserNames)
            {
                var updateUserPostsDto = new UpdateUserPostsDTO {
                    UserName = username, PostIds = new int[] { post.Id }
                };
                await SetUserPostsAsync(updateUserPostsDto);
            }

            //Update the roles given to the post.
            var modDto = new ModifyPostRolesDTO()
            {
                Id = dto.Post.Id, Roles = dto.Roles
            };

            await SetPostRolesAsync(modDto);

            if (!await _postRepository.SaveAll())
            {
                throw new ApplicationException("Could not update the given post.");
            }
        }
示例#2
0
        public async Task <PostDTO> SetPostRolesAsync(ModifyPostRolesDTO dto)
        {
            var post = await FindPostAsync(dto.Id);

            if (post.Roles.Count != 0)
            {
                post.Roles.Clear();

                if (!await _postRepository.SaveAll())
                {
                    throw new ApplicationException("Something went wrong clearing the roles of the given post.");
                }
            }

            var newRoles = _roleManager.Roles.Where(r => dto.Roles.Any(n => n == r.Name));

            foreach (var role in newRoles)
            {
                post.Roles.Add(new PostRole
                {
                    PostId = post.Id,
                    RoleId = role.Id
                });
            }

            if (!await _postRepository.SaveAll())
            {
                throw new ApplicationException("Something went wrong adding new roles to the post.");
            }

            foreach (var user in post.Users)
            {
                await RefreshUserPostRolesAsync(user.UserId);
            }

            var postToReturn = _mapper.Map <PostDTO>(post);

            return(postToReturn);
        }
示例#3
0
        public async Task <IActionResult> ModifyPostRoles(ModifyPostRolesDTO dto)
        {
            var post = await _postService.SetPostRolesAsync(dto);

            return(Ok(post));
        }