-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: 벌레 조회 기능 구현 #21
Changes from 16 commits
12a512d
6f4b4e0
a4f3897
e7ab9ef
3e45ef9
ee520a8
a9afd52
1c0c287
594208d
48f0e7c
ea7170e
6d66ee5
bd35b4e
2f493bd
1eb913a
48f49d2
3c90fa8
416adac
ebb1e1e
89163c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
## 📋 Checklist | ||
|
||
- [ ] 🔀 PR 제목의 형식을 잘 작성했나요? (e.g. `feat: 유저 조회 기능 구현`) | ||
- [ ] 🏷️ 라벨, 프로젝트, 마일스톤은 등록했나요? | ||
- [ ] 🧹 코드 스멜은 해결했나요? | ||
|
||
## 🧩 이슈 번호 <!-- 이슈 번호를 작성해주세요 ex) #11 --> | ||
|
||
- #이슈번호 | ||
- close #이슈번호 | ||
|
||
## 👩💻 공유 포인트 및 논의 사항 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.moabam.api.application; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.moabam.api.domain.entity.Member; | ||
import com.moabam.api.dto.BugMapper; | ||
import com.moabam.api.dto.BugResponse; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class BugService { | ||
|
||
private final MemberService memberService; | ||
|
||
public BugResponse getBug(Long memberId) { | ||
Member member = memberService.getById(memberId); | ||
return BugMapper.from(member.getBug()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.moabam.api.application; | ||
|
||
import static com.moabam.global.error.model.ErrorMessage.*; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.moabam.api.domain.entity.Member; | ||
import com.moabam.api.domain.repository.MemberRepository; | ||
import com.moabam.global.error.exception.NotFoundException; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class MemberService { | ||
|
||
private final MemberRepository memberRepository; | ||
|
||
public Member getById(Long memberId) { | ||
return memberRepository.findById(memberId) | ||
.orElseThrow(() -> new NotFoundException(MEMBER_NOT_FOUND)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.moabam.api.domain.entity; | ||
|
||
import static com.moabam.global.error.model.ErrorMessage.*; | ||
|
||
import org.hibernate.annotations.ColumnDefault; | ||
|
||
import com.moabam.global.error.exception.BadRequestException; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Embeddable; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Embeddable | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Bug { | ||
|
||
@Column(name = "morning_bug", nullable = false) | ||
@ColumnDefault("0") | ||
private int morningBug; | ||
|
||
@Column(name = "night_bug", nullable = false) | ||
@ColumnDefault("0") | ||
private int nightBug; | ||
|
||
@Column(name = "golden_bug", nullable = false) | ||
@ColumnDefault("0") | ||
private int goldenBug; | ||
|
||
@Builder | ||
private Bug(int morningBug, int nightBug, int goldenBug) { | ||
this.morningBug = validateBugCount(morningBug); | ||
this.nightBug = validateBugCount(nightBug); | ||
this.goldenBug = validateBugCount(goldenBug); | ||
} | ||
|
||
private int validateBugCount(int bug) { | ||
if (bug < 0) { | ||
throw new BadRequestException(INVALID_BUG_COUNT); | ||
} | ||
|
||
return bug; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.moabam.api.domain.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.moabam.api.domain.entity.Member; | ||
|
||
public interface MemberRepository extends JpaRepository<Member, Long> { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.moabam.api.dto; | ||
|
||
import com.moabam.api.domain.entity.Bug; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public final class BugMapper { | ||
|
||
public static BugResponse from(Bug bug) { | ||
return BugResponse.builder() | ||
.morningBug(bug.getMorningBug()) | ||
.nightBug(bug.getNightBug()) | ||
.goldenBug(bug.getGoldenBug()) | ||
.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.moabam.api.dto; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record BugResponse( | ||
int morningBug, | ||
int nightBug, | ||
int goldenBug | ||
) { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.moabam.api.presentation; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.moabam.api.application.BugService; | ||
import com.moabam.api.dto.BugResponse; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequestMapping("/bugs") | ||
@RequiredArgsConstructor | ||
public class BugController { | ||
|
||
private final BugService bugService; | ||
|
||
@GetMapping | ||
public ResponseEntity<BugResponse> getBug() { | ||
return ResponseEntity.ok(bugService.getBug(1L)); | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,11 @@ public enum ErrorMessage { | |
ROOM_MODIFY_UNAUTHORIZED_REQUEST("방장이 아닌 사용자는 방을 수정할 수 없습니다."), | ||
PARTICIPANT_NOT_FOUND("방에 대한 참여자의 정보가 없습니다."), | ||
LOGIN_FAILED("로그인에 실패했습니다."), | ||
REQUEST_FAILD("네트우크 접근 실패입니다."); | ||
REQUEST_FAILD("네트우크 접근 실패입니다."), | ||
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. @parksey 이것 좀 수정해주세요ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ 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. ㅇㅈㅋㅋㅋㅋ 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. ㅋㅋㅋㅋㅋㅋㅋ맞다 이거였다 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. 고쳤습니다ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ |
||
|
||
MEMBER_NOT_FOUND("존재하지 않는 회원입니다."), | ||
|
||
INVALID_BUG_COUNT("벌레 개수는 0 이상이어야 합니다."); | ||
|
||
private final String message; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.moabam.api.application; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.mockito.BDDMockito.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import com.moabam.api.domain.entity.Bug; | ||
import com.moabam.api.domain.entity.Member; | ||
import com.moabam.api.dto.BugResponse; | ||
import com.moabam.fixture.MemberFixture; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class BugServiceTest { | ||
|
||
@InjectMocks | ||
BugService bugService; | ||
|
||
@Mock | ||
MemberService memberService; | ||
|
||
@DisplayName("벌레를 조회한다.") | ||
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. 조회 실패에 대한 테스트 코드는 없나요? |
||
@Test | ||
void get_bug_success() { | ||
// given | ||
Long memberId = 1L; | ||
Member member = MemberFixture.member(); | ||
given(memberService.getById(memberId)).willReturn(member); | ||
|
||
// when | ||
BugResponse response = bugService.getBug(memberId); | ||
|
||
// then | ||
Bug bug = member.getBug(); | ||
assertThat(response.morningBug()).isEqualTo(bug.getMorningBug()); | ||
assertThat(response.nightBug()).isEqualTo(bug.getNightBug()); | ||
assertThat(response.goldenBug()).isEqualTo(bug.getGoldenBug()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.moabam.api.domain.entity; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.moabam.global.error.exception.BadRequestException; | ||
|
||
class BugTest { | ||
|
||
@DisplayName("벌레 개수가 음수이면 예외가 발생한다.") | ||
@Test | ||
void negative_bug_count_throws_exception() { | ||
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. 벌레 개수가 3개 전부이기도 하고 |
||
Bug.BugBuilder bugBuilder = Bug.builder() | ||
.morningBug(10) | ||
.nightBug(10) | ||
.goldenBug(-10); | ||
|
||
assertThatThrownBy(bugBuilder::build) | ||
.isInstanceOf(BadRequestException.class) | ||
.hasMessage("벌레 개수는 0 이상이어야 합니다."); | ||
} | ||
} |
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.
C: 저희 ResponseEntity 사용하기로 했었나요?? 제가 잘 기억이 안나서 @ResponseStatus 를 쓰긴했는데 만약 ResponseEntity 쓰는거였으면 바꿔야겠군유
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.
R: ResponseEntity 안쓰기로 해씀다!!
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.
저 RestTemplate을 사용하게 되면 RespnoseEntity사용하게 되는데 이때는 사용해도 될까요?
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.
아 맞네유 자연스럽게 쓰긴 했는데, 찾아보니까 ResponseEntity가 좀 더 유연성있다는 점 말고는 차이가 없는 것 같아서 정합시다요