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

모임 상세 조회 기능 구현 #60

Merged
merged 5 commits into from
Jul 17, 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 @@ -16,6 +16,7 @@
import mouda.backend.common.RestResponse;
import mouda.backend.moim.domain.Moim;
import mouda.backend.moim.dto.request.MoimCreateRequest;
import mouda.backend.moim.dto.response.MoimDetailsFindResponse;
import mouda.backend.moim.dto.response.MoimFindAllResponses;
import mouda.backend.moim.service.MoimService;

Expand Down Expand Up @@ -45,12 +46,21 @@ public ResponseEntity<RestResponse<MoimFindAllResponses>> findAllMoim() {
return ResponseEntity.ok().body(new RestResponse<>(moimService.findAllMoim()));
}

@Operation(summary = "모임 상세 조회", description = "모임 상세 조회한다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "모임 상세 조회 성공!"),
})
@GetMapping("/{moimId}")
public ResponseEntity<RestResponse<MoimDetailsFindResponse>> findMoimDetails(@PathVariable long moimId) {
return ResponseEntity.ok().body(new RestResponse<>(moimService.findMoimDetails(moimId)));
}

@Operation(summary = "모임 삭제", description = "해당하는 id의 모임을 삭제한다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "모임 삭제 성공!"),
})
@DeleteMapping("/{id}")
public void deleteMoim(@PathVariable long id) {
moimService.deleteMoim(id);
@DeleteMapping("/{moimId}")
public void deleteMoim(@PathVariable long moimId) {
moimService.deleteMoim(moimId);
}
}
2 changes: 2 additions & 0 deletions backend/src/main/java/mouda/backend/moim/domain/Moim.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class Moim {

private String place;

private int currentPeople;

private int maxPeople;

private String authorNickname;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package mouda.backend.moim.dto.response;

import java.time.LocalDate;
import java.time.LocalTime;

import lombok.Builder;
import mouda.backend.moim.domain.Moim;

@Builder
public record MoimDetailsFindResponse(
String title,
LocalDate date,
LocalTime time,
String place,
int currentPeople,
int maxPeople,
String authorNickname,
String description
) {
public static MoimDetailsFindResponse toResponse(Moim moim) {
return MoimDetailsFindResponse.builder()
.title(moim.getTitle())
.date(moim.getDate())
.time(moim.getTime())
.place(moim.getPlace())
.currentPeople(moim.getCurrentPeople())
.maxPeople(moim.getMaxPeople())
.authorNickname(moim.getAuthorNickname())
.description(moim.getDescription())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public record MoimFindAllResponse(
LocalDate date,
LocalTime time,
String place,
int currentPeople,
int maxPeople,
String authorNickname,
String description
Expand All @@ -24,6 +25,7 @@ public static MoimFindAllResponse toResponse(Moim moim) {
.date(moim.getDate())
.time(moim.getTime())
.place(moim.getPlace())
.currentPeople(moim.getCurrentPeople())
.maxPeople(moim.getMaxPeople())
.authorNickname(moim.getAuthorNickname())
.description(moim.getDescription())
Expand Down
13 changes: 11 additions & 2 deletions backend/src/main/java/mouda/backend/moim/service/MoimService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.RequiredArgsConstructor;
import mouda.backend.moim.domain.Moim;
import mouda.backend.moim.dto.request.MoimCreateRequest;
import mouda.backend.moim.dto.response.MoimDetailsFindResponse;
import mouda.backend.moim.dto.response.MoimFindAllResponse;
import mouda.backend.moim.dto.response.MoimFindAllResponses;
import mouda.backend.moim.repository.MoimRepository;
Expand All @@ -33,10 +34,18 @@ public MoimFindAllResponses findAllMoim() {
);
}

public void deleteMoim(long id) {
@Transactional(readOnly = true)
public MoimDetailsFindResponse findMoimDetails(long id) {
Moim moim = moimRepository.findById(id)
.orElseThrow(IllegalArgumentException::new);
Copy link
Contributor

Choose a reason for hiding this comment

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

예외 메시지는 어떻게 할까여


return MoimDetailsFindResponse.toResponse(moim);
}

public void deleteMoim(long id) {
Moim moim = moimRepository.findById(id)
.orElseThrow(IllegalArgumentException::new);

moimRepository.delete(moim);
}
}
}
137 changes: 77 additions & 60 deletions backend/src/test/java/mouda/backend/moim/service/MoimServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,90 @@
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
import mouda.backend.config.DatabaseCleaner;
import mouda.backend.moim.domain.Moim;
import mouda.backend.moim.dto.request.MoimCreateRequest;
import mouda.backend.moim.dto.response.MoimFindAllResponses;
import mouda.backend.moim.repository.MoimRepository;

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.moim.domain.Moim;
import mouda.backend.moim.dto.request.MoimCreateRequest;
import mouda.backend.moim.dto.response.MoimDetailsFindResponse;
import mouda.backend.moim.dto.response.MoimFindAllResponses;
import mouda.backend.moim.repository.MoimRepository;

@SpringBootTest
class MoimServiceTest {

@Autowired
private MoimService moimService;

@Autowired
private MoimRepository moimRepository;

@Autowired
private DatabaseCleaner databaseCleaner;

@AfterEach
void cleanUp() {
databaseCleaner.cleanUp();
}

@DisplayName("모임을 생성한다.")
@Test
void createMoim() {
MoimCreateRequest moimCreateRequest = new MoimCreateRequest(
"title", LocalDate.now(), LocalTime.now(), "place",
10, "안나", "설명"
);
Moim moim = moimService.createMoim(moimCreateRequest);
assertThat(moim.getId()).isEqualTo(1L);
}

@DisplayName("모임을 전체 조회한다.")
@Test
void findAllMoim() {
MoimCreateRequest moimCreateRequest = new MoimCreateRequest(
"title", LocalDate.now(), LocalTime.now(), "place",
10, "안나", "설명"
);
moimService.createMoim(moimCreateRequest);
moimService.createMoim(moimCreateRequest);

MoimFindAllResponses moimResponses = moimService.findAllMoim();

assertThat(moimResponses).isNotNull();
assertThat(moimResponses.moims()).hasSize(2);
}

@DisplayName("모임을 삭제한다.")
@Test
void deleteMoim() {
MoimCreateRequest moimCreateRequest = new MoimCreateRequest(
"title", LocalDate.now(), LocalTime.now(), "place",
10, "안나", "설명"
);
moimService.createMoim(moimCreateRequest);

moimService.deleteMoim(1L);
List<Moim> moims = moimRepository.findAll();

assertThat(moims).hasSize(0);
}
@Autowired
private MoimService moimService;

@Autowired
private MoimRepository moimRepository;

@Autowired
private DatabaseCleaner databaseCleaner;

@AfterEach
void cleanUp() {
databaseCleaner.cleanUp();
}

@DisplayName("모임을 생성한다.")
@Test
void createMoim() {
MoimCreateRequest moimCreateRequest = new MoimCreateRequest(
"title", LocalDate.now(), LocalTime.now(), "place",
10, "안나", "설명"
);
Moim moim = moimService.createMoim(moimCreateRequest);
assertThat(moim.getId()).isEqualTo(1L);
}

@DisplayName("모임을 전체 조회한다.")
@Test
void findAllMoim() {
MoimCreateRequest moimCreateRequest = new MoimCreateRequest(
"title", LocalDate.now(), LocalTime.now(), "place",
10, "안나", "설명"
);
moimService.createMoim(moimCreateRequest);
moimService.createMoim(moimCreateRequest);

MoimFindAllResponses moimResponses = moimService.findAllMoim();

assertThat(moimResponses).isNotNull();
assertThat(moimResponses.moims()).hasSize(2);
}

@DisplayName("모임 상세를 조회한다.")
@Test
void findMoimDetails() {
MoimCreateRequest moimCreateRequest = new MoimCreateRequest(
"title", LocalDate.now(), LocalTime.now(), "place",
10, "안나", "설명"
);
moimService.createMoim(moimCreateRequest);

MoimDetailsFindResponse moimDetails = moimService.findMoimDetails(1L);

assertThat(moimDetails.authorNickname()).isEqualTo("안나");
}

@DisplayName("모임을 삭제한다.")
@Test
void deleteMoim() {
MoimCreateRequest moimCreateRequest = new MoimCreateRequest(
"title", LocalDate.now(), LocalTime.now(), "place",
10, "안나", "설명"
);
moimService.createMoim(moimCreateRequest);

moimService.deleteMoim(1L);
List<Moim> moims = moimRepository.findAll();

assertThat(moims).hasSize(0);
}
}
Loading