-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: 게시글 공동저자 설정 / 공개 범위 설정 / 소속 팀 설정 API 기능 구현 #299
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,8 @@ | |
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
@@ -55,6 +57,31 @@ public class PostCommandService { | |
private final CategoryService categoryService; | ||
private final AmazonS3Manager s3Manager; | ||
|
||
|
||
private void validatePostAccess(Post post, Member member) { | ||
// MEMBER 접근 권한: 작성자만 접근 가능 | ||
if (post.getPostAccess() == PostAccess.MEMBER && !post.getMember().equals(member)) { | ||
throw new GeneralException(ErrorStatus.NO_ACCESS_PERMISSION); | ||
} | ||
// TEAM 접근 권한: 팀 멤버만 접근 가능 | ||
if (post.getPostAccess() == PostAccess.TEAM && post.getTeam() != null) { | ||
if (!teamRepository.isTeamMember(post.getTeam(), member)) { | ||
throw new GeneralException(ErrorStatus.NO_ACCESS_PERMISSION); | ||
} | ||
} | ||
} | ||
|
||
private Member getAuthenticatedMember() { | ||
// 현재 인증된 사용자 가져오기 | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
// username (식별자) 가져오기 | ||
String username = authentication.getName(); | ||
// username으로 Member 엔터티 조회 | ||
return memberRepository.findByEmail(username) | ||
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND)); | ||
} | ||
|
||
|
||
// 포스트 생성 | ||
public Post createPost(Long memberId, PostRequestDTO.CreatePostRequestDTO request) { | ||
// validation: member|team|project 유무 확인 (team 및 project 없는 경우 null) | ||
|
@@ -204,4 +231,49 @@ public Post setPostCategories(Long postId, Set<String> categoryNames) { | |
return postRepository.save(post); | ||
} | ||
|
||
|
||
public Post updateCoauthors(Long postId, PostRequestDTO.UpdateCoauthorRequestDTO request) { | ||
Post post = postRepository.findById(postId).orElseThrow(() -> new GeneralException(ErrorStatus.POST_NOT_FOUND)); | ||
Member authenticatedMember = getAuthenticatedMember(); | ||
validatePostAccess(post, authenticatedMember); | ||
// 기존 공동 저자 리스트 삭제 | ||
post.getAuthorList().clear(); | ||
// 새로운 공동 저자 리스트 추가 | ||
Set<Author> coauthors = request.getMemberIds().stream() | ||
.map(newCoauthorId -> { | ||
Member newCoauthor = memberRepository.findById(newCoauthorId).orElseThrow(() -> new IllegalArgumentException("Member not found: " + newCoauthorId)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예외처리 GeneralException 사용 안하신 이유가 있나요? |
||
return Author.createAuthors(post, newCoauthor); | ||
}) | ||
.collect(Collectors.toSet()); | ||
post.getAuthorList().addAll(coauthors); | ||
|
||
return postRepository.save(post); | ||
} | ||
|
||
|
||
public Post setPostTeam(Long postId, Long teamId) { | ||
Post post = postRepository.findById(postId).orElseThrow(() -> new GeneralException(ErrorStatus.POST_NOT_FOUND)); | ||
|
||
Member authenticatedMember = getAuthenticatedMember(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getAuthenticatedMember를 만드신 이유가 궁금합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재 로그인한 사용자가 게시글을 작성한 저자와 일치하는지 확인하기 위해 썼습니다! |
||
validatePostAccess(post, authenticatedMember); | ||
|
||
Team team = teamRepository.findById(teamId).orElseThrow(() -> new GeneralException(ErrorStatus.TEAM_NOT_FOUND)); | ||
post.setTeam(team); | ||
|
||
return postRepository.save(post); | ||
} | ||
|
||
|
||
public Post updateVisibility(Long postId, PostRequestDTO.UpdateVisibilityRequestDTO request) { | ||
Post post = postRepository.findById(postId).orElseThrow(() -> new GeneralException(ErrorStatus.POST_NOT_FOUND)); | ||
Member authenticatedMember = getAuthenticatedMember(); | ||
|
||
if (!post.getMember().equals(authenticatedMember)) { throw new GeneralException(ErrorStatus.POST_ACCESS_SET_UNAUTHORIZED);} | ||
post.setPostAccess(PostAccess.valueOf(request.getPostAccess())); | ||
|
||
return postRepository.save(post); | ||
} | ||
|
||
|
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
멤버정보는
@AuthenticationPrincipal CustomMemberDetails customMemberDetails
사용하면 될 것 같습니다!