Skip to content

Commit

Permalink
[BE] refactor/#279 permission 패키지 분리 (#280)
Browse files Browse the repository at this point in the history
* refactor: Permission Enum 네이밍 변경(PermissionType)

* refactor: MemberTopicPermission 네이밍 변경(Permission) 및 패키지 분리

* refactor: Service 및 Controller 분리

* test: 테스트 오류 수정

* refactor: 메서드 네임 수정

* docs: restDocs 추가

* refactor: 메서드 네이밍 변경
  • Loading branch information
junpakPark authored Aug 14, 2023
1 parent e32bc89 commit 83200a1
Show file tree
Hide file tree
Showing 43 changed files with 870 additions and 707 deletions.
1 change: 1 addition & 0 deletions backend/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ toc::[]
include::topic.adoc[]
include::pin.adoc[]
include::member.adoc[]
include::permission.adoc[]
include::oauth.adoc[]
16 changes: 0 additions & 16 deletions backend/src/docs/asciidoc/member.adoc
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
== 유저

=== 유저 권한 부여

operation::member-controller-test/add-member-topic-permission[snippets='http-request,http-response']

=== 유저 권한 삭제

operation::member-controller-test/delete-member-topic-permission[snippets='http-request,http-response']

=== 토픽에 권한을 가진 유저 목록 조회

operation::member-controller-test/find-member-topic-permission-all[snippets='http-request,http-response']

=== 토픽에 권한을 가진 유저 단일 조회

operation::member-controller-test/find-member-topic-permission-by-id[snippets='http-request,http-response']

=== 유저 목록 조회

operation::member-controller-test/find-all-member[snippets='http-request,http-response']
Expand Down
17 changes: 17 additions & 0 deletions backend/src/docs/asciidoc/permission.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
== 권한

=== 권한 부여

operation::permission-controller-test/add-permission[snippets='http-request,http-response']

=== 권한 삭제

operation::permission-controller-test/delete-permission[snippets='http-request,http-response']

=== 토픽에 권한을 가진 유저 목록 조회

operation::permission-controller-test/find-all-topic-permissions[snippets='http-request,http-response']

=== 토픽에 권한을 가진 유저 단일 조회

operation::permission-controller-test/find-permission-by-id[snippets='http-request,http-response']
Original file line number Diff line number Diff line change
@@ -1,90 +0,0 @@
package com.mapbefine.mapbefine.member.application;

