게시글 목록 조회
JPQL의 결과로 나오는 Object[]를 DTO로 변환하여 게시글 목록을 출력할 수 있다.
- Objcet[]의 내용을 파라미터로 전달받아서 StadiumDTO를 구성한다.
=> LandersService의 entityToDTO() 메소드를 이용하여 처리할 수 있다.
- entityToDTO의 파라미터로 Member를 처리할 수 있다.
하지만 여기에서는 entity에 Member 정보를 담아서 한 번에 처리하도록 했다.
※ 추후에 변경하는 것이 좋을 것 같다. > 불필요한 데이터의 중복
default StadiumDTO entityToDTO(LandersStadium entity) {
StadiumDTO stadiumDTO = StadiumDTO.builder()
.sno(entity.getSno())
.base(entity.getBase())
.section(entity.getSection())
.row(entity.getRow())
.num(entity.getNum())
.regDate(entity.getRegDate())
.modDate(entity.getModDate())
// Member
.username(entity.getUsername())
.name(entity.getName())
.mno(entity.getMno())
.build();
return stadiumDTO;
}
게시글 조회
게시글의 번호(sno)를 파라미터로 받아서 게시글을 출력할 수 있다.(기능 정의: Service, 구현: ServiceImpl)
- LandersService 인터페이스의 getStadium() 메소드를 이용하여 처리한다.
StadiumDTO getStadium(Long sno, PrincipalDetail principalDetail);
- 게시글 번호(sno)를 파라미터로 StadiumRepository의 getStadiumWithAll()을 이용하여 게시글 정보를 가져온다.
=> Repository에서 가져오는 Stadium, StadiumImageList를 가공해야 한다.
1) LandersStadium 엔티티의 모든 Row는 동일한 값을 출력한다. 따라서 가장 앞의 값을 가져온다.
2) 등록된 이미지의 개수만큼 Image 객체가 필요하다.
forEach문의 arr[] => arr[0]은 LandersStadium, arr[1]은 LandersStadiumImage이 담겨있다.
forEach문을 통해 StadiumImageList에 이미지 값을 담은 후 return을 통해 Entity값을 DTO로 변환한다.
@Override
public StadiumDTO getStadium(Long sno, PrincipalDetail principalDetail) {
List<Object[]> result = stadiumRepository.getStadiumWithAll(sno);
1) LandersStadium stadium = (LandersStadium) result.get(0)[0];
2) List<LandersStadiumImage> stadiumImageList = new ArrayList<>();
result.forEach(arr -> {
LandersStadiumImage stadiumImage = (LandersStadiumImage) arr[1];
stadiumImageList.add(stadiumImage);
});
return entitiesToDTO(stadium, stadiumImageList);
}
- 이미지는 getStadium에서 조회한 썸네일URL을 통해 출력할 수 있다.
조회 화면에서는 th:each문을 통해 등록된 이미지 개수만큼 이미지를 출력한다.
<div class="readImg mt-3">
<a>※ 하단의 이미지를 클릭하면 원본 크기로 확인할 수 있습니다.</a>
<ul>
<li th:each="stadiumImage : ${dto.imageDTOList}" th:data-file="${stadiumImage.getThumbnailURL()}">
<img th:if="${stadiumImage.path != null}"
th:src="|/display?fileName=${stadiumImage.getThumbnailURL()}|">
</li>
</ul>
</div>
'BACK-END > Springboot' 카테고리의 다른 글
[BaseBall GO 리뷰] 후기 게시글 검색 (0) | 2023.01.30 |
---|---|
[BaseBall GO 리뷰] 후기 게시글 등록 (1) | 2023.01.30 |
게시글 삭제하기 (0) | 2022.10.25 |
게시글 수정하기 (0) | 2022.10.25 |
게시글 검색하기 (0) | 2022.10.20 |