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

fix : team 생성 로직 수정 #181

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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());

Expand All @@ -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
Expand Down Expand Up @@ -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<TeamMemberResponse> getTeamMembers(String userId, Long teamId) {
Expand Down
87 changes: 47 additions & 40 deletions src/main/java/com/jangburich/domain/team/domain/Team.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,55 +23,60 @@
@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)) {
throw new IllegalArgumentException("유효하지 않은 입장 코드입니다.");
}
}


@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("팀의 리더가 아닌 사람은 선결제를 할 수 없습니다.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Team, Long>, TeamQueryDslRepository {
Optional<Team> findBySecretCode(String joinCode);
Optional<Team> 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<List<Team>> findAllByUserAndStatus(@Param("user") User user, @Param("status") Status status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ public record RegisterTeamRequest(
String teamType,
String teamName,
String description,
String secretCode,
String teamLeaderAccountNumber,
String bankName,
int memberLimit
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -35,7 +37,7 @@ public class TeamController {

@Operation(summary = "팀 생성", description = "팀을 생성한다. 팀 리더는 생성자")
@PostMapping
public ResponseCustom<Message> registerTeam(
public ResponseCustom<TeamSecretCodeResponse> registerTeam(
Authentication authentication,
@RequestBody RegisterTeamRequest registerTeamRequest
) {
Expand Down