Skip to content

Commit

Permalink
Merge pull request #68 from Team-BomBomBom/feat/my_studies#BBB-145
Browse files Browse the repository at this point in the history
[BBB-145] Feat: ๋‚ด๊ฐ€ ๋งŒ๋“  ์Šคํ„ฐ๋”” ๋ชฉ๋ก API
  • Loading branch information
msjang4 authored Oct 10, 2024
2 parents 049c23d + 8330902 commit d226e53
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public ResponseEntity<Void> configureStudy(
@LoginUser AppUserDetails userDetails,
@PathVariable("id") Long studyId,
@Valid @RequestBody ConfigureStudyRequest configureStudyRequest) {

studyService.configure(
userDetails.getId(),
studyId,
Expand Down Expand Up @@ -253,4 +253,15 @@ public ResponseEntity<List<AssignmentResult>> createAssignments(

return ResponseEntity.created(location).body(assignmentResults);
}

@GetMapping("/owned")
public ResponseEntity<List<StudyResponse>> ownedStudies(
@LoginUser AppUserDetails userDetails
) {

return ResponseEntity.ok(studyService.getOwnedStudies(userDetails.getId()).stream().map(
StudyResponse::fromResult
).toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,8 @@ public void startVoting(Long userId, Long studyId) {
studyRepository.save(study);
}


public List<StudyResult> getOwnedStudies(Long userId) {
List<Study> studies = studyRepository.findAllByLeader(userId);
return studies.stream().map(StudyResult::fromEntity).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import static com.bombombom.devs.study.model.Study.MAX_DIFFICULTY_LEVEL;
import static com.bombombom.devs.study.model.Study.MIN_DIFFICULTY_LEVEL;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
Expand Down Expand Up @@ -198,6 +199,70 @@ public void init() {
bookRepository.save(testBook);
}


@Test
@DisplayName("์ž์‹ ์ด ๊ฐœ์„คํ•œ ์Šคํ„ฐ๋”” ๋ชฉ๋ก์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.")
@WithUserDetails(value = "testuser",
setupBefore = TestExecutionEvent.TEST_EXECUTION)
void can_get_my_studies() throws Exception {
/*
* Given
*/
LocalDate roundStartDate = LocalDate.of(2024, 7, 22);
Study study = BookStudy.builder()
.name("์Šคํ„ฐ๋””")
.introduce("์•ˆ๋…•ํ•˜์„ธ์š”")
.headCount(1)
.capacity(5)
.penalty(10000)
.reliabilityLimit(0)
.startDate(roundStartDate)
.weeks(2)
.leader(testuser)
.book(testBook)
.votingProcess(VotingProcess.READY)
.state(StudyStatus.RUNNING)
.duplicated(false)
.build();
studyRepository.save(study);

Study study2 = BookStudy.builder()
.name("์Šคํ„ฐ๋””")
.introduce("์•ˆ๋…•ํ•˜์„ธ์š”")
.headCount(1)
.capacity(5)
.penalty(10000)
.reliabilityLimit(0)
.startDate(roundStartDate)
.weeks(2)
.leader(testuser)
.book(testBook)
.votingProcess(VotingProcess.READY)
.state(StudyStatus.RUNNING)
.duplicated(false)
.build();
studyRepository.save(study2);
/*
* When
*/
ResultActions resultActions = mockMvc.perform(
get("/api/v1/studies/owned")
);

/*
* Then
*/

resultActions.andDo(print())
.andExpect(status().isOk())
.andExpect(
jsonPath("$.length()")
.value(equalTo(2)))
.andExpect(
jsonPath("$[*].id",
containsInAnyOrder(study.getId().intValue(), study2.getId().intValue())));
}

@Nested
@DisplayName("ํˆฌํ‘œ ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ")
class VoteTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.bombombom.devs.study.model.Study;
import jakarta.persistence.LockModeType;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -11,6 +12,14 @@

public interface StudyRepository extends JpaRepository<Study, Long> {


@Query(value = "SELECT s FROM Study s "
+ "LEFT JOIN FETCH s.leader "
+ "LEFT JOIN FETCH TREAT(s as AlgorithmStudy).difficulties "
+ "LEFT JOIN FETCH TREAT(s as BookStudy).book "
+ "WHERE s.leader.id = :id")
List<Study> findAllByLeader(Long id);

@Query("select s from Study s "
+ "join fetch s.leader "
+ "where s.id = :id")
Expand Down

0 comments on commit d226e53

Please sign in to comment.