-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: StudyService #1 (Make Study, Get Study, Pagination)
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/main/java/dcom/homepage/api/domain/study/repository/StudyRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package dcom.homepage.api.domain.study.repository; | ||
|
||
import dcom.homepage.api.domain.study.Study; | ||
import dcom.homepage.api.domain.study.dto.StudyResponseDto; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface StudyRepository extends JpaRepository<Study, Integer> { | ||
Optional<Study> getStudyById(Integer id); | ||
Page<Study> findAll(Pageable pageable); | ||
Page<Study> findAllByNameContains(String name, Pageable pageable); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/dcom/homepage/api/domain/study/service/StudyService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package dcom.homepage.api.domain.study.service; | ||
|
||
import dcom.homepage.api.domain.study.Study; | ||
import dcom.homepage.api.domain.study.dto.StudyRequestDto; | ||
import dcom.homepage.api.domain.study.dto.StudyResponseDto; | ||
import dcom.homepage.api.domain.users.User; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface StudyService { | ||
StudyResponseDto.Info findStudyById(Integer id); | ||
StudyResponseDto.SimpleInfo makeStudy(User user, StudyRequestDto.Make make); | ||
Page<StudyResponseDto.SimpleInfo> findAll(Pageable pageable); | ||
Page<StudyResponseDto.SimpleInfo> findAllByNameContains(String name, Pageable pageable); | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/dcom/homepage/api/domain/study/service/StudyServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package dcom.homepage.api.domain.study.service; | ||
|
||
import dcom.homepage.api.domain.study.Study; | ||
import dcom.homepage.api.domain.study.dto.StudyRequestDto; | ||
import dcom.homepage.api.domain.study.dto.StudyResponseDto; | ||
import dcom.homepage.api.domain.study.repository.StudyRepository; | ||
import dcom.homepage.api.domain.users.User; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class StudyServiceImpl implements StudyService { | ||
private final StudyRepository studyRepository; | ||
|
||
@Override | ||
public StudyResponseDto.Info findStudyById(Integer id) { | ||
return StudyResponseDto.Info.of(studyRepository.getStudyById(id).orElseThrow( | ||
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "스터디를 찾을 수 없습니다.") | ||
)); | ||
} | ||
|
||
@Override | ||
public StudyResponseDto.SimpleInfo makeStudy(User user, StudyRequestDto.Make make) { | ||
Study study = Study.builder() | ||
.name(make.getName()) | ||
.type(make.getType()) | ||
.description(make.getDescription()) | ||
.startDate(make.getStartDate()) | ||
.endDate(make.getEndDate()) | ||
.cycle(make.getCycle()) | ||
.build(); | ||
studyRepository.save(study); | ||
|
||
return StudyResponseDto.SimpleInfo.of(study); | ||
} | ||
|
||
@Override | ||
public Page<StudyResponseDto.SimpleInfo> findAll(Pageable pageable) { | ||
return studyRepository.findAll(pageable).map(StudyResponseDto.SimpleInfo::of); | ||
} | ||
|
||
@Override | ||
public Page<StudyResponseDto.SimpleInfo> findAllByNameContains(String name, Pageable pageable) { | ||
return studyRepository.findAllByNameContains(name, pageable).map(StudyResponseDto.SimpleInfo::of); | ||
} | ||
} |