|
|
@@ -25,21 +25,45 @@ public class SongController {
|
|
|
this.songService = songRepository;
|
|
|
}
|
|
|
|
|
|
- //################################################################_REST methods
|
|
|
|
|
|
|
|
|
- @RequestMapping(
|
|
|
- value = "/song",
|
|
|
- method = RequestMethod.GET,
|
|
|
- produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
+//#################################################################################_GET ALL SONGS
|
|
|
+
|
|
|
+// @RequestMapping(value = "/song", method = RequestMethod.GET) //, produces = MediaType.APPLICATION_JSON_VALUE
|
|
|
+// public ResponseEntity<List<Song>> getAllSongs()
|
|
|
+// {
|
|
|
+// List<Song> songs = songService.getAllSongs();
|
|
|
+// System.out.println("getAllsongs get request works" + songs.size() );
|
|
|
+//
|
|
|
+// // Print the riffs count for the first song
|
|
|
+// if (!songs.isEmpty()) {
|
|
|
+// Song firstSong = songs.get(0);
|
|
|
+// int riffCount = firstSong.getRiffs().size();
|
|
|
+// System.out.println("Riffs count for the first song: " + riffCount);
|
|
|
+// }
|
|
|
+//
|
|
|
+// return ResponseEntity.ok(songs);
|
|
|
+// }
|
|
|
+
|
|
|
+ @RequestMapping(value = "/song", method = RequestMethod.GET)
|
|
|
public ResponseEntity<List<Song>> getAllSongs() {
|
|
|
List<Song> songs = songService.getAllSongs();
|
|
|
- System.out.println("getAllsongs get request works" + songs.size());
|
|
|
+
|
|
|
+ // Load riffs for each song
|
|
|
+ for (Song song : songs) {
|
|
|
+ List<Riff> riffs = song.getRiffs();
|
|
|
+ riffs.size(); // Trigger lazy loading of riffs
|
|
|
+ song.setRiffs(riffs);
|
|
|
+ }
|
|
|
+
|
|
|
return ResponseEntity.ok(songs);
|
|
|
}
|
|
|
|
|
|
+//#################################################################################_GET A SONG
|
|
|
+
|
|
|
@RequestMapping(value="/{id}", method = RequestMethod.GET)
|
|
|
- public ResponseEntity<Song> getSongById(@PathVariable("id") long id) {
|
|
|
+ public ResponseEntity<Song> getSongById(@PathVariable("id") long id)
|
|
|
+ {
|
|
|
Optional<Song> song = Optional.ofNullable(songService.getSong(id));
|
|
|
if (song.isPresent()) {
|
|
|
return ResponseEntity.ok(song.get());
|
|
|
@@ -48,17 +72,27 @@ public class SongController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @RequestMapping(
|
|
|
- value = "/song",
|
|
|
- method = RequestMethod.POST)
|
|
|
- public ResponseEntity<Song> addSong(@RequestBody Song song) {
|
|
|
+//#################################################################################_ADD NEW SONG
|
|
|
+ @RequestMapping( value = "/song", method = RequestMethod.POST)
|
|
|
+ public ResponseEntity<Song> addSong(@RequestBody Song song)
|
|
|
+ {
|
|
|
+ List<Riff> riffs = song.getRiffs();
|
|
|
+ if (riffs != null) {
|
|
|
+ for (Riff riff : riffs) {
|
|
|
+ riff.setSong(song); // Set the song reference for each riff
|
|
|
+ }
|
|
|
+ }
|
|
|
Song addedSong = songService.addSong(song);
|
|
|
+ System.out.println("\n\nadding a new song: riffs:" + song.getRiffs().stream().count() + " \n\n");
|
|
|
return ResponseEntity.status(HttpStatus.CREATED).body(addedSong);
|
|
|
}
|
|
|
|
|
|
|
|
|
+//#################################################################################_UPDATE A SONG
|
|
|
+
|
|
|
@PutMapping("/{id}")
|
|
|
- public ResponseEntity<Song> updateSong(@PathVariable("id") long id, @RequestBody Song updatedSong) {
|
|
|
+ public ResponseEntity<Song> updateSong(@PathVariable("id") long id, @RequestBody Song updatedSong)
|
|
|
+ {
|
|
|
Optional<Song> song = Optional.ofNullable(songService.getSong(id));
|
|
|
if (song.isPresent()) {
|
|
|
Song existingSong = song.get();
|
|
|
@@ -72,8 +106,11 @@ public class SongController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//#################################################################################_DELETE A SONG
|
|
|
+
|
|
|
@DeleteMapping("/{id}")
|
|
|
- public ResponseEntity<Void> deleteSong(@PathVariable("id") long id) {
|
|
|
+ public ResponseEntity<Void> deleteSong(@PathVariable("id") long id)
|
|
|
+ {
|
|
|
Optional<Song> song = Optional.ofNullable(songService.getSong(id));
|
|
|
if (song.isPresent()) {
|
|
|
songService.removeSong(id);
|
|
|
@@ -83,20 +120,21 @@ public class SongController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//#################################################################################_GET RIFFS
|
|
|
|
|
|
@GetMapping("/{songId}/riffs")
|
|
|
- public ResponseEntity<Set<Riff>> getRiffs(@PathVariable long songId) {
|
|
|
- Optional<Song> optionalSong = Optional.ofNullable(songService.getSong(songId));
|
|
|
- if (optionalSong.isPresent()) {
|
|
|
- Song song = optionalSong.get();
|
|
|
- Set<Riff> riffs = song.getRiffs();
|
|
|
- return ResponseEntity.ok(riffs);
|
|
|
+ public ResponseEntity<List<Riff>> getRiffs(@PathVariable long songId)
|
|
|
+ {
|
|
|
+ Song song = songService.getSong(songId);
|
|
|
+ List<Riff> riffs = song.getRiffs();
|
|
|
+ if (riffs.isEmpty()) {
|
|
|
+ return ResponseEntity.noContent().build();
|
|
|
} else {
|
|
|
- return ResponseEntity.notFound().build();
|
|
|
+ return ResponseEntity.ok(riffs);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //################################################################_
|
|
|
+
|
|
|
|
|
|
|
|
|
}
|