-
Notifications
You must be signed in to change notification settings - Fork 6
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
참여자 닉네임 생성, 모임의 참여자 조회 기능 생성 #96
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e82cd1e
feat: Member 엔티티 생성
hoyeonyy 84d8ec7
merge: 최신 변경 사항 반영
hoyeonyy 6b6c535
feat: MemberRepository 생성
hoyeonyy 85c400e
refactor: 맴버 닉네임 추가
hoyeonyy 7911373
test: 테스트 케이스 추가
hoyeonyy 2ad4b4d
refactor: currentPeople 필드 제거
hoyeonyy 1f8655d
refactor: 카멜케이스 적용
hoyeonyy 60666b2
refactor: String을 반환하지 않고 객체 반환
hoyeonyy 3984e66
refactor: delete 메서드 수정
hoyeonyy 122178b
style: 카멜 케이스 수정
hoyeonyy 2968d2a
refactor: 메서드 명 수정
hoyeonyy 1e54ed8
refactor: 주석 코드 제거
hoyeonyy 6de6293
merge: 최신 변경 사항 반영
hoyeonyy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
backend/src/main/java/mouda/backend/member/domain/Member.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package mouda.backend.member.domain; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.ManyToOne; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import mouda.backend.moim.domain.Moim; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor | ||
public class Member { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String nickname; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
private Moim moim; | ||
|
||
@Builder | ||
public Member(String nickname) { | ||
this.nickname = nickname; | ||
} | ||
|
||
public void joinMoim(Moim moim) { | ||
this.moim = moim; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/mouda/backend/member/repository/MemberRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package mouda.backend.member.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import mouda.backend.member.domain.Member; | ||
|
||
public interface MemberRepository extends JpaRepository<Member, Long> { | ||
|
||
List<Member> findAllByMoimId(long moimId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
backend/src/main/java/mouda/backend/moim/dto/request/MoimJoinRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package mouda.backend.moim.dto.request; | ||
|
||
public record MoimJoinRequest( | ||
Long moimId | ||
Long moimId, | ||
String nickname | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import java.time.LocalDate; | ||
import java.time.LocalTime; | ||
import java.util.List; | ||
|
||
import lombok.Builder; | ||
import mouda.backend.moim.domain.Moim; | ||
|
@@ -15,19 +16,21 @@ public record MoimDetailsFindResponse( | |
int currentPeople, | ||
int maxPeople, | ||
String authorNickname, | ||
String description | ||
String description, | ||
List<String> participants | ||
) { | ||
|
||
public static MoimDetailsFindResponse toResponse(Moim moim) { | ||
public static MoimDetailsFindResponse toResponse(Moim moim, List<String> participants) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 모임 객체에 |
||
return MoimDetailsFindResponse.builder() | ||
.title(moim.getTitle()) | ||
.date(moim.getDate()) | ||
.time(moim.getTime()) | ||
.place(moim.getPlace()) | ||
.currentPeople(moim.getCurrentPeople()) | ||
.currentPeople(participants.size()) | ||
.maxPeople(moim.getMaxPeople()) | ||
.authorNickname(moim.getAuthorNickname()) | ||
.description(moim.getDescription()) | ||
.participants(participants) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import mouda.backend.member.domain.Member; | ||
import mouda.backend.member.repository.MemberRepository; | ||
import mouda.backend.moim.domain.Moim; | ||
import mouda.backend.moim.dto.request.MoimCreateRequest; | ||
import mouda.backend.moim.dto.request.MoimJoinRequest; | ||
|
@@ -24,16 +26,26 @@ public class MoimService { | |
|
||
private final MoimRepository moimRepository; | ||
|
||
private final MemberRepository memberRepository; | ||
|
||
public Moim createMoim(MoimCreateRequest moimCreateRequest) { | ||
return moimRepository.save(moimCreateRequest.toEntity()); | ||
Member author = new Member(moimCreateRequest.authorNickname()); | ||
Moim moim = moimRepository.save(moimCreateRequest.toEntity()); | ||
author.joinMoim(moim); | ||
memberRepository.save(author); | ||
|
||
return moim; | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public MoimFindAllResponses findAllMoim() { | ||
List<Moim> moims = moimRepository.findAll(); | ||
return new MoimFindAllResponses( | ||
moims.stream() | ||
.map(MoimFindAllResponse::toResponse) | ||
.map(moim -> { | ||
List<Member> participants = memberRepository.findAllByMoimId(moim.getId()); | ||
return MoimFindAllResponse.toResponse(moim, participants.size()); | ||
Comment on lines
+45
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 양방향이면 확실히 이런 부분에서 코드가 개선되긴 하겠네요 😄 |
||
}) | ||
.toList() | ||
); | ||
} | ||
|
@@ -43,18 +55,32 @@ public MoimDetailsFindResponse findMoimDetails(long id) { | |
Moim moim = moimRepository.findById(id) | ||
.orElseThrow(() -> new MoimException(HttpStatus.NOT_FOUND, MoimErrorMessage.NOT_FOUND)); | ||
|
||
return MoimDetailsFindResponse.toResponse(moim); | ||
List<String> participants = memberRepository.findAllByMoimId(id).stream() | ||
.map(Member::getNickname) | ||
.toList(); | ||
|
||
return MoimDetailsFindResponse.toResponse(moim, participants); | ||
} | ||
|
||
public void joinMoim(MoimJoinRequest moimJoinRequest) { | ||
Member member = new Member(moimJoinRequest.nickname()); | ||
Moim moim = moimRepository.findById(moimJoinRequest.moimId()) | ||
.orElseThrow(() -> new MoimException(HttpStatus.NOT_FOUND, MoimErrorMessage.NOT_FOUND)); | ||
moim.join(); | ||
|
||
member.joinMoim(moim); | ||
memberRepository.save(member); | ||
List<Member> participants = memberRepository.findAllByMoimId(moim.getId()); | ||
|
||
moim.validateAlreadyFullMoim(participants.size()); | ||
} | ||
|
||
public void deleteMoim(long id) { | ||
Moim moim = moimRepository.findById(id) | ||
.orElseThrow(() -> new MoimException(HttpStatus.NOT_FOUND, MoimErrorMessage.NOT_FOUND)); | ||
List<Member> participants = memberRepository.findAllByMoimId(moim.getId()); | ||
for (Member participant : participants) { | ||
memberRepository.deleteById(participant.getId()); | ||
} | ||
|
||
moimRepository.delete(moim); | ||
} | ||
|
37 changes: 20 additions & 17 deletions
37
backend/src/test/java/mouda/backend/config/DatabaseCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
package mouda.backend.config; | ||
|
||
import java.util.List; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import jakarta.persistence.EntityManager; | ||
|
||
@Component | ||
public class DatabaseCleaner { | ||
|
||
@Autowired | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
public DatabaseCleaner(final JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public void cleanUp() { | ||
String sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'PUBLIC' AND table_type='BASE TABLE'"; | ||
List<String> tables = jdbcTemplate.queryForList(sql, String.class); | ||
for (String tableName : tables) { | ||
jdbcTemplate.update("DELETE FROM " + tableName); | ||
jdbcTemplate.update("ALTER TABLE " + tableName + " alter column id restart with 1"); | ||
} | ||
} | ||
@Autowired | ||
private EntityManager entityManager; | ||
|
||
public DatabaseCleaner(EntityManager entityManager) { | ||
this.entityManager = entityManager; | ||
} | ||
|
||
@Transactional | ||
public void cleanUp() { | ||
entityManager.createNativeQuery("DELETE FROM MEMBER").executeUpdate(); | ||
entityManager.createNativeQuery("ALTER TABLE MEMBER alter column id restart with 1").executeUpdate(); | ||
|
||
entityManager.createNativeQuery("DELETE FROM MOIM").executeUpdate(); | ||
entityManager.createNativeQuery("ALTER TABLE MOIM alter column id restart with 1").executeUpdate(); | ||
|
||
entityManager.clear(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
backend/src/test/java/mouda/backend/member/repository/MemberRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package mouda.backend.member.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import mouda.backend.config.DatabaseCleaner; | ||
import mouda.backend.member.domain.Member; | ||
import mouda.backend.moim.domain.Moim; | ||
import mouda.backend.moim.repository.MoimRepository; | ||
|
||
@SpringBootTest | ||
class MemberRepositoryTest { | ||
|
||
@Autowired | ||
private MemberRepository memberRepository; | ||
|
||
@Autowired | ||
private MoimRepository moimRepository; | ||
|
||
@Autowired | ||
private DatabaseCleaner databaseCleaner; | ||
|
||
@AfterEach | ||
void cleanUp() { | ||
databaseCleaner.cleanUp(); | ||
} | ||
|
||
@DisplayName("모임에 가입된 참여자의 수를 반환한다.") | ||
@Test | ||
void findNickNamesByMoimId() { | ||
Member member = new Member("tehah"); | ||
Moim moim = Moim.builder() | ||
.build(); | ||
Moim saveMoim = moimRepository.save(moim); | ||
member.joinMoim(saveMoim); | ||
memberRepository.save(member); | ||
|
||
List<Member> participants = memberRepository.findAllByMoimId(saveMoim.getId()); | ||
|
||
Assertions.assertThat(participants.size()).isEqualTo(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
한 명의 사람이 하나의 모임에만 참여할 수 있는 구조인데,
수정할 필요가 있지 않을까요?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아, 현재 로그인 기능이 구현되어있지 않아서
테니가 여러 개의 모임에 참여하여도 테니가 여러 명 생성되는 식으로 임시 개발한 거군요.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
지금 멤버가 계속 생성되는 상황에서
@OneToOne
이 아니라@ManyToOne
을 사용한 이유가 후에 회원정보를 저장(로그인) 기능을 염두에 두었기 때문이 맞을까용?아니네요 핫핫