왕초보일지

240104 TIL |

다시은 2024. 1. 4. 21:18
1. 할 일 카드 목록 api 의 응답에 연관된 댓글 내용을 추가해주세요.

정렬기준 받으면서 댓글 내용 추가하는건 도저히 못하겠어서 정렬기준은 빼고 이름으로 댓글과 같이 조회해봤고 잘된다.

[
  {
    "card": {
      "id": 23,
      "status": "FALSE",
      "title": "1111",
      "content": "1111",
      "createdAt": "24. 1. 3. 오후 3:23",
      "name": "1111"
    },
    "comments": [
      {
        "id": 12,
        "name": "string",
        "content": "string",
        "createdAt": "24. 1. 4. 오후 5:16"
      },
      {
        "id": 13,
        "name": "string",
        "content": "string",
        "createdAt": "24. 1. 4. 오후 5:16"
      },
      {
        "id": 14,
        "name": "string",
        "content": "string",
        "createdAt": "24. 1. 4. 오후 5:16"
      }
    ]
  },
  {
    "card": {
      "id": 28,
      "status": "FALSE",
      "title": "string",
      "content": "string",
      "createdAt": "24. 1. 3. 오후 7:38",
      "name": "1111"
    },
    "comments": []
  },
  {
    "card": {
      "id": 29,
      "status": "FALSE",
      "title": "string",
      "content": "string",
      "createdAt": "24. 1. 4. 오후 3:53",
      "name": "1111"
    },
    "comments": []
  }
fun getAllCardsWithComments(name: String , pageable: Pageable): List<CardWithCommentResponse> {
        val cards = cardRepository.findAllByName(name)
        val cardListWithComments = cards.map{ card ->
            val cardId = card.id
            val comments = commentRepository.findAllByCardId(cardId)
            CardWithCommentResponse(card, comments.map { it.toResponse() })
        }
        return cardListWithComments
    }

 

 


2. 할 일 카드 목록 api에 pagination 기능을 추가해주세요
- offset 기반 pagination 과 cursor 기반 pagination 에 대해 알아보기

https://kimvampa.tistory.com/173

 

Pagination

JpaRespository 가 상속받고있는 페이징 Respository

 

 

pageable 인터페이스

스프링 데이터 JPA 에서 쿼리 메소드의 인자를 pageable 을 받으면 페이징 된 결과를 Page 객체로 반환한다.

controller에서 인자로 pageable 인터페이스를 넣어주고 @PageableDefault 어노테이션으로 기본 페이지 위치, 글 갯수, 정렬기준, 정렬등을 설정해준다.

반환타입이 Page<>여야 해서 PageImpl 로 감싸줬다.

설정한 사이즈만 조회하는 것도 안되고 정렬도 안된다...

다른 사람들 페이징한 예시랑 달라도 너무 다른데 

카드랑 댓글이 자동으로 같이 조회안된다는 점 때문에 따라할 수가 없다 ㅜㅜ 

findAll() 로 다시 해봐도 댓글이 같이 안 보인다...

어디서부터 잘못된걸까.!

내일 해설영상보면서 클론코딩 최소2번은 해야겠다 ㅋㅋㅋ...