Chaeyeon0 / GreenDay_Study

여은개의 공부 일지
0 stars 0 forks source link

[20240522] History service 패키지, repository 작성 #18

Open Chaeyeon0 opened 1 month ago

Chaeyeon0 commented 1 month ago

먼저 기본적으로 <조회만> 가능하도록 기능을 우선적으로 만들어 보고 다 만들어지면 핵심적 기능을 만들어 나가려고 합니다. 사실 남은 기간에 있어 파일 페이지에 대한 기능을 완벽하게 구현할 수 있을지에 대해서 고민이 많았습니다.

프론트와 이 파일 페이지에 대해서 얘기를 나누어보았는데, 원래는 파일 페이지에서 7개의 일기가 저장이 되면 자동으로 파일이 생성되개 함 -> 이후 파일을 누르면 일기가 조회되도록 다음 7개 쌓이면 또 파일이 생성 되는 (반복) ... 파일 페이지에 필요한 기능과 목록을 나타내는 페이지에 필요한 API를 현재로서 두개를 완벽하게 만들 수 있는 상황이 아닌 것 같아 파일 페이지를 빼고

먼저 7개를 채워서 히스토리 열람 가능하도록 함(목록으로 열람 가능) -> 대신 다음 7개를 채워지기 전에 히스토리에 들어가면 이전의 7개만 보이도록 하고 다음 사과 일기 7개가 채워졌을 때 14개를 볼 수 있도록 하려고 합니다.

스크린샷 2024-05-24 103221

package com.example.greendayhistory.service;
import com.example.greendayhistory.domain.entity.Historyentity;
import com.example.greendayhistory.dto.HistoryDto;
import com.example.greendayhistory.repository.Historyrepository;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class Historyservice {
    private final Historyrepository historyRepository;

    @Autowired
    public Historyservice(Historyrepository historyRepository) {
        this.historyRepository = historyRepository;
    }

    public HistoryDto getHistoryByPageId(Integer pageId) {
        Historyentity historyEntity = historyRepository.findByPageId(pageId);
        if (historyEntity == null) {
            throw new EntityNotFoundException("History not found for page id: " + pageId);
        }
        return convertToDto(historyEntity);
    }

    public List<HistoryDto> getHistories(int groupSize) {
        List<Historyentity> allHistories = historyRepository.findAllByOrderByCreatedAtAsc();
        int availableGroups = allHistories.size() / groupSize;
        int historyCountToReturn = (availableGroups + 1) * groupSize;
        List<Historyentity> limitedHistories = allHistories.stream()
                .limit(historyCountToReturn)
                .collect(Collectors.toList());
        return limitedHistories.stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }

    private HistoryDto convertToDto(Historyentity entity) {
        return HistoryDto.builder()
                .page_id(entity.getPageId())  // DTO 빌더 메서드 수정
                .file_id(entity.getFileId())
                .diary_content(entity.getDiaryContent())
                .login_id(entity.getLoginId())
                .build();
    }
}

일단 현재까지의 상황 보고를 위해 쓰게 되었습니다. 하지만 빌드가 안되는걸 보니 리펙토링이 필요하겠네요 !!!