|
|
@@ -0,0 +1,94 @@
|
|
|
+package pl.dmcs.jwoszczyk.controller;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import pl.dmcs.jwoszczyk.domain.Riff;
|
|
|
+import pl.dmcs.jwoszczyk.domain.Song;
|
|
|
+import pl.dmcs.jwoszczyk.repository.ISongRepository;
|
|
|
+import pl.dmcs.jwoszczyk.service.ISongService;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("song")
|
|
|
+public class SongController {
|
|
|
+
|
|
|
+ //################################################################_Dependency Injection
|
|
|
+ private ISongService songService;
|
|
|
+ @Autowired
|
|
|
+ public SongController(ISongService songRepository) {
|
|
|
+ this.songService = songRepository;
|
|
|
+ }
|
|
|
+
|
|
|
+ //################################################################_REST methods
|
|
|
+
|
|
|
+ @RequestMapping(method = RequestMethod.GET)
|
|
|
+ public ResponseEntity<List<Song>> getAllSongs() {
|
|
|
+ List<Song> songs = songService.getAllSongs();
|
|
|
+ return ResponseEntity.ok(songs);
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value="/{id}", method = RequestMethod.GET)
|
|
|
+ public ResponseEntity<Song> getSongById(@PathVariable("id") long id) {
|
|
|
+ Optional<Song> song = Optional.ofNullable(songService.getSong(id));
|
|
|
+ if (song.isPresent()) {
|
|
|
+ return ResponseEntity.ok(song.get());
|
|
|
+ } else {
|
|
|
+ return ResponseEntity.notFound().build();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(method = RequestMethod.POST)
|
|
|
+ public ResponseEntity<Song> addSong(@RequestBody Song song) {
|
|
|
+ Song addedSong = songService.addSong(song);
|
|
|
+ return ResponseEntity.status(HttpStatus.CREATED).body(addedSong);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @PutMapping("/{id}")
|
|
|
+ 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();
|
|
|
+ existingSong.setName(updatedSong.getName());
|
|
|
+ existingSong.setLink(updatedSong.getLink());
|
|
|
+ // Update other fields as needed
|
|
|
+ Song updatedSongEntity = songService.editSong(existingSong);
|
|
|
+ return ResponseEntity.ok(updatedSongEntity);
|
|
|
+ } else {
|
|
|
+ return ResponseEntity.notFound().build();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @DeleteMapping("/{id}")
|
|
|
+ public ResponseEntity<Void> deleteSong(@PathVariable("id") long id) {
|
|
|
+ Optional<Song> song = Optional.ofNullable(songService.getSong(id));
|
|
|
+ if (song.isPresent()) {
|
|
|
+ songService.removeSong(id);
|
|
|
+ return ResponseEntity.noContent().build();
|
|
|
+ } else {
|
|
|
+ return ResponseEntity.notFound().build();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @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);
|
|
|
+ } else {
|
|
|
+ return ResponseEntity.notFound().build();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //################################################################_
|
|
|
+
|
|
|
+
|
|
|
+}
|