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

feature: PR #11 코드 리뷰 반영 #29

Merged
merged 9 commits into from
Nov 6, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.hyunsolution.dangu.participant.domain;

import com.hyunsolution.dangu.user.domain.User;
import com.hyunsolution.dangu.workspace.domain.Workspace;
import javax.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Participant {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(
name = "user_id",
nullable = false,
foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(
name = "workspace_id",
nullable = false,
foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Workspace workspace;

@Column(name = "participant_match", nullable = false)
@ColumnDefault("false")
private boolean participantMatch;

@Builder
public Participant(User user, Workspace workspace, boolean participantMatch) {
this.user = user;
this.workspace = workspace;
this.participantMatch = participantMatch;
}

public void accept() {
this.participantMatch = true;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.hyunsolution.dangu.participant.domain;

import com.hyunsolution.dangu.participant.Participant;
import com.hyunsolution.dangu.user.domain.User;
import java.util.List;
import java.util.Optional;
Expand All @@ -12,13 +11,11 @@ public interface ParticipantRepository extends JpaRepository<User, Long> {

// 같은 방에 있는 사람들 id(PK) 리스트
@Query(value = "SELECT p.id FROM Participant p WHERE p.workspace.id=:workspaceId")
List<Long> findByVisitorMatch(Long workspaceId);
List<Long> findParticipantIdByWorkspaceId(Long workspaceId);

// ID별 매칭버튼 클릭 여부
@Query(
value =
"SELECT COUNT(p) >0 FROM Participant p WHERE p.id=:id AND p.participantMatch=true")
Boolean existsById(long id);
@Query(value = "SELECT COUNT(p) >0 FROM Participant p WHERE p.id=:id AND p.participantMatch=true")
Boolean isClickedById(long id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boolean existsByIdAndAndParticipantMatchTrue(long id);
위처럼 JPQL을 사용하지 않고 Data JPA를 사용 할 수도 있을 것 같아요

그리고 isClicekd는 너무 추상적인 단어 인 것 같아요.


// 개인 ID 찾기
@Query(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.hyunsolution.dangu.participant.service;

import com.hyunsolution.dangu.participant.Participant;
import com.hyunsolution.dangu.participant.domain.Participant;
import com.hyunsolution.dangu.participant.domain.ParticipantRepository;
import com.hyunsolution.dangu.user.domain.UserRepository;
import com.hyunsolution.dangu.workspace.domain.Workspace;
import com.hyunsolution.dangu.workspace.domain.WorkspaceRepository;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -21,25 +22,24 @@ public class ParticipantService {
@Transactional
public void changeMatching(Long id, Long workspaceId) {
// 상태 변경
Participant participantOptional =
participantRepository.findByUserIdAndWorkspaceId(id, workspaceId)
.orElseThrow(()-> new NoSuchElementException("Participant not found"));

Optional<Participant> participantOptional =
participantRepository.findByUserIdAndWorkspaceId(id, workspaceId);
participantOptional.get().accept();
// participantRepository.updateByIdAndWorkspace(id, workspaceId);
participantOptional.accept();

// participant테이블에서 roomNumber로 들어온 숫자를 통해 누가 있는지 파악
List<Long> participants = participantRepository.findByVisitorMatch(workspaceId);
List<Long> participantIds = participantRepository.findParticipantIdByWorkspaceId(workspaceId);

for (Long participant : participants) {
boolean mathingCheck = participantRepository.existsById(participant);
// 방안에 모든 참가자가 "확정"버튼을 눌렀는지 확인
for (Long participant : participantIds) {
boolean mathingCheck = participantRepository.isClickedById(participant);
if (!mathingCheck) {
return;
}
}
// 방안에 모든 참가자가 "확정"버튼을 눌렀는지 확인하고 게임방 테이블 속 매칭 결과를 true로 바꿈
Workspace workspace1 = workspaceRepository.findById(workspaceId).get();
workspace1.finalAccept();
// workspaceRepository.UpdateWorkspaceStatus(workspaceId);

// 게임방 테이블 속 매칭 결과를 true로 바꿈
Workspace workspace1 = workspaceRepository.findById(workspaceId).orElseThrow(()-> new NoSuchElementException("Workspace not found"));
workspace1.acceptFinal();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.hyunsolution.dangu.workspace.domain;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface WorkspaceRepository extends JpaRepository<Workspace, Long> {

@Modifying
@Query(value = "UPDATE Workspace w SET w.isMatched=true WHERE w.id=:workspaceId")
void UpdateWorkspaceStatus(Long workspaceId);
}
Loading