Skip to content

Commit

Permalink
club, submit, invite 도메인 리팩토링 (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
juno-junho authored Apr 24, 2024
1 parent 56a6257 commit 241af45
Show file tree
Hide file tree
Showing 30 changed files with 305 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -26,15 +25,18 @@ public class ClubEventController {
private final ClubEventService clubEventService;

@GetMapping("/{clubId}/events")
public ResponseEntity<PageResponse<ClubEventOverviewGetResponse, ClubEventOverviewGetInfo>> getClubEvents(@PathVariable Long clubId, @PageableDefault(size = 1000) Pageable pageable, @Authenticated JwtUser jwtUser) {
public PageResponse<ClubEventOverviewGetResponse, ClubEventOverviewGetInfo> getClubEvents(
@PathVariable Long clubId,
@PageableDefault(size = 1000) Pageable pageable,
@Authenticated JwtUser jwtUser
) {
Page<ClubEventOverviewGetInfo> events = clubEventService.getClubEvents(clubId, pageable, jwtUser.id());

List<ClubEventOverviewGetResponse> clubEventOverviewGetResponse = events.getContent()
.stream()
.map(ClubEventOverviewGetResponse::from)
.toList();

return ResponseEntity.ok(new PageResponse<>(clubEventOverviewGetResponse, events));
return new PageResponse<>(clubEventOverviewGetResponse, events);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,53 @@ public class ClubUserController {
private final ClubMemberManagerService clubMemberManagerService;

@GetMapping("/{clubId}/users")
public ResponseEntity<ClubUserRoleResponse> getClubUserRole(@PathVariable Long clubId, @Authenticated JwtUser jwtUser) {
public ResponseEntity<ClubUserRoleResponse> getClubUserRole(
@PathVariable Long clubId,
@Authenticated JwtUser jwtUser
) {
String role = clubMemberManagerService.getUserRole(clubId, jwtUser.id());

return ResponseEntity.ok(new ClubUserRoleResponse(role));
}

@PatchMapping("/{clubId}/members/{memberId}")
public ResponseEntity<Void> updateMemberRole(@PathVariable Long clubId, @PathVariable Long memberId, @RequestBody ClubUserUpdateRequest request, @Authenticated JwtUser jwtUser) {
public ResponseEntity<Void> updateMemberRole(
@PathVariable Long clubId,
@PathVariable Long memberId,
@RequestBody ClubUserUpdateRequest request,
@Authenticated JwtUser jwtUser
) {
clubMemberManagerService.updateMemberRole(new ClubUserUpdate(clubId, memberId, request.role(), jwtUser.id()));

return ResponseEntity.noContent().build();
}

@GetMapping("/{clubId}/members")
public ResponseEntity<List<MemberGetInfo>> getMembers(@PathVariable Long clubId, @Authenticated JwtUser jwtUser) {
public ResponseEntity<List<MemberGetInfo>> getMembers(
@PathVariable Long clubId,
@Authenticated JwtUser jwtUser
) {
List<MemberGetInfo> response = clubMemberManagerService.getMembers(clubId, jwtUser.id());

return ResponseEntity.ok(response);
}

@DeleteMapping("/{clubId}/members/{memberId}")
public ResponseEntity<Void> deleteMember(@PathVariable Long clubId, @PathVariable Long memberId, @Authenticated JwtUser jwtUser) {
public ResponseEntity<Void> deleteMember(
@PathVariable Long clubId,
@PathVariable Long memberId,
@Authenticated JwtUser jwtUser
) {
clubMemberManagerService.deleteMember(clubId, memberId, jwtUser.id());

return ResponseEntity.noContent().build();
}

@DeleteMapping("/{clubId}/users")
public ResponseEntity<Void> exitClub(@PathVariable Long clubId, @Authenticated JwtUser jwtUser) {
public ResponseEntity<Void> exitClub(
@PathVariable Long clubId,
@Authenticated JwtUser jwtUser
) {
clubMemberManagerService.exitClub(clubId, jwtUser.id());

return ResponseEntity.noContent().build();
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/com/spaceclub/club/domain/Club.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,4 @@ public Club updateLogoImage(String logoImageName) {
.build();
}

public Club updateCoverImage(String coverImageName) {
return Club.builder()
.id(this.id)
.name(this.name)
.logoImageName(this.logoImageName)
.coverImageName(coverImageName)
.info(this.info)
.notices(this.notices != null ? this.notices : null)
.clubUser(this.clubUser != null ? this.clubUser : null)
.createdAt(this.createdAt)
.build();
}

}
12 changes: 11 additions & 1 deletion src/main/java/com/spaceclub/club/domain/ClubUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,21 @@ public class ClubUser extends BaseTimeEntity {
private ClubUserRole role;

@Builder
public ClubUser(Long id, Club club, Long userId, ClubUserRole role) {
private ClubUser(Long id, Club club, Long userId, ClubUserRole role) {
this.id = id;
this.club = club;
this.userId = userId;
this.role = role;
}

public static ClubUser createManger(Club club, Long userId) {
return ClubUser.builder()
.club(club)
.userId(userId)
.role(ClubUserRole.MANAGER)
.build();
}

public ClubUser updateRole(ClubUserRole role) {
return ClubUser.builder()
.id(this.id)
Expand All @@ -66,4 +74,6 @@ public boolean isManager() {
return this.role.equals(ClubUserRole.MANAGER);
}



}
7 changes: 3 additions & 4 deletions src/main/java/com/spaceclub/club/domain/ClubUserRole.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.spaceclub.club.domain;

import lombok.Getter;

@Getter
public enum ClubUserRole {

MANAGER(1),
Expand All @@ -11,8 +14,4 @@ public enum ClubUserRole {
this.sortPriority = sortPriority;
}

public int getSortPriority() {
return sortPriority;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import com.spaceclub.club.domain.Club;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface ClubRepository extends JpaRepository<Club, Long> {

Optional<Club> findByName(String name);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ public interface ClubUserRepository extends JpaRepository<ClubUser, Long> {

List<ClubUser> findByUserId(Long userId);

boolean existsByClubAndUserId(Club club, Long userId);

Long countByClub(Club club);

ClubUser findByClub_IdAndRole(Long clubId, ClubUserRole role);

Boolean existsByClub_IdAndUserId(Long clubId, Long userId);

int countByUserId(Long userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public Page<ClubEventOverviewGetInfo> getClubEvents(Long clubId, Pageable pageab
}

private void checkClubMember(Long clubId, Long userId) {
if (!clubUserRepository.existsByClub_IdAndUserId(clubId, userId))
if (!clubUserRepository.existsByClub_IdAndUserId(clubId, userId)){
throw new IllegalArgumentException(NOT_CLUB_MEMBER.toString());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,8 @@ public class ClubMemberManagerService {
private final UserService userService;

@Transactional
public void updateMemberRole(ClubUserUpdate updateVo) {
this.validManager(updateVo.clubId(), updateVo.userId());

int count = clubUserRepository.countByClub_IdAndRole(updateVo.clubId(), MANAGER);

if (isSelfDegrading(count, updateVo)) {
throw new IllegalArgumentException(CAN_NOT_SELF_DEGRADING.toString());
}

ClubUser clubMember = this.getClubUser(updateVo.clubId(), updateVo.memberId());
ClubUser updateClubUser = clubMember.updateRole(updateVo.role());

clubUserRepository.save(updateClubUser);
public void save(ClubUser clubUser) {
clubUserRepository.save(clubUser);
}

public List<MemberGetInfo> getMembers(Long clubId, Long userId) {
Expand All @@ -61,10 +50,6 @@ public List<MemberGetInfo> getMembers(Long clubId, Long userId) {
.toList();
}

public Long countMember(Club club) {
return clubUserRepository.countByClub(club);
}

public String getUserRole(Long clubId, Long userId) {
Optional<ClubUser> clubUser = clubUserRepository.findByClub_IdAndUserId(clubId, userId);

Expand All @@ -73,6 +58,26 @@ public String getUserRole(Long clubId, Long userId) {

}

@Transactional
public void updateMemberRole(ClubUserUpdate updateVo) {
validManager(updateVo.clubId(), updateVo.userId());

int count = clubUserRepository.countByClub_IdAndRole(updateVo.clubId(), MANAGER);

if (isSelfDegrading(count, updateVo)) {
throw new IllegalArgumentException(CAN_NOT_SELF_DEGRADING.toString());
}

ClubUser clubMember = getClubUser(updateVo.clubId(), updateVo.memberId());
ClubUser updateClubUser = clubMember.updateRole(updateVo.role());

clubUserRepository.save(updateClubUser);
}

public Long countMember(Club club) {
return clubUserRepository.countByClub(club);
}

@Transactional
public void exitClub(Long clubId, Long memberId) {
ClubUser clubUser = this.getClubUser(clubId, memberId);
Expand All @@ -83,7 +88,7 @@ public void exitClub(Long clubId, Long memberId) {

@Transactional
public void deleteMember(Long clubId, Long memberId, Long userId) {
this.validManager(clubId, userId);
validManager(clubId, userId);

int count = clubUserRepository.countByClub_IdAndRole(clubId, MANAGER);

Expand All @@ -93,7 +98,7 @@ public void deleteMember(Long clubId, Long memberId, Long userId) {
throw new IllegalArgumentException(CAN_NOT_WITHDRAW.toString());
}

ClubUser clubMember = this.getClubUser(clubId, memberId);
ClubUser clubMember = getClubUser(clubId, memberId);

if (clubMember.isManager()) throw new IllegalStateException(UNAUTHORIZED.toString());

Expand Down Expand Up @@ -126,9 +131,4 @@ public boolean isAlreadyJoined(Club club, Long userId) {
return clubUserRepository.existsByClub_IdAndUserId(club.getId(), userId);
}

@Transactional
public void save(ClubUser clubUser) {
clubUserRepository.save(clubUser);
}

}
59 changes: 23 additions & 36 deletions src/main/java/com/spaceclub/club/service/ClubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.spaceclub.global.aws.s3.S3Folder;
import com.spaceclub.global.aws.s3.S3ImageUploader;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -21,7 +20,6 @@
import static com.spaceclub.club.ClubExceptionMessage.DUPLICATED_CLUB_NAME;
import static com.spaceclub.club.ClubExceptionMessage.NOT_CLUB_MEMBER;
import static com.spaceclub.club.ClubExceptionMessage.UNAUTHORIZED;
import static com.spaceclub.club.domain.ClubUserRole.MANAGER;

@Service
@Transactional(readOnly = true)
Expand All @@ -40,29 +38,20 @@ public class ClubService implements ClubProvider {

@Transactional
public Club createClub(Club club, Long userId, MultipartFile logoImage) {
if (logoImage != null) {
String logoImageName = imageUploader.upload(logoImage, S3Folder.LOGO);
club = club.updateLogoImage(logoImageName);
}

ClubUser clubUser = ClubUser.builder()
.club(club)
.userId(userId)
.role(MANAGER)
.build();
try {
clubUserRepository.save(clubUser);
return clubRepository.save(club);
} catch (DataIntegrityViolationException e) {
throw new IllegalArgumentException(DUPLICATED_CLUB_NAME.toString());
}
club = uploadImage(logoImage, club, S3Folder.LOGO);
ClubUser clubUser = ClubUser.createManger(club, userId);

clubRepository.findByName(club.getName()).ifPresent(duplicateClubName -> {
throw new IllegalArgumentException(DUPLICATED_CLUB_NAME.toString());
});
clubUserRepository.save(clubUser);
return clubRepository.save(club);
}

public Club getClub(Long clubId, Long userId) {
ClubUser clubUser = clubUserRepository.findByClub_IdAndUserId(clubId, userId)
.orElseThrow(() -> new IllegalArgumentException(NOT_CLUB_MEMBER.toString()));

return clubUser.getClub();
return clubUserRepository.findByClub_IdAndUserId(clubId, userId)
.orElseThrow(() -> new IllegalArgumentException(NOT_CLUB_MEMBER.toString()))
.getClub();
}

@Transactional
Expand All @@ -88,24 +77,22 @@ public void updateClub(Club newClub, Long userId, MultipartFile logoImage, Multi
Club club = clubRepository.findById(clubId)
.orElseThrow(() -> new IllegalArgumentException(CLUB_NOT_FOUND.toString()));

if (logoImage != null) {
String logoImageName = imageUploader.upload(logoImage, S3Folder.LOGO);
club = club.updateLogoImage(logoImageName);
}

if (coverImage != null) {
String coverImageName = imageUploader.upload(coverImage, S3Folder.COVER);
club = club.updateCoverImage(coverImageName);
}

club = uploadImage(logoImage, club, S3Folder.LOGO);
club = uploadImage(coverImage, club, S3Folder.COVER);
Club updatedClub = club.update(newClub);

try {
clubRepository.save(updatedClub);
} catch (DataIntegrityViolationException e) {
clubRepository.findByName(updatedClub.getName()).ifPresent(duplicateClubName -> {
throw new IllegalArgumentException(DUPLICATED_CLUB_NAME.toString());
}
});
clubRepository.save(updatedClub);
}

private Club uploadImage(MultipartFile logoImage, Club club, S3Folder folderName) {
if (logoImage != null) {
String logoImageName = imageUploader.upload(logoImage, folderName);
club = club.updateLogoImage(logoImageName);
}
return club;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;

Expand All @@ -37,10 +36,10 @@ public ResponseEntity<Void> create(
}

@GetMapping("/{eventId}/forms")
public ResponseEntity<FormGetResponse> get(@PathVariable Long eventId, @Authenticated JwtUser jwtUser) {
FormGetInfo formGetInfoVo = formService.get(eventId);
public ResponseEntity<FormGetResponse> get(@PathVariable Long eventId) {
FormGetInfo formInfo = formService.get(eventId);

return ResponseEntity.ok(FormGetResponse.from(formGetInfoVo));
return ResponseEntity.ok(FormGetResponse.from(formInfo));
}

}
Loading

0 comments on commit 241af45

Please sign in to comment.