|
| 1 | +package dcom.homepage.api.domain.study.service; |
| 2 | + |
| 3 | +import dcom.homepage.api.domain.study.Study; |
| 4 | +import dcom.homepage.api.domain.study.dto.StudyRequestDto; |
| 5 | +import dcom.homepage.api.domain.study.dto.StudyResponseDto; |
| 6 | +import dcom.homepage.api.domain.study.repository.StudyRepository; |
| 7 | +import dcom.homepage.api.domain.users.User; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import org.springframework.data.domain.Page; |
| 10 | +import org.springframework.data.domain.Pageable; |
| 11 | +import org.springframework.http.HttpStatus; |
| 12 | +import org.springframework.stereotype.Service; |
| 13 | +import org.springframework.web.server.ResponseStatusException; |
| 14 | + |
| 15 | +@Service |
| 16 | +@RequiredArgsConstructor |
| 17 | +public class StudyServiceImpl implements StudyService { |
| 18 | + private final StudyRepository studyRepository; |
| 19 | + |
| 20 | + @Override |
| 21 | + public StudyResponseDto.Info findStudyById(Integer id) { |
| 22 | + return StudyResponseDto.Info.of(studyRepository.getStudyById(id).orElseThrow( |
| 23 | + () -> new ResponseStatusException(HttpStatus.NOT_FOUND, "스터디를 찾을 수 없습니다.") |
| 24 | + )); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public StudyResponseDto.SimpleInfo makeStudy(User user, StudyRequestDto.Make make) { |
| 29 | + Study study = Study.builder() |
| 30 | + .name(make.getName()) |
| 31 | + .type(make.getType()) |
| 32 | + .description(make.getDescription()) |
| 33 | + .startDate(make.getStartDate()) |
| 34 | + .endDate(make.getEndDate()) |
| 35 | + .cycle(make.getCycle()) |
| 36 | + .build(); |
| 37 | + studyRepository.save(study); |
| 38 | + |
| 39 | + return StudyResponseDto.SimpleInfo.of(study); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public Page<StudyResponseDto.SimpleInfo> findAll(Pageable pageable) { |
| 44 | + return studyRepository.findAll(pageable).map(StudyResponseDto.SimpleInfo::of); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Page<StudyResponseDto.SimpleInfo> findAllByNameContains(String name, Pageable pageable) { |
| 49 | + return studyRepository.findAllByNameContains(name, pageable).map(StudyResponseDto.SimpleInfo::of); |
| 50 | + } |
| 51 | +} |
0 commit comments