|
| 1 | +package dmu.dasom.api.domain.activity.service; |
| 2 | + |
| 3 | +import dmu.dasom.api.domain.activity.dto.ActivityRequestDto; |
| 4 | +import dmu.dasom.api.domain.activity.dto.ActivityResponseDto; |
| 5 | +import dmu.dasom.api.domain.activity.entity.Activity; |
| 6 | +import dmu.dasom.api.domain.activity.entity.Section; |
| 7 | +import dmu.dasom.api.domain.activity.repository.ActivityRepository; |
| 8 | +import dmu.dasom.api.domain.activity.repository.SectionRepository; |
| 9 | +import dmu.dasom.api.domain.common.exception.CustomException; |
| 10 | +import dmu.dasom.api.domain.common.exception.ErrorCode; |
| 11 | +import lombok.RequiredArgsConstructor; |
| 12 | +import org.springframework.stereotype.Service; |
| 13 | +import org.springframework.transaction.annotation.Transactional; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +@Service |
| 18 | +@RequiredArgsConstructor |
| 19 | +@Transactional |
| 20 | +public class ActivityServiceImpl implements ActivityService { |
| 21 | + |
| 22 | + private final ActivityRepository activityRepository; |
| 23 | + private final SectionRepository sectionRepository; |
| 24 | + |
| 25 | + @Override |
| 26 | + @Transactional(readOnly = true) |
| 27 | + public List<ActivityResponseDto> getActivities() { |
| 28 | + return ActivityResponseDto.of(activityRepository.findAll()); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public void createActivity(ActivityRequestDto requestDto) { |
| 33 | + // DTO에서 받은 String(섹션 이름)으로 Section 엔티티를 찾거나 생성 |
| 34 | + Section section = findOrCreateSection(requestDto.getSection()); |
| 35 | + |
| 36 | + activityRepository.save(Activity.create( |
| 37 | + section, |
| 38 | + requestDto.getActivityDate(), |
| 39 | + requestDto.getTitle(), |
| 40 | + requestDto.getAward() |
| 41 | + )); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void updateActivity(Long activityId, ActivityRequestDto requestDto) { |
| 46 | + Activity activity = activityRepository.findById(activityId) |
| 47 | + .orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND)); |
| 48 | + |
| 49 | + // DTO에서 받은 String(섹션 이름)으로 Section 엔티티를 찾거나 생성 |
| 50 | + Section section = findOrCreateSection(requestDto.getSection()); |
| 51 | + |
| 52 | + activity.update( |
| 53 | + section, |
| 54 | + requestDto.getActivityDate(), |
| 55 | + requestDto.getTitle(), |
| 56 | + requestDto.getAward() |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void deleteActivity(Long activityId) { |
| 62 | + if (!activityRepository.existsById(activityId)) { |
| 63 | + throw new CustomException(ErrorCode.NOT_FOUND); |
| 64 | + } |
| 65 | + activityRepository.deleteById(activityId); |
| 66 | + } |
| 67 | + |
| 68 | + // 파라미터 타입을 Section에서 String으로 변경 |
| 69 | + private Section findOrCreateSection(String sectionName) { |
| 70 | + return sectionRepository.findByName(sectionName) |
| 71 | + .orElseGet(() -> sectionRepository.save(Section.create(sectionName))); |
| 72 | + } |
| 73 | +} |
0 commit comments