|
| 1 | +package dmu.dasom.api.domain.activity.service; |
| 2 | + |
| 3 | +import dmu.dasom.api.domain.activity.dto.ActivityHistoryRequestDto; |
| 4 | +import dmu.dasom.api.domain.activity.dto.ActivityHistoryResponseDto; |
| 5 | +import dmu.dasom.api.domain.activity.dto.GroupedActivityHistoryDto; |
| 6 | +import dmu.dasom.api.domain.activity.entity.ActivityHistory; |
| 7 | +import dmu.dasom.api.domain.activity.repository.ActivityHistoryRepository; |
| 8 | +import dmu.dasom.api.domain.common.exception.CustomException; |
| 9 | +import dmu.dasom.api.domain.common.exception.ErrorCode; |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import org.springframework.stereotype.Service; |
| 12 | +import org.springframework.transaction.annotation.Transactional; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +@Service |
| 17 | +@RequiredArgsConstructor |
| 18 | +@Transactional |
| 19 | +public class ActivityHistoryServiceImpl implements ActivityHistoryService { |
| 20 | + |
| 21 | + private final ActivityHistoryRepository historyRepository; |
| 22 | + |
| 23 | + @Override |
| 24 | + @Transactional(readOnly = true) |
| 25 | + public List<GroupedActivityHistoryDto> getAllHistories() { |
| 26 | + List<ActivityHistory> histories = historyRepository.findAll(); |
| 27 | + return GroupedActivityHistoryDto.groupedActivityHistoryDto(histories); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public ActivityHistoryResponseDto createHistory(ActivityHistoryRequestDto requestDto) { |
| 32 | + ActivityHistory history = requestDto.toEntity(); |
| 33 | + ActivityHistory savedHistory = historyRepository.save(history); |
| 34 | + return ActivityHistoryResponseDto.toDto(savedHistory); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public ActivityHistoryResponseDto updateHistory(Long id, ActivityHistoryRequestDto requestDto) { |
| 39 | + ActivityHistory history = historyRepository.findById(id) |
| 40 | + // CustomException 사용 |
| 41 | + .orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND)); |
| 42 | + |
| 43 | + history.update( |
| 44 | + requestDto.getYear(), |
| 45 | + requestDto.getSection(), |
| 46 | + requestDto.getTitle(), |
| 47 | + requestDto.getAward() |
| 48 | + ); |
| 49 | + return ActivityHistoryResponseDto.toDto(history); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void deleteHistory(Long id) { |
| 54 | + // CustomException 사용 |
| 55 | + if (!historyRepository.existsById(id)) { |
| 56 | + throw new CustomException(ErrorCode.NOT_FOUND); |
| 57 | + } |
| 58 | + historyRepository.deleteById(id); |
| 59 | + } |
| 60 | +} |
0 commit comments