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

참여자 닉네임 생성, 모임의 참여자 조회 기능 생성 #96

Merged
merged 13 commits into from
Jul 24, 2024
36 changes: 36 additions & 0 deletions backend/src/main/java/mouda/backend/member/domain/Member.java
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;
Comment on lines +25 to +26
Copy link
Contributor

Choose a reason for hiding this comment

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

한 명의 사람이 하나의 모임에만 참여할 수 있는 구조인데,
수정할 필요가 있지 않을까요?!

Copy link
Contributor

Choose a reason for hiding this comment

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

아, 현재 로그인 기능이 구현되어있지 않아서
테니가 여러 개의 모임에 참여하여도 테니가 여러 명 생성되는 식으로 임시 개발한 거군요.

Copy link
Contributor

@ay-eonii ay-eonii Jul 23, 2024

Choose a reason for hiding this comment

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

지금 멤버가 계속 생성되는 상황에서 @OneToOne이 아니라 @ManyToOne을 사용한 이유가 후에 회원정보를 저장(로그인) 기능을 염두에 두었기 때문이 맞을까용?

아니네요 핫핫


@Builder
public Member(String nickname) {
this.nickname = nickname;
}

public void joinMoim(Moim moim) {
this.moim = moim;
}
}
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);
}
6 changes: 1 addition & 5 deletions backend/src/main/java/mouda/backend/moim/domain/Moim.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public class Moim {

private String place;

private int currentPeople;

private int maxPeople;

private String authorNickname;
Expand All @@ -54,16 +52,14 @@ public Moim(
this.date = date;
this.time = time;
this.place = place;
this.currentPeople = 1;
this.maxPeople = maxPeople;
this.authorNickname = authorNickname;
this.description = description;
}

public void join() {
public void validateAlreadyFullMoim(int currentPeople) {
if (currentPeople + 1 > maxPeople) {
throw new MoimException(HttpStatus.BAD_REQUEST, MoimErrorMessage.MAX_PEOPLE);
}
currentPeople++;
}
}
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
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

모임 객체에 Participants가 있는 것이 아니라 따로 받아오도록 설계한 이유가 있을까요?

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public record MoimFindAllResponse(
String description
) {

public static MoimFindAllResponse toResponse(Moim moim) {
public static MoimFindAllResponse toResponse(Moim moim, int currentPeople) {
return MoimFindAllResponse.builder()
.moimId(moim.getId())
.title(moim.getTitle())
.date(moim.getDate())
.time(moim.getTime())
.place(moim.getPlace())
.currentPeople(moim.getCurrentPeople())
.currentPeople(currentPeople)
.maxPeople(moim.getMaxPeople())
.authorNickname(moim.getAuthorNickname())
.description(moim.getDescription())
Expand Down
34 changes: 30 additions & 4 deletions backend/src/main/java/mouda/backend/moim/service/MoimService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

양방향이면 확실히 이런 부분에서 코드가 개선되긴 하겠네요 😄
나중에 수정해봅시다 💪

})
.toList()
);
}
Expand All @@ -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);
}
Expand Down
37 changes: 20 additions & 17 deletions backend/src/test/java/mouda/backend/config/DatabaseCleaner.java
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();
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.springframework.boot.test.context.SpringBootTest;

import mouda.backend.config.DatabaseCleaner;
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;
Expand All @@ -30,6 +32,9 @@ class MoimServiceTest {
@Autowired
private MoimRepository moimRepository;

@Autowired
private MemberRepository memberRepository;

@Autowired
private DatabaseCleaner databaseCleaner;

Expand Down Expand Up @@ -87,14 +92,15 @@ void joinMoim() {
"title", LocalDate.now(), LocalTime.now(), "place",
10, "안나", "설명"
);
moimService.createMoim(moimCreateRequest);
Moim moim = moimService.createMoim(moimCreateRequest);

MoimJoinRequest moimJoinRequest = new MoimJoinRequest(1L);
MoimJoinRequest moimJoinRequest = new MoimJoinRequest(moim.getId(), "호기");
moimService.joinMoim(moimJoinRequest);
List<Member> participants = memberRepository.findAllByMoimId(moim.getId());

Optional<Moim> moimOptional = moimRepository.findById(1L);
Optional<Moim> moimOptional = moimRepository.findById(moim.getId());
assertThat(moimOptional).isNotEmpty();
assertThat(moimOptional.get().getCurrentPeople()).isEqualTo(2);
assertThat(participants.size()).isEqualTo(2);
}

@DisplayName("모임을 삭제한다.")
Expand Down
Loading