import com.mapbefine.mapbefine.auth.domain.AuthMember;
import com.mapbefine.mapbefine.member.domain.Member;
import com.mapbefine.mapbefine.member.domain.MemberRepository;
import com.mapbefine.mapbefine.member.domain.MemberTopicPermission;
import com.mapbefine.mapbefine.member.domain.MemberTopicPermissionRepository;
import com.mapbefine.mapbefine.member.dto.request.MemberTopicPermissionCreateRequest;
import com.mapbefine.mapbefine.topic.domain.Topic;
import com.mapbefine.mapbefine.topic.domain.TopicRepository;
import java.util.NoSuchElementException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class MemberCommandService {

private final MemberRepository memberRepository;
private final TopicRepository topicRepository;
private final MemberTopicPermissionRepository memberTopicPermissionRepository;

public MemberCommandService(
MemberRepository memberRepository,
TopicRepository topicRepository,
MemberTopicPermissionRepository memberTopicPermissionRepository
) {
this.memberRepository = memberRepository;
this.topicRepository = topicRepository;
this.memberTopicPermissionRepository = memberTopicPermissionRepository;
}

public Long saveMemberTopicPermission(AuthMember authMember, MemberTopicPermissionCreateRequest request) {
Member member = memberRepository.findById(request.memberId())
.orElseThrow(NoSuchElementException::new);
Topic topic = topicRepository.findById(request.topicId())
.orElseThrow(NoSuchElementException::new);

validateSaveMemberTopicPermission(authMember, request, member, topic);

MemberTopicPermission memberTopicPermission =
MemberTopicPermission.createPermissionAssociatedWithTopicAndMember(topic, member);

return memberTopicPermissionRepository.save(memberTopicPermission).getId();
}

private void validateSaveMemberTopicPermission(
AuthMember authMember,
MemberTopicPermissionCreateRequest request,
Member member,
Topic topic
) {
validateMemberCanTopicUpdate(authMember, topic);
validateSelfPermission(authMember, request);
validateDuplicatePermission(topic.getId(), member.getId());
}

private void validateMemberCanTopicUpdate(AuthMember authMember, Topic topic) {
if (authMember.canTopicUpdate(topic)) {
return;
}

throw new IllegalArgumentException("해당 유저는 해당 토픽에서 다른 유저에게 권한을 줄 수 없습니다.");
}

private void validateSelfPermission(
AuthMember authMember,
MemberTopicPermissionCreateRequest request
) {
if (authMember.getMemberId().equals(request.memberId())) {
throw new IllegalArgumentException("본인에게 권한을 줄 수 없습니다.");
}
}

private void validateDuplicatePermission(Long topicId, Long memberId) {
if (memberTopicPermissionRepository.existsByTopicIdAndMemberId(topicId, memberId)) {
throw new IllegalArgumentException("권한은 중복으로 줄 수 없습니다.");
}
}

public void deleteMemberTopicPermission(AuthMember authMember, Long permissionId) {
MemberTopicPermission memberTopicPermission = memberTopicPermissionRepository.findById(permissionId)
.orElseThrow(NoSuchElementException::new);

validateMemberCanTopicUpdate(authMember, memberTopicPermission.getTopic());

memberTopicPermissionRepository.delete(memberTopicPermission);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
import com.mapbefine.mapbefine.auth.domain.AuthMember;
import com.mapbefine.mapbefine.member.domain.Member;
import com.mapbefine.mapbefine.member.domain.MemberRepository;
import com.mapbefine.mapbefine.member.domain.MemberTopicPermission;
import com.mapbefine.mapbefine.member.domain.MemberTopicPermissionRepository;
import com.mapbefine.mapbefine.member.dto.response.MemberDetailResponse;
import com.mapbefine.mapbefine.member.dto.response.MemberResponse;
import com.mapbefine.mapbefine.member.dto.response.MemberTopicPermissionDetailResponse;
import com.mapbefine.mapbefine.member.dto.response.MemberTopicPermissionResponse;
import com.mapbefine.mapbefine.pin.domain.Pin;
import com.mapbefine.mapbefine.pin.domain.PinRepository;
import com.mapbefine.mapbefine.pin.dto.response.PinResponse;
Expand All @@ -28,18 +24,15 @@ public class MemberQueryService {
private final MemberRepository memberRepository;
private final TopicRepository topicRepository;
private final PinRepository pinRepository;
private final MemberTopicPermissionRepository memberTopicPermissionRepository;

public MemberQueryService(
MemberRepository memberRepository,
TopicRepository topicRepository,
PinRepository pinRepository,
MemberTopicPermissionRepository memberTopicPermissionRepository
PinRepository pinRepository
) {
this.memberRepository = memberRepository;
this.topicRepository = topicRepository;
this.pinRepository = pinRepository;
this.memberTopicPermissionRepository = memberTopicPermissionRepository;
}

public MemberDetailResponse findById(Long id) {
Expand All @@ -49,13 +42,15 @@ public MemberDetailResponse findById(Long id) {
return MemberDetailResponse.from(member);
}

// TODO: 2023/08/14 해당 메서드는 ADMIN만 접근 가능하게 리팩터링 하기
public List<MemberResponse> findAll() {
return memberRepository.findAll()
.stream()
.map(MemberResponse::from)
.toList();
}

// TODO: 2023/08/14 해당 메서드는 TopicQueryService로 옮기기
public List<TopicResponse> findTopicsByMember(AuthMember authMember) {
validateNonExistsMember(authMember.getMemberId());
List<Topic> topicsByCreator = topicRepository.findByCreatorId(authMember.getMemberId());
Expand All @@ -65,6 +60,7 @@ public List<TopicResponse> findTopicsByMember(AuthMember authMember) {
.toList();
}

// TODO: 2023/08/14 해당 메서드는 PinQueryService로 옮기기
public List<PinResponse> findPinsByMember(AuthMember authMember) {
validateNonExistsMember(authMember.getMemberId());
List<Pin> pinsByCreator = pinRepository.findByCreatorId(authMember.getMemberId());
Expand All @@ -73,28 +69,11 @@ public List<PinResponse> findPinsByMember(AuthMember authMember) {
.map(PinResponse::from)
.toList();
}
public void validateNonExistsMember(Long memberId) {

private void validateNonExistsMember(Long memberId) {
if (Objects.isNull(memberId)) {
throw new IllegalArgumentException("존재하지 않는 유저입니다.");
}
}

public List<MemberTopicPermissionResponse> findAllWithPermission(Long topicId) {
Topic topic = topicRepository.findById(topicId)
.orElseThrow(NoSuchElementException::new);

return memberTopicPermissionRepository.findAllByTopic(topic)
.stream()
.map(MemberTopicPermissionResponse::from)
.toList();
}

public MemberTopicPermissionDetailResponse findMemberTopicPermissionById(Long permissionId) {
MemberTopicPermission memberTopicPermission = memberTopicPermissionRepository.findById(permissionId)
.orElseThrow(NoSuchElementException::new);

return MemberTopicPermissionDetailResponse.from(memberTopicPermission);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static lombok.AccessLevel.PROTECTED;

import com.mapbefine.mapbefine.common.entity.BaseTimeEntity;
import com.mapbefine.mapbefine.permission.domain.Permission;
import com.mapbefine.mapbefine.pin.domain.Pin;
import com.mapbefine.mapbefine.topic.domain.Topic;
import jakarta.persistence.Embedded;
Expand Down Expand Up @@ -42,7 +43,7 @@ public class Member extends BaseTimeEntity {
private List<Pin> createdPins = new ArrayList<>();

@OneToMany(mappedBy = "member")
private List<MemberTopicPermission> topicsWithPermissions = new ArrayList<>();
private List<Permission> topicsWithPermissions = new ArrayList<>();

private Member(MemberInfo memberInfo, OauthId oauthId) {
this.memberInfo = memberInfo;
Expand Down Expand Up @@ -105,8 +106,8 @@ public void addPin(Pin pin) {
createdPins.add(pin);
}

public void addMemberTopicPermission(MemberTopicPermission memberTopicPermission) {
topicsWithPermissions.add(memberTopicPermission);
public void addMemberTopicPermission(Permission permission) {
topicsWithPermissions.add(permission);
}

public String getRoleKey() {
Expand All @@ -122,7 +123,7 @@ public boolean isUser() {

public List<Topic> getTopicsWithPermissions() {
return topicsWithPermissions.stream()
.map(MemberTopicPermission::getTopic)
.map(Permission::getTopic)
.toList();
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 83200a1

Please sign in to comment.