Created
August 8, 2018 18:57
-
-
Save mesmacosta/37f4ef6732bcd9d35f5b7b05de2e4702 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dojo.springboot.rest.template.controllers; | |
import java.util.Arrays; | |
import java.util.List; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.http.MediaType; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.CrossOrigin; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestParam; | |
import org.springframework.web.bind.annotation.RestController; | |
import org.springframework.web.client.RestTemplate; | |
import com.dojo.springboot.rest.template.entity.Musica; | |
import com.dojo.springboot.rest.template.entity.Playlist; | |
import com.dojo.springboot.rest.template.entity.Usuario; | |
@RestController | |
@CrossOrigin | |
@RequestMapping("/api/playlists") | |
public class PlaylistController { | |
@Value("${rest.musica.endpoint}") | |
private String endpoint; | |
@Autowired | |
private RestTemplate restTemplate; | |
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) | |
public ResponseEntity<Playlist> getByUser(@RequestParam("user") String user) { | |
return ResponseEntity.ok(mockPlaylist(user)); | |
} | |
private Playlist mockPlaylist(String user) { | |
return Playlist.builder().id("1").musicas(fetchMusicas()).usuario(Usuario.builder().id("1").nome(user).build()) | |
.build(); | |
} | |
private List<Musica> fetchMusicas() { | |
Musica musica = restTemplate.getForObject(endpoint, Musica.class); | |
return Arrays.asList(musica); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment