Skip to content

Commit

Permalink
Merge pull request #79 from study-hub-inu/feat/SH-297-post
Browse files Browse the repository at this point in the history
Feat/sh 297 post
  • Loading branch information
elyudwo authored Jan 26, 2024
2 parents f144cac + 6c04640 commit ba0f90f
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
import kr.co.studyhubinu.studyhubserver.common.dto.Converter;
import kr.co.studyhubinu.studyhubserver.exception.apply.ApplyNotFoundException;
import kr.co.studyhubinu.studyhubserver.exception.apply.SameUserRequestException;
import kr.co.studyhubinu.studyhubserver.exception.study.PostNotFoundExceptionByStudyId;
import kr.co.studyhubinu.studyhubserver.exception.user.UserNotFoundException;
import kr.co.studyhubinu.studyhubserver.study.domain.StudyEntity;
import kr.co.studyhubinu.studyhubserver.study.repository.StudyRepository;
import kr.co.studyhubinu.studyhubserver.studypost.domain.StudyPostEntity;
import kr.co.studyhubinu.studyhubserver.studypost.repository.StudyPostRepository;
import kr.co.studyhubinu.studyhubserver.user.domain.UserEntity;
import kr.co.studyhubinu.studyhubserver.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -25,6 +28,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static kr.co.studyhubinu.studyhubserver.apply.enums.Inspection.ACCEPT;

@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
Expand All @@ -33,6 +38,7 @@ public class ApplyService {
private final UserRepository userRepository;
private final StudyRepository studyRepository;
private final ApplyRepository applyRepository;
private final StudyPostRepository studyPostRepository;

@Transactional
public void enroll(Long userId, EnrollApplyRequest request) {
Expand All @@ -48,6 +54,8 @@ public void update(UpdateApplyRequest request) {
UserEntity user = userRepository.findById(request.getUserId()).orElseThrow(UserNotFoundException::new);
StudyEntity study = studyRepository.findById(request.getStudyId()).orElseThrow();
ApplyEntity applyEntity = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId()).orElseThrow(ApplyNotFoundException::new);

isAccept(request);
applyEntity.update(request.getInspection());
}

Expand All @@ -67,9 +75,16 @@ private void validateSameRequest(UserEntity user, StudyEntity study) {
public FindParticipateApplyResponse getParticipateApply(final Long userId, final int page, final int size) {
UserEntity user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new);
final Pageable pageable = PageRequest.of(page, size);
Long totalCount = applyRepository.countByUserIdAndInspection(user.getId(), Inspection.ACCEPT);
Long totalCount = applyRepository.countByUserIdAndInspection(user.getId(), ACCEPT);
Slice<ParticipateApplyData> participateApplyData = Converter.toSlice
(pageable, applyRepository.findByUserIdAndInspection(user.getId(), pageable));
return new FindParticipateApplyResponse(totalCount, participateApplyData);
}

private void isAccept(UpdateApplyRequest request) {
if(request.getInspection().equals(ACCEPT)) {
StudyPostEntity post = studyPostRepository.findByStudyId(request.getStudyId()).orElseThrow(PostNotFoundExceptionByStudyId::new);
post.minusRemainingSeat();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package kr.co.studyhubinu.studyhubserver.exception.study;

import kr.co.studyhubinu.studyhubserver.exception.StatusType;
import kr.co.studyhubinu.studyhubserver.exception.common.CustomException;

public class PostNotFoundExceptionByStudyId extends CustomException {

private final StatusType status;
private static final String message = "해당 스터디 아이디를 가진 게시글이 없습니다.";

public PostNotFoundExceptionByStudyId() {
super(message);
this.status = StatusType.POST_NOT_FOUND;
}

@Override
public StatusType getStatus() {
return status;
}

@Override
public String getMessage() {
return message;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,8 @@ public boolean isPostOfUser(Long userId) {
public void close() {
this.close = true;
}

public void minusRemainingSeat() {
this.remainingSeat--;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

public interface StudyPostRepository extends JpaRepository<StudyPostEntity, Long>, StudyPostRepositoryCustom {

Expand All @@ -20,4 +21,6 @@ public interface StudyPostRepository extends JpaRepository<StudyPostEntity, Long
@Modifying(clearAutomatically = true)
@Query("UPDATE StudyPostEntity sp SET sp.close = true WHERE sp.studyStartDate = :studyStartDate AND sp.close = false")
void closeStudyPostsByStartDate(@Param("studyStartDate") LocalDate studyStartDate);

Optional<StudyPostEntity> findByStudyId(Long studyId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import kr.co.studyhubinu.studyhubserver.apply.repository.ApplyRepository;
import kr.co.studyhubinu.studyhubserver.study.repository.StudyRepository;
import kr.co.studyhubinu.studyhubserver.study.domain.StudyEntity;
import kr.co.studyhubinu.studyhubserver.studypost.repository.StudyPostRepository;
import kr.co.studyhubinu.studyhubserver.user.domain.UserEntity;
import kr.co.studyhubinu.studyhubserver.user.repository.UserRepository;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -44,6 +45,9 @@ class ApplyServiceTest {
@Mock
ApplyRepository applyRepository;

@Mock
StudyPostRepository studyPostRepository;

@Test
void 스터디_가입신청() {
// given
Expand All @@ -64,24 +68,29 @@ class ApplyServiceTest {
applyService.enroll(user.getId(), request);
}

@Test
void 스터디_신청상태_변경() {
// given
UpdateApplyRequest request = UpdateApplyRequest.builder()
.userId(1L)
.studyId(1L)
.inspection(Inspection.ACCEPT)
.build();

Optional<UserEntity> user = Optional.ofNullable(UserEntity.builder().id(1L).build());
Optional<StudyEntity> study = Optional.ofNullable(StudyEntity.builder().id(1L).build());
when(userRepository.findById(anyLong())).thenReturn(user);
when(studyRepository.findById(anyLong())).thenReturn(study);
when(applyRepository.findByUserIdAndStudyId(1L, 1L)).thenReturn(Optional.ofNullable(ApplyEntity.builder().build()));

// when, then
applyService.update(request);
}
/**
* 비즈니스 레이어 단위테스트 의미있는 작업인지 고민중
*/

// @Test
// void 스터디_신청상태_변경() {
// // given
// UpdateApplyRequest request = UpdateApplyRequest.builder()
// .userId(1L)
// .studyId(1L)
// .inspection(Inspection.ACCEPT)
// .build();
//
// Optional<UserEntity> user = Optional.ofNullable(UserEntity.builder().id(1L).build());
// Optional<StudyEntity> study = Optional.ofNullable(StudyEntity.builder().id(1L).build());
// when(userRepository.findById(anyLong())).thenReturn(user);
// when(studyRepository.findById(anyLong())).thenReturn(study);
// when(applyRepository.findByUserIdAndStudyId(1L, 1L)).thenReturn(Optional.ofNullable(ApplyEntity.builder().build()));
// when(studyPostRepository.findByStudyId()).thenReturn()
//
// // when, then
// applyService.update(request);
// }

@Test
void 스터디_요청상태_조회() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import kr.co.studyhubinu.studyhubserver.bookmark.domain.BookmarkEntity;
import kr.co.studyhubinu.studyhubserver.bookmark.repository.BookmarkRepository;
import kr.co.studyhubinu.studyhubserver.exception.study.PostNotFoundException;
import kr.co.studyhubinu.studyhubserver.exception.study.PostNotFoundExceptionByStudyId;
import kr.co.studyhubinu.studyhubserver.study.domain.StudyEntity;
import kr.co.studyhubinu.studyhubserver.study.repository.StudyRepository;
import kr.co.studyhubinu.studyhubserver.studypost.domain.StudyPostEntity;
import kr.co.studyhubinu.studyhubserver.studypost.dto.data.*;
import kr.co.studyhubinu.studyhubserver.studypost.dto.request.InquiryRequest;
import kr.co.studyhubinu.studyhubserver.support.fixture.BookmarkEntityFixture;
import kr.co.studyhubinu.studyhubserver.support.fixture.StudyEntityFixture;
import kr.co.studyhubinu.studyhubserver.support.fixture.StudyPostEntityFixture;
import kr.co.studyhubinu.studyhubserver.support.fixture.UserEntityFixture;
import kr.co.studyhubinu.studyhubserver.support.repository.RepositoryTest;
Expand All @@ -28,11 +32,16 @@ class StudyPostRepositoryTest {

@Autowired
private StudyPostRepository studyPostRepository;

@Autowired
private BookmarkRepository bookmarkRepository;

@Autowired
private UserRepository userRepository;

@Autowired
private StudyRepository studyRepository;

@Test
void 유저의_식별자로_게시글_개수를_조회한다() {
// given
Expand Down Expand Up @@ -289,4 +298,23 @@ class StudyPostRepositoryTest {
// then
assertThat(posts.size()).isEqualTo(2);
}

@Test
void 스터디_식별자로_게시글_조회() {
// given
StudyEntity study = StudyEntityFixture.INU.studyEntity_생성();
StudyEntity savedStudy = studyRepository.save(study);
StudyPostEntity post = StudyPostEntityFixture.ENGINEER_INFORMATION_PROCESSING.studyPostEntity_생성_studyId_추가(1L, 1L, savedStudy.getId());

studyPostRepository.save(post);

// when
StudyPostEntity savedPost = studyPostRepository.findByStudyId(savedStudy.getId()).orElseThrow(PostNotFoundExceptionByStudyId::new);

// then
assertAll(
() -> assertTrue(savedPost.getContent().equals("심장을 바칠 사람만요")),
() -> assertTrue(savedPost.getTitle().equals("정처기 딸사람 구해요"))
);
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
package kr.co.studyhubinu.studyhubserver.support.fixture;

import kr.co.studyhubinu.studyhubserver.study.domain.StudyEntity;
import kr.co.studyhubinu.studyhubserver.user.enums.MajorType;

import java.time.LocalDate;

public enum StudyEntityFixture {

INU("인천대학교 스터디", "반가워요 잘해봐요", LocalDate.of(2024, 1, 3), LocalDate.of(2024, 1, 5), "asdadsa", 1L);
INU("인천대학교 스터디", "반가워요 잘해봐요", LocalDate.of(2024, 1, 3), LocalDate.of(2024, 1, 5), "asdadsa", 1L, MajorType.COMPUTER_SCIENCE_ENGINEERING);

private String title;
private String content;
private LocalDate studyStartDate;
private LocalDate studyEndDate;
private String chatUrl;
private Long userId;
private MajorType majorType;

StudyEntityFixture(String title, String content, LocalDate studyStartDate, LocalDate studyEndDate, String chatUrl, Long userId) {
StudyEntityFixture(String title, String content, LocalDate studyStartDate, LocalDate studyEndDate, String chatUrl, Long userId, MajorType majorType) {
this.title = title;
this.content = content;
this.studyStartDate = studyStartDate;
this.studyEndDate = studyEndDate;
this.chatUrl = chatUrl;
this.userId = userId;
this.majorType = majorType;
}

public StudyEntity studyEntity_생성() {
Expand All @@ -32,6 +35,7 @@ public enum StudyEntityFixture {
.studyEndDate(studyEndDate)
.chatUrl(chatUrl)
.userId(userId)
.major(majorType)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public enum StudyPostEntityFixture {
private final LocalDate studyStartDate;
private final LocalDate studyEndDate;
private final Integer remainingSeat;
private final Long studyId;
private Long studyId;

StudyPostEntityFixture(String title, String content, MajorType major, Integer studyPerson, GenderType gender, StudyWayType studyWay, Integer penalty, String penaltyWay, LocalDate studyStartDate, LocalDate studyEndDate, Integer remainingSeat, Long studyId) {
this.title = title;
Expand Down Expand Up @@ -79,4 +79,27 @@ public enum StudyPostEntityFixture {
.studyId(this.studyId)
.build();
}

public StudyPostEntity studyPostEntity_생성_studyId_추가(Long userId, Long postId, Long studyId) {
return StudyPostEntity.builder()
.id(postId)
.title(this.title)
.content(this.content)
.major(this.major)
.studyPerson(this.studyPerson)
.filteredGender(this.gender)
.studyWay(this.studyWay)
.penalty(this.penalty)
.penaltyWay(this.penaltyWay)
.studyStartDate(this.studyStartDate)
.studyEndDate(this.studyEndDate)
.postedUserId(userId)
.remainingSeat(this.remainingSeat)
.studyId(studyId)
.build();
}

public void setStudyId(Long studyId) {
this.studyId = studyId;
}
}

0 comments on commit ba0f90f

Please sign in to comment.