diff --git a/src/main/java/com/jangburich/domain/team/application/TeamService.java b/src/main/java/com/jangburich/domain/team/application/TeamService.java index c9df54c..8a97d2c 100644 --- a/src/main/java/com/jangburich/domain/team/application/TeamService.java +++ b/src/main/java/com/jangburich/domain/team/application/TeamService.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.UUID; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -19,6 +20,7 @@ import com.jangburich.domain.team.dto.response.MyTeamResponse; import com.jangburich.domain.team.dto.response.TeamCodeResponse; import com.jangburich.domain.team.dto.response.TeamMemberResponse; +import com.jangburich.domain.team.dto.response.TeamSecretCodeResponse; import com.jangburich.domain.user.domain.User; import com.jangburich.domain.user.repository.UserRepository; import com.jangburich.global.payload.Message; @@ -38,7 +40,7 @@ public class TeamService { private final UserTeamRepository userTeamRepository; @Transactional - public Message registerTeam(String userId, RegisterTeamRequest registerTeamRequest) { + public TeamSecretCodeResponse registerTeam(String userId, RegisterTeamRequest registerTeamRequest) { User user = userRepository.findByProviderId(userId) .orElseThrow(() -> new NullPointerException()); @@ -47,19 +49,19 @@ public Message registerTeam(String userId, RegisterTeamRequest registerTeamReque .description(registerTeamRequest.description()) .teamLeader(new TeamLeader(user.getUserId(), registerTeamRequest.teamLeaderAccountNumber(), registerTeamRequest.bankName())) - .secretCode(registerTeamRequest.secretCode()) .point(ZERO) .teamType(TeamType.valueOf(registerTeamRequest.teamType())) .build(); - teamRepository.save(team); + Team saved = teamRepository.save(team); UserTeam userTeam = UserTeam.of(user, team); userTeamRepository.save(userTeam); - return Message.builder() - .message("팀 생성이 완료되었습니다.") - .build(); + TeamSecretCodeResponse teamSecretCodeResponse = new TeamSecretCodeResponse(); + teamSecretCodeResponse.setUuid(saved.getSecretCode()); + + return teamSecretCodeResponse; } @Transactional @@ -134,15 +136,13 @@ public MyTeamDetailsResponse getTeamDetailsById(String userId, Long teamId) { if (!team.getTeamLeader().getUser_id().equals(user.getUserId())) { // 일반 구성원 - MyTeamDetailsResponse myTeamDetailsAsMember = teamRepository.findMyTeamDetailsAsMember(user.getUserId(), + return teamRepository.findMyTeamDetailsAsMember(user.getUserId(), teamId); - return myTeamDetailsAsMember; } // 팀 리더일 때 - MyTeamDetailsResponse myTeamDetailsAsLeader = teamRepository.findMyTeamDetailsAsLeader(user.getUserId(), - teamId); - return myTeamDetailsAsLeader; + return teamRepository.findMyTeamDetailsAsLeader(user.getUserId(), + teamId); } public List getTeamMembers(String userId, Long teamId) { diff --git a/src/main/java/com/jangburich/domain/team/domain/Team.java b/src/main/java/com/jangburich/domain/team/domain/Team.java index 79ef3ca..bd79f51 100644 --- a/src/main/java/com/jangburich/domain/team/domain/Team.java +++ b/src/main/java/com/jangburich/domain/team/domain/Team.java @@ -1,16 +1,18 @@ package com.jangburich.domain.team.domain; -import jakarta.persistence.Embedded; -import jakarta.persistence.EnumType; -import jakarta.persistence.Enumerated; +import java.util.UUID; import com.jangburich.domain.common.BaseEntity; import jakarta.persistence.Column; +import jakarta.persistence.Embedded; import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.PrePersist; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; @@ -21,33 +23,33 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) public class Team extends BaseEntity { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id", updatable = false) - private Long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", updatable = false) + private Long id; - @Column(name = "name") - private String name; + @Column(name = "name") + private String name; - @Column(name = "description") - private String description; + @Column(name = "description") + private String description; - @Column(name = "secret_code") - private String secretCode; + @Column(name = "secret_code", updatable = false, unique = true) + private String secretCode; - @Embedded - private TeamLeader teamLeader; + @Embedded + private TeamLeader teamLeader; - @Column(name = "point") - private Integer point; + @Column(name = "point") + private Integer point; - @Enumerated(EnumType.STRING) - @Column(name = "team_type") - private TeamType teamType; + @Enumerated(EnumType.STRING) + @Column(name = "team_type") + private TeamType teamType; - public void updatePoint(Integer point) { - this.point += point; - } + public void updatePoint(Integer point) { + this.point += point; + } public void validateJoinCode(String joinCode) { if (!this.secretCode.equals(joinCode)) { @@ -55,21 +57,26 @@ public void validateJoinCode(String joinCode) { } } - - @Builder - public Team(String name, String description, String secretCode, TeamLeader teamLeader, Integer point, - TeamType teamType) { - this.name = name; - this.description = description; - this.secretCode = secretCode; - this.teamLeader = teamLeader; - this.point = point; - this.teamType = teamType; - } - - public void validateIsTeamLeader(Long userId, Long userId1) { - if (!userId.equals(userId1)) { - throw new IllegalArgumentException("팀의 리더가 아닌 사람은 선결제를 할 수 없습니다."); - } - } + @PrePersist + private void generateSecretCode() { + if (this.secretCode == null) { + this.secretCode = UUID.randomUUID().toString().replace("-", "").substring(0, 8); + } + } + + @Builder + public Team(String name, String description, TeamLeader teamLeader, Integer point, + TeamType teamType) { + this.name = name; + this.description = description; + this.teamLeader = teamLeader; + this.point = point; + this.teamType = teamType; + } + + public void validateIsTeamLeader(Long userId, Long userId1) { + if (!userId.equals(userId1)) { + throw new IllegalArgumentException("팀의 리더가 아닌 사람은 선결제를 할 수 없습니다."); + } + } } diff --git a/src/main/java/com/jangburich/domain/team/domain/repository/TeamQueryDslRepositoryImpl.java b/src/main/java/com/jangburich/domain/team/domain/repository/TeamQueryDslRepositoryImpl.java index 9cbfcdd..d45b2dd 100644 --- a/src/main/java/com/jangburich/domain/team/domain/repository/TeamQueryDslRepositoryImpl.java +++ b/src/main/java/com/jangburich/domain/team/domain/repository/TeamQueryDslRepositoryImpl.java @@ -77,27 +77,35 @@ public MyTeamDetailsResponse findMyTeamDetailsAsMember(Long userId, Long teamId) .fetch(); return queryFactory - .selectDistinct(new QMyTeamDetailsResponse( - storeTeam.team.id, - storeTeam.team.name, - storeTeam.team.description, - Expressions.constant(-1), - storeTeam.remainPoint, - storeTeam.personalAllocatedPoint, - pointTransaction.transactionedPoint.sum(), - Expressions.constant(prepayedStores), - Expressions.constant(images), - Expressions.constant(images.size()), - Expressions.constant(todayPayments), - Expressions.constant(todayPayments.size()) - )) - .from(storeTeam) - .leftJoin(team).on(storeTeam.team.id.eq(teamId)) - .leftJoin(userTeam).on(userTeam.team.id.eq(storeTeam.team.id)) - .leftJoin(pointTransaction).on(pointTransaction.transactionType.eq(TransactionType.FOOD_PURCHASE), - pointTransaction.user.userId.eq(userId)) - .where(storeTeam.team.id.eq(teamId)) - .fetchOne(); + .selectDistinct(new QMyTeamDetailsResponse( + storeTeam.team.id, + storeTeam.team.name, + storeTeam.team.description, + Expressions.constant(-1), + storeTeam.remainPoint, + storeTeam.personalAllocatedPoint, + pointTransaction.transactionedPoint.sum(), + Expressions.constant(prepayedStores), + Expressions.constant(images), + Expressions.constant(images.size()), + Expressions.constant(todayPayments), + Expressions.constant(todayPayments.size()) + )) + .from(storeTeam) + .leftJoin(team).on(storeTeam.team.id.eq(teamId)) + .leftJoin(userTeam).on(userTeam.team.id.eq(storeTeam.team.id)) + .leftJoin(pointTransaction).on(pointTransaction.transactionType.eq(TransactionType.FOOD_PURCHASE), + pointTransaction.user.userId.eq(userId)) + .where(storeTeam.team.id.eq(teamId)) + .groupBy( + storeTeam.team.id, + storeTeam.team.name, + storeTeam.team.description, + storeTeam.remainPoint, + storeTeam.personalAllocatedPoint + ) + .fetchOne(); + } @Override @@ -139,26 +147,34 @@ public MyTeamDetailsResponse findMyTeamDetailsAsLeader(Long userId, Long teamId) return queryFactory - .selectDistinct(new QMyTeamDetailsResponse( - storeTeam.team.id, - storeTeam.team.name, - storeTeam.team.description, - storeTeam.point, - storeTeam.remainPoint, - Expressions.constant(-1), - pointTransaction.transactionedPoint.sum(), - Expressions.constant(prepayedStores), - Expressions.constant(images), - Expressions.constant(images.size()), - Expressions.constant(todayPayments), - Expressions.constant(todayPayments.size()) - )) - .from(storeTeam) - .leftJoin(team).on(storeTeam.team.id.eq(teamId)) - .leftJoin(userTeam).on(userTeam.team.id.eq(storeTeam.team.id)) - .leftJoin(pointTransaction).on(pointTransaction.transactionType.eq(TransactionType.FOOD_PURCHASE), - pointTransaction.user.userId.eq(userId)) - .where(storeTeam.team.id.eq(teamId)) - .fetchOne(); + .selectDistinct(new QMyTeamDetailsResponse( + storeTeam.team.id, + storeTeam.team.name, + storeTeam.team.description, + storeTeam.point, + storeTeam.remainPoint, + Expressions.constant(-1), + pointTransaction.transactionedPoint.sum(), + Expressions.constant(prepayedStores), + Expressions.constant(images), + Expressions.constant(images.size()), + Expressions.constant(todayPayments), + Expressions.constant(todayPayments.size()) + )) + .from(storeTeam) + .leftJoin(team).on(storeTeam.team.id.eq(teamId)) + .leftJoin(userTeam).on(userTeam.team.id.eq(storeTeam.team.id)) + .leftJoin(pointTransaction).on(pointTransaction.transactionType.eq(TransactionType.FOOD_PURCHASE), + pointTransaction.user.userId.eq(userId)) + .where(storeTeam.team.id.eq(teamId)) + .groupBy( + storeTeam.team.id, + storeTeam.team.name, + storeTeam.team.description, + storeTeam.point, + storeTeam.remainPoint + ) + .fetchOne(); + } } diff --git a/src/main/java/com/jangburich/domain/team/domain/repository/TeamRepository.java b/src/main/java/com/jangburich/domain/team/domain/repository/TeamRepository.java index ecfcb55..ce11e4d 100644 --- a/src/main/java/com/jangburich/domain/team/domain/repository/TeamRepository.java +++ b/src/main/java/com/jangburich/domain/team/domain/repository/TeamRepository.java @@ -5,12 +5,15 @@ import com.jangburich.domain.user.domain.User; import java.util.List; import java.util.Optional; +import java.util.UUID; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; public interface TeamRepository extends JpaRepository, TeamQueryDslRepository { - Optional findBySecretCode(String joinCode); + Optional findBySecretCode(String secretCode); + @Query("SELECT t FROM Team t JOIN UserTeam ut ON ut.team = t WHERE ut.user = :user AND t.status = :status") Optional> findAllByUserAndStatus(@Param("user") User user, @Param("status") Status status); diff --git a/src/main/java/com/jangburich/domain/team/dto/request/RegisterTeamRequest.java b/src/main/java/com/jangburich/domain/team/dto/request/RegisterTeamRequest.java index a0f9eda..e034616 100644 --- a/src/main/java/com/jangburich/domain/team/dto/request/RegisterTeamRequest.java +++ b/src/main/java/com/jangburich/domain/team/dto/request/RegisterTeamRequest.java @@ -4,7 +4,6 @@ public record RegisterTeamRequest( String teamType, String teamName, String description, - String secretCode, String teamLeaderAccountNumber, String bankName, int memberLimit diff --git a/src/main/java/com/jangburich/domain/team/dto/response/TeamSecretCodeResponse.java b/src/main/java/com/jangburich/domain/team/dto/response/TeamSecretCodeResponse.java new file mode 100644 index 0000000..c7b0e7e --- /dev/null +++ b/src/main/java/com/jangburich/domain/team/dto/response/TeamSecretCodeResponse.java @@ -0,0 +1,14 @@ +package com.jangburich.domain.team.dto.response; + +import java.util.UUID; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@RequiredArgsConstructor +public class TeamSecretCodeResponse { + private String uuid; +} diff --git a/src/main/java/com/jangburich/domain/team/presentation/TeamController.java b/src/main/java/com/jangburich/domain/team/presentation/TeamController.java index 794f7fa..c852d50 100644 --- a/src/main/java/com/jangburich/domain/team/presentation/TeamController.java +++ b/src/main/java/com/jangburich/domain/team/presentation/TeamController.java @@ -1,6 +1,7 @@ package com.jangburich.domain.team.presentation; import java.util.List; +import java.util.UUID; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.GetMapping; @@ -17,6 +18,7 @@ import com.jangburich.domain.team.dto.response.MyTeamResponse; import com.jangburich.domain.team.dto.response.TeamCodeResponse; import com.jangburich.domain.team.dto.response.TeamMemberResponse; +import com.jangburich.domain.team.dto.response.TeamSecretCodeResponse; import com.jangburich.global.payload.Message; import com.jangburich.global.payload.ResponseCustom; import com.jangburich.utils.parser.AuthenticationParser; @@ -35,7 +37,7 @@ public class TeamController { @Operation(summary = "팀 생성", description = "팀을 생성한다. 팀 리더는 생성자") @PostMapping - public ResponseCustom registerTeam( + public ResponseCustom registerTeam( Authentication authentication, @RequestBody RegisterTeamRequest registerTeamRequest ) {