package nl.orangeflamingo.controller;

import com.fasterxml.jackson.annotation.JsonView;
import nl.orangeflamingo.domain.Song;
import nl.orangeflamingo.domain.SongRepository;
import nl.orangeflamingo.domain.SongSpecs;
import nl.orangeflamingo.domain.View;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SongController {

    private final SongRepository songRepository;

    private final Logger log = LoggerFactory.getLogger(SongController.class);

    @Autowired
    public SongController(SongRepository songRepository) {
        this.songRepository = songRepository;
    }

    @RequestMapping("/song/{id}")
    @JsonView(View.Detail.class)
    public Song getSongById(@PathVariable long id) {
        return songRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Song not found for id " + id));
    }

    @RequestMapping(value = "/song/artist/{artist}", method = RequestMethod.GET)
    @JsonView(View.Summary.class)
    public List<Song> findSongsByArtist(@PathVariable("artist") String artist) {
        return songRepository.findByArtistLikeIgnoreCase("%" + artist + "%");
    }

    @RequestMapping(value = "/song/title/{title}", method = RequestMethod.GET)
    @JsonView(View.Summary.class)
    public List<Song> findSongsByTitle(@PathVariable("title") String title) {
         return songRepository.findByTitleLikeIgnoreCase("%" + title + "%");
    }

    @RequestMapping(value = "/song/{query}", method = RequestMethod.GET)
    @JsonView(View.Summary.class)
    public List<Song> findSongsByQuery(@PathVariable("query") String query) {
        return songRepository.findAll(SongSpecs.songWithArtistOrTitleLike(query));
    }

    @RequestMapping(value = "/song", params = { "page", "size" }, method = RequestMethod.GET)
    public Page<Song> findSongsByPage(@RequestParam("page") int page, @RequestParam("size") int size) {
        log.info("Getting songs for page {} and size {}", page, size);
        return songRepository.findAll(PageRequest.of(page, size));
    }

    @RequestMapping(value = "/song/{query}", params = { "page", "size" }, method = RequestMethod.GET)
    public Page<Song> findSongsByQueryAndPage(@PathVariable("query") String query, @RequestParam("page") int page,
                                              @RequestParam("size") int size) {
        return songRepository.findAll(SongSpecs.songWithArtistOrTitleLike(query), PageRequest.of(page, size));
    }
}

@@ -588,16 +588,61 @@
 ller %7B%0A%0A
+    private static final int MAX_SIZE = 30;%0A%0A
     priv
@@ -682,16 +682,16 @@
 sitory;%0A
-
 %0A    pri
@@ -1335,38 +1335,58 @@
 tist) %7B%0A        
-return
+List%3CSong%3E songsByArtist =
  songRepository.
@@ -1425,32 +1425,107 @@
  artist + %22%25%22);%0A
+        return songsByArtist.subList(0, getMaxSize(songsByArtist.size()));%0A
     %7D%0A%0A    @Requ
@@ -1710,31 +1710,49 @@
 ) %7B%0A        
- return
+List%3CSong%3E songsByTitle =
  songReposit
@@ -1797,24 +1797,97 @@
 tle + %22%25%22);%0A
+        return songsByTitle.subList(0, getMaxSize(songsByTitle.size()));%0A
     %7D%0A%0A    @
@@ -2066,38 +2066,57 @@
 uery) %7B%0A        
-return
+List%3CSong%3E songsByQuery =
  songRepository.
@@ -2164,24 +2164,97 @@
 ke(query));%0A
+        return songsByQuery.subList(0, getMaxSize(songsByQuery.size()));%0A
     %7D%0A%0A    @
@@ -2567,32 +2567,61 @@
 Request.of(page,
+ size %3E MAX_SIZE ? MAX_SIZE :
  size));%0A    %7D%0A%0A
@@ -3020,24 +3020,149 @@
 f(page, size
-))
+ %3E MAX_SIZE ? MAX_SIZE : size));%0A    %7D%0A%0A    private int getMaxSize(int size) %7B%0A        return size %3E MAX_SIZE ? MAX_SIZE : size
 ;%0A    %7D%0A%7D%0A

Add static max size for lists