Skip to content

Commit

Permalink
feat: 팀 생성 기능 구현 (#4)
Browse files Browse the repository at this point in the history
* feat: Team 도메인 수정 및 일급 컬렉션화

* feat: 팀 생성 기능 구현
  • Loading branch information
LEEJaeHyeok97 authored Nov 18, 2024
1 parent ea68269 commit 7fb2161
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public String getType() {

@Transactional
@Override
public ReadyResponse payReady(Long userId, PayRequest payRequest) {
public ReadyResponse payReady(String userId, PayRequest payRequest) {
Map<String, String> parameters = new HashMap<>();

parameters.put("cid", "TC0ONETIME"); // 가맹점 코드(테스트용)
Expand Down Expand Up @@ -85,7 +85,7 @@ public ReadyResponse payReady(Long userId, PayRequest payRequest) {

@Transactional
@Override
public ApproveResponse payApprove(Long userId, String tid, String pgToken) {
public ApproveResponse payApprove(String userId, String tid, String pgToken) {
Map<String, String> parameters = new HashMap<>();
parameters.put("cid", "TC0ONETIME"); // 가맹점 코드(테스트용)
parameters.put("tid", tid); // 결제 고유번호
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public class PaymentProcessingService {

private final PaymentServiceStrategy paymentServiceStrategy;

public ReadyResponse processPayment(Long userId, PayRequest payRequest) {
public ReadyResponse processPayment(String userId, PayRequest payRequest) {
PaymentService paymentService = paymentServiceStrategy.getPaymentService(payRequest.paymentType());
return paymentService.payReady(userId, payRequest);
}

public ApproveResponse processSuccess(Long userId, String tid, String pgToken) {
public ApproveResponse processSuccess(String userId, String tid, String pgToken) {
PaymentService paymentService = paymentServiceStrategy.getPaymentService("kakao");
return paymentService.payApprove(userId, tid, pgToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

public interface PaymentService {
String getType();
ReadyResponse payReady(Long userId, PayRequest payRequest);
ApproveResponse payApprove(Long userId, String tid, String pgToken);
ReadyResponse payReady(String userId, PayRequest payRequest);
ApproveResponse payApprove(String userId, String tid, String pgToken);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package com.jangburich.domain.team.application;

import com.jangburich.domain.team.domain.Team;
import com.jangburich.domain.team.domain.TeamLeader;
import com.jangburich.domain.team.domain.TeamType;
import com.jangburich.domain.team.domain.UserTeam;
import com.jangburich.domain.team.domain.repository.TeamRepository;
import com.jangburich.domain.team.domain.repository.UserTeamRepository;
import com.jangburich.domain.team.dto.request.RegisterTeamRequest;
import com.jangburich.domain.user.domain.User;
import com.jangburich.domain.user.domain.repository.UserRepository;
import com.jangburich.global.payload.Message;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -9,5 +19,35 @@
@RequiredArgsConstructor
public class TeamService {

private static final int ZERO = 0;

private final TeamRepository teamRepository;
private final UserRepository userRepository;
private final UserTeamRepository userTeamRepository;

@Transactional
public Message registerTeam(String userId, RegisterTeamRequest registerTeamRequest) {
User user = userRepository.findByProviderId(userId)
.orElseThrow(() -> new NullPointerException());

Team team = Team.builder()
.name(registerTeamRequest.teamName())
.description(registerTeamRequest.description())
.teamLeader(new TeamLeader(user.getUserId(), registerTeamRequest.teamLeaderAccountNumber(),
registerTeamRequest.bankName()))
.point(ZERO)
.memberLimit(registerTeamRequest.memberLimit())
.teamType(TeamType.valueOf(registerTeamRequest.teamType()))
.build();

System.out.println("222");
teamRepository.save(team);

UserTeam userTeam = UserTeam.of(user, team);
userTeamRepository.save(userTeam);

return Message.builder()
.message("팀 생성이 완료되었습니다.")
.build();
}
}
21 changes: 14 additions & 7 deletions src/main/java/com/jangburich/domain/team/domain/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
import jakarta.persistence.Embedded;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import java.util.HashSet;
import java.util.Set;

import com.jangburich.domain.common.BaseEntity;
import com.jangburich.domain.user.domain.User;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand Down Expand Up @@ -51,10 +48,20 @@ public class Team extends BaseEntity {
@Column(name = "team_type")
private TeamType teamType;

@ManyToMany(mappedBy = "teams")
private Set<User> users = new HashSet<>();

public void updatePoint(Integer point) {
this.point += point;
}


@Builder
public Team(String name, String description, String secretCode, TeamLeader teamLeader, Integer point,
Integer memberLimit, TeamType teamType) {
this.name = name;
this.description = description;
this.secretCode = secretCode;
this.teamLeader = teamLeader;
this.point = point;
this.memberLimit = memberLimit;
this.teamType = teamType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
public class TeamLeader {
private Long user_id;
private String accountNumber;
private String bankName;

public TeamLeader(Long user_id, String accountNumber) {
public TeamLeader(Long user_id, String accountNumber, String bankName) {
this.user_id = user_id;
this.accountNumber = accountNumber;
this.bankName = bankName;
}

public boolean isSameLeader(Long userId) {
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/jangburich/domain/team/domain/UserTeam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.jangburich.domain.team.domain;

import com.jangburich.domain.common.BaseEntity;
import com.jangburich.domain.user.domain.User;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class UserTeam extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "team_id")
private Team team;

public UserTeam(User user, Team team) {
this.user = user;
this.team = team;
}

public static UserTeam of(User user, Team team) {
if (user == null || team == null) {
throw new IllegalArgumentException("유저와 팀은 null이 될 수 없습니다.");
}
return new UserTeam(user, team);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.jangburich.domain.team.domain.repository;

import com.jangburich.domain.team.domain.UserTeam;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserTeamRepository extends JpaRepository<UserTeam, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
package com.jangburich.domain.team.dto.request;

public record RegisterTeamRequest() {
public record RegisterTeamRequest(
String teamType,
String teamName,
String description,
String secretCode,
String teamLeaderAccountNumber,
String bankName,
int memberLimit
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.jangburich.domain.team.dto.request.RegisterTeamRequest;
import com.jangburich.global.payload.Message;
import com.jangburich.global.payload.ResponseCustom;
import com.jangburich.utils.parser.AuthenticationParser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
Expand All @@ -20,11 +22,12 @@ public class TeamController {

private final TeamService teamService;

@Operation(summary = "팀 생성", description = "팀을 생성한다. 팀 리더는 생성자")
@PostMapping
public ResponseCustom<Message> registerTeam(
Authentication authentication,
@RequestBody RegisterTeamRequest registerTeamRequest
) {
return ResponseCustom.OK();
return ResponseCustom.OK(teamService.registerTeam(AuthenticationParser.parseUserId(authentication), registerTeamRequest));
}
}
8 changes: 0 additions & 8 deletions src/main/java/com/jangburich/domain/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ public class User extends BaseEntity {
private String role;


@ManyToMany
@JoinTable(
name = "user_team",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "team_id")
)
private Set<Team> teams = new HashSet<>();

public static User create(String userId, String nickname, String image, String role) {
User newUser = new User();
newUser.setProviderId(userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
public class AuthenticationParser {
private AuthenticationParser(){}

public static Long parseUserId(Authentication authentication) {
public static String parseUserId(Authentication authentication) {
CustomOAuthUser principal = (CustomOAuthUser) authentication.getPrincipal();
return Long.parseLong(principal.getUserId());
return principal.getUserId();
}
}

0 comments on commit 7fb2161

Please sign in to comment.