Skip to content

Commit

Permalink
Merge pull request #78 from study-hub-inu/refactor/SH-289-post
Browse files Browse the repository at this point in the history
Refactor/sh 289 post
  • Loading branch information
elyudwo authored Jan 25, 2024
2 parents bb9782c + fdc9009 commit 5408c02
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ public class ApplyEntity extends BaseTimeEntity {
private Long userId;

@Builder
public ApplyEntity(Inspection inspection, String introduce, Long study, Long userId) {
public ApplyEntity(Inspection inspection, String introduce, Long studyId, Long userId) {
this.inspection = inspection;
this.introduce = introduce;
this.studyId = study;
this.studyId = studyId;
this.userId = userId;
}

public static ApplyEntity of(Long userId, Long studyId, String introduce) {
return ApplyEntity.builder()
.userId(userId)
.study(studyId)
.studyId(studyId)
.introduce(introduce)
.inspection(Inspection.STANDBY)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public ResponseEntity<FindPostResponseByBookmark> getBookmarkedPosts(@RequestPar
}

@Operation(summary = "스터디 단건 조회", description = "url 끝에 postId를 넣어주세요")
@GetMapping("/v1/study-posts/{postId}")
@GetMapping("/v2/study-posts/{postId}")
public ResponseEntity<FindPostResponseById> findPostById(@PathVariable Long postId, UserId userId) {
FindPostResponseById findPostResponseById = studyPostFindService.findPostById(postId, userId.getId());
return ResponseEntity.ok(findPostResponseById);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@

@Getter
public class FindPostResponseById {
private Long postId;
private String title;
private LocalDateTime createdDate;
private String content;
private MajorType major;
private int studyPerson;
private GenderType filteredGender;
private StudyWayType studyWay;
private int penalty;
private String penaltyWay;
private LocalDate studyStartDate;
private LocalDate studyEndDate;
private int remainingSeat;
private String chatUrl;
private boolean isUsersPost;
private boolean isBookmarked;
private UserData postedUser;
private List<PostDataByMajor> relatedPost;
private final Long postId;
private final String title;
private final LocalDateTime createdDate;
private final String content;
private final MajorType major;
private final int studyPerson;
private final GenderType filteredGender;
private final StudyWayType studyWay;
private final int penalty;
private final String penaltyWay;
private final LocalDate studyStartDate;
private final LocalDate studyEndDate;
private final int remainingSeat;
private final String chatUrl;
private final boolean isUsersPost;
private final boolean isBookmarked;
private final boolean isApply;
private final UserData postedUser;
private final List<PostDataByMajor> relatedPost;

public FindPostResponseById(PostData postData, List<PostDataByMajor> relatedPosts) {
public FindPostResponseById(PostData postData, List<PostDataByMajor> relatedPosts, boolean isApply) {
this.postId = postData.getPostId();
this.title = postData.getTitle();
this.createdDate = postData.getCreatedDate();
Expand All @@ -52,5 +53,6 @@ public FindPostResponseById(PostData postData, List<PostDataByMajor> relatedPost
this.isBookmarked = postData.isBookmarked();
this.postedUser = postData.getPostedUser();
this.relatedPost = relatedPosts;
this.isApply = isApply;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kr.co.studyhubinu.studyhubserver.studypost.service;


import kr.co.studyhubinu.studyhubserver.apply.domain.ApplyEntity;
import kr.co.studyhubinu.studyhubserver.apply.repository.ApplyRepository;
import kr.co.studyhubinu.studyhubserver.bookmark.repository.BookmarkRepository;
import kr.co.studyhubinu.studyhubserver.common.dto.Converter;
import kr.co.studyhubinu.studyhubserver.exception.study.PostNotFoundException;
Expand All @@ -24,17 +26,19 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
@Service
@Transactional
@Transactional(readOnly = true)
public class StudyPostFindService {

private static final int POST_RECOMMEND_COUNT = 10;

private final StudyPostRepository studyPostRepository;
private final UserRepository userRepository;
private final BookmarkRepository bookMarkRepository;
private final ApplyRepository applyRepository;

public FindPostResponseByInquiry findPostResponseByInquiry(final InquiryRequest inquiryRequest, final int page, final int size, Long userId) {
final Pageable pageable = PageRequest.of(page, size);
Expand Down Expand Up @@ -69,7 +73,14 @@ public FindPostResponseByUserId getMyPosts(int page, int size, Long userId) {

public FindPostResponseById findPostById(Long postId, Long userId) {
final PostData postData = findPostDataById(postId, userId);
return new FindPostResponseById(postData, getRelatedPosts(postData.getMajor(), postId));
boolean isApply = getUserApply(userId, postData);

return new FindPostResponseById(postData, getRelatedPosts(postData.getMajor(), postId), isApply);
}

private boolean getUserApply(Long userId, PostData postData) {
Optional<ApplyEntity> apply = applyRepository.findByUserIdAndStudyId(userId, postData.getStudyId());
return apply.isPresent();
}

public List<PostDataByMajor> getRelatedPosts(MajorType major, Long exceptPostId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ApplyRepositoryTest {
// when
ApplyEntity apply = ApplyEntity.builder()
.userId(user.getId())
.study(study.getId())
.studyId(study.getId())
.inspection(Inspection.ACCEPT)
.build();
ApplyEntity result = applyRepository.save(apply);
Expand All @@ -79,7 +79,7 @@ class ApplyRepositoryTest {
// when
ApplyEntity applyEntity = ApplyEntity.builder()
.userId(user.getId())
.study(study.getId())
.studyId(study.getId())
.inspection(Inspection.ACCEPT)
.build();
applyRepository.save(applyEntity);
Expand All @@ -101,7 +101,7 @@ class ApplyRepositoryTest {
StudyEntity study = studyRepository.save(StudyEntityFixture.INU.studyEntity_생성());
ApplyEntity apply = ApplyEntity.builder()
.userId(user.getId())
.study(study.getId())
.studyId(study.getId())
.inspection(Inspection.ACCEPT)
.build();
applyRepository.save(apply);
Expand Down Expand Up @@ -132,14 +132,14 @@ class ApplyRepositoryTest {

ApplyEntity apply1 = ApplyEntity.builder()
.userId(user.getId())
.study(study.getId())
.studyId(study.getId())
.inspection(Inspection.ACCEPT)
.introduce("벌금내러 왔습니다.")
.build();

ApplyEntity apply2 = ApplyEntity.builder()
.userId(user2.getId())
.study(study.getId())
.studyId(study.getId())
.inspection(Inspection.ACCEPT)
.introduce("목숨을 걸겠습니다.")
.build();
Expand All @@ -156,7 +156,14 @@ class ApplyRepositoryTest {
}

@Test
void 스터디_참가요청_조회_유저데이터_반환() {
void 로그인하지_않은_유저가_조회할_경우_반환값_없음() {
// given
applyRepository.save(ApplyEntity.builder().userId(1L).studyId(1L).build());

// when
Optional<ApplyEntity> apply = applyRepository.findByUserIdAndStudyId(null, 1L);

// then
assertThat(apply).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ApplyServiceTest {
when(studyRepository.findById(anyLong())).thenReturn(Optional.ofNullable(study));
when(applyRepository.save(any())).thenReturn(ApplyEntity.builder()
.userId(user.getId())
.study(study.getId())
.studyId(study.getId())
.build());

// when, then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ static Stream<Arguments> requestParameters() {
void 스터디_단건_조회_성공() throws Exception {
// given
PostData postData = PostData.builder().build();
when(studyPostFindService.findPostById(any(), any())).thenReturn(new FindPostResponseById(postData, new ArrayList<>()));
when(studyPostFindService.findPostById(any(), any())).thenReturn(new FindPostResponseById(postData, new ArrayList<>(), true));

// when
ResultActions resultActions = performGetRequest("/api/v1/study-posts/1", null);
ResultActions resultActions = performGetRequest("/api/v2/study-posts/1", null);
MvcResult mvcResult = resultActions.andReturn();
String responseBody = mvcResult.getResponse().getContentAsString(UTF_8);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kr.co.studyhubinu.studyhubserver.studypost.service;

import kr.co.studyhubinu.studyhubserver.apply.domain.ApplyEntity;
import kr.co.studyhubinu.studyhubserver.apply.repository.ApplyRepository;
import kr.co.studyhubinu.studyhubserver.bookmark.repository.BookmarkRepository;
import kr.co.studyhubinu.studyhubserver.exception.study.PostNotFoundException;
import kr.co.studyhubinu.studyhubserver.exception.user.UserNotFoundException;
Expand Down Expand Up @@ -46,6 +48,9 @@ class StudyPostFindServiceTest {
@Mock
BookmarkRepository bookmarkRepository;

@Mock
ApplyRepository applyRepository;

@Test
void 스터디_게시글_전체_조회() {
// given
Expand Down Expand Up @@ -159,6 +164,7 @@ class StudyPostFindServiceTest {
postsByMajor.add(postDataByMajor);
when(studyPostRepository.findByMajor(any(), anyLong())).thenReturn(postsByMajor);
when(studyPostRepository.findPostById(anyLong(), anyLong())).thenReturn(Optional.ofNullable(PostData.builder().postId(1L).build()));
when(applyRepository.findByUserIdAndStudyId(anyLong(), any())).thenReturn(Optional.empty());

// when
FindPostResponseById postResponse = studyPostFindService.findPostById(1L, 1L);
Expand Down

0 comments on commit 5408c02

Please sign in to comment.