Skip to content
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] 팀 탈퇴 기능 구현 #181

Merged
merged 19 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/com/tiki/server/external/service/S3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public class S3Service {

private final S3Handler s3Handler;

public PreSignedUrlResponse getUploadPreSignedUrl(String fileFormat) {
public PreSignedUrlResponse getUploadPreSignedUrl(final String fileFormat) {
return s3Handler.getUploadPreSignedUrl(fileFormat);
}

public void deleteFile(S3DeleteRequest request) {
public void deleteFile(final S3DeleteRequest request) {
s3Handler.deleteFile(request.fileName());
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/tiki/server/external/util/S3Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class S3Handler {
@Value("${aws-property.bucket}")
private String bucket;

public PreSignedUrlResponse getUploadPreSignedUrl(String fileFormat) {
public PreSignedUrlResponse getUploadPreSignedUrl(final String fileFormat) {
try {
String fileName = generateFileName(fileFormat);
String key = FILE_SAVE_PREFIX + fileName;
Expand All @@ -46,7 +46,7 @@ public PreSignedUrlResponse getUploadPreSignedUrl(String fileFormat) {
}
}

public void deleteFile(String request) {
public void deleteFile(final String request) {
try {
S3Client s3Client = awsConfig.getS3Client();
s3Client.deleteObject((DeleteObjectRequest.Builder builder) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
public record FolderNameUpdateRequest(
@NonNull String name
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.tiki.server.memberteammanager.controller;

import com.tiki.server.common.dto.BaseResponse;
import com.tiki.server.memberteammanager.service.MemberTeamManagerService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;


@RestController
@AllArgsConstructor
@RequestMapping("api/v1/teams")
public class MemberTeamManagerController {

private final MemberTeamManagerService memberTeamManagerService;

@DeleteMapping("/{teamId}/members/{kickOutMemberId}")
public ResponseEntity<BaseResponse> kickOutMemberFromTeam(
final Principal principal,
@PathVariable final long teamId,
@PathVariable final long kickOutMemberId
) {
long memberId = Long.parseLong(principal.getName());
memberTeamManagerService.kickOutMemberFromTeam(memberId, teamId, kickOutMemberId);
return ResponseEntity.noContent().build();
}

@DeleteMapping("/{teamId}/leave")
public ResponseEntity<BaseResponse> leaveTeam(
final Principal principal,
@PathVariable final long teamId
) {
long memberId = Long.parseLong(principal.getName());
memberTeamManagerService.leaveTeam(memberId, teamId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
package com.tiki.server.memberteammanager.service;

import com.tiki.server.memberteammanager.adapter.MemberTeamManagerDeleter;
import com.tiki.server.memberteammanager.adapter.MemberTeamManagerFinder;
import com.tiki.server.memberteammanager.adapter.MemberTeamManagerSaver;
import com.tiki.server.memberteammanager.entity.MemberTeamManager;
import com.tiki.server.note.adapter.NoteFinder;
import com.tiki.server.note.entity.Note;
import com.tiki.server.team.exception.TeamException;
import com.tiki.server.memberteammanager.service.dto.response.MemberTeamPositionGetResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;

import java.util.List;

import static com.tiki.server.common.entity.Position.ADMIN;
import static com.tiki.server.team.message.ErrorCode.INVALID_AUTHORIZATION_DELETE;
import static com.tiki.server.team.message.ErrorCode.TOO_HIGH_AUTHORIZATION;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MemberTeamManagerService {

private final NoteFinder noteFinder;
private final MemberTeamManagerFinder memberTeamManagerFinder;
private final MemberTeamManagerDeleter memberTeamManagerDeleter;
private final MemberTeamManagerSaver memberTeamManagerSaver;

@Transactional
public void kickOutMemberFromTeam(final long memberId, final long teamId, final long kickOutMemberId) {
checkIsAdmin(memberId, teamId);
MemberTeamManager memberTeamManager = memberTeamManagerFinder.findByMemberIdAndTeamId(kickOutMemberId, teamId);
deleteNoteDependency(kickOutMemberId, teamId);
memberTeamManagerDeleter.delete(memberTeamManager);
}

@Transactional
public void leaveTeam(final long memberId, final long teamId) {
MemberTeamManager memberTeamManager = checkIsNotAdmin(memberId, teamId);
deleteNoteDependency(memberId, teamId);
memberTeamManagerDeleter.delete(memberTeamManager);
}

public MemberTeamPositionGetResponse getPosition(final long memberId, final long teamId) {
MemberTeamManager memberTeamManager = memberTeamManagerFinder.findByMemberIdAndTeamId(memberId, teamId);
Expand All @@ -25,4 +54,24 @@ public void updateTeamMemberName(final long memberId, final long teamId, final S
MemberTeamManager memberTeamManager = memberTeamManagerFinder.findByMemberIdAndTeamId(memberId, teamId);
memberTeamManager.updateName(name);
}

private void checkIsAdmin(final long memberId, final long teamId) {
MemberTeamManager memberTeamManager = memberTeamManagerFinder.findByMemberIdAndTeamId(memberId, teamId);
if (!memberTeamManager.getPosition().equals(ADMIN)) {
throw new TeamException(INVALID_AUTHORIZATION_DELETE);
}
}

private MemberTeamManager checkIsNotAdmin(final long memberId, final long teamId) {
MemberTeamManager memberTeamManager = memberTeamManagerFinder.findByMemberIdAndTeamId(memberId, teamId);
if (memberTeamManager.getPosition().equals(ADMIN)) {
throw new TeamException(TOO_HIGH_AUTHORIZATION);
}
return memberTeamManager;
}

private void deleteNoteDependency(final long memberId, final long teamId) {
List<Note> notes = noteFinder.findAllByMemberIdAndTeamId(memberId, teamId);
notes.forEach(Note::deleteMemberDependency);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/tiki/server/note/adapter/NoteFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public List<Note> findByCreatedAtAfterOrderByModifiedAtAsc(
return noteRepository.findByTeamIdAndCreatedAtAfterOrderByCreatedAtAsc(createdAt, pageRequest, teamId);
}

public List<Note> findAllByMemberIdAndTeamId(final long memberId, final long teamId) {
return noteRepository.findAllByMemberIdAndTeamId(memberId, teamId);
}

public Note findById(final long noteId) {
return noteRepository.findById(noteId)
.orElseThrow(() -> new NoteException(INVALID_NOTE));
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/tiki/server/note/entity/Note.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public void updateValue(
this.noteType = noteType;
}

public void deleteMemberDependency() {
this.memberId = null;
}

private void checkAuthor(final long clientId) {
if (this.memberId != clientId) {
throw new NoteException(UPDATE_ONLY_AUTHOR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface NoteRepository extends JpaRepository<Note, Long> {

@Query("SELECT n FROM Note n WHERE n.teamId = :teamId AND n.createdAt > :createdAt ORDER BY n.createdAt ASC")
List<Note> findByTeamIdAndCreatedAtAfterOrderByCreatedAtAsc(@Param("createdAt") LocalDateTime createdAt, Pageable pageable, long teamId);

List<Note> findAllByMemberIdAndTeamId(long memberId, long TeamId);
}
10 changes: 6 additions & 4 deletions src/main/java/com/tiki/server/team/message/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package com.tiki.server.team.message;

import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.NOT_FOUND;

import org.springframework.http.HttpStatus;

import lombok.AllArgsConstructor;
import lombok.Getter;

import static org.springframework.http.HttpStatus.*;

@Getter
@AllArgsConstructor
public enum ErrorCode {

/* 400 BAD_REQUEST : 잘못된 요청 */
TOO_HIGH_AUTHORIZATION(BAD_REQUEST, "어드민은 진행할 수 없습니다."),

/* 403 FORBIDDEN : 권한 없음 */
INVALID_AUTHORIZATION_DELETE(FORBIDDEN, "팀 삭제에 대한 권한이 없습니다."),
INVALID_AUTHORIZATION_DELETE(FORBIDDEN, "권한이 없습니다."),

/* 404 NOT_FOUND : 자원을 찾을 수 없음 */
INVALID_TEAM(NOT_FOUND, "유효하지 않은 단체입니다.");
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/com/tiki/server/team/service/TeamService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.tiki.server.team.service;

import static com.tiki.server.common.entity.Position.ADMIN;
import static com.tiki.server.team.message.ErrorCode.INVALID_AUTHORIZATION_DELETE;

import java.util.List;

Expand Down Expand Up @@ -84,6 +83,10 @@ public void deleteTeam(final long memberId, final long teamId) {
teamDeleter.deleteById(teamId);
}

private Team createTeam(final TeamCreateRequest request, final University univ) {
return Team.of(request, univ);
}

@Transactional
public void updateTeamName(final long memberId, final long teamId, final String newTeamName) {
checkIsAdmin(memberId, teamId);
Expand All @@ -107,8 +110,8 @@ public void alterAdmin(final long memberId, final long teamId, final long target
newAdmin.updatePositionToAdmin();
}

private Team createTeam(final TeamCreateRequest request, final University univ) {
return Team.of(request, univ);
private MemberTeamManager createMemberTeamManager(final Member member, final Team team, final Position position) {
return MemberTeamManager.of(member, team, position);
}

private void deleteIconUrl(final Team team) {
Expand All @@ -117,10 +120,6 @@ private void deleteIconUrl(final Team team) {
}
}

private MemberTeamManager createMemberTeamManager(final Member member, final Team team, final Position position) {
return MemberTeamManager.of(member, team, position);
}

private MemberTeamManager checkIsAdmin(final long memberId, final long teamId) {
MemberTeamManager accessMember = memberTeamManagerFinder.findByMemberIdAndTeamId(memberId, teamId);
accessMember.checkMemberAccessible(ADMIN);
Expand Down
Loading