Skip to content

Commit

Permalink
feat: StudyService #1 (Make Study, Get Study, Pagination)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustKode committed Mar 28, 2022
1 parent 119239b commit 18055c6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
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);
}
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);
}
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);
}
}

0 comments on commit 18055c6

Please sign in to comment.