Skip to content
Snippets Groups Projects
Commit ca2c7aa7 authored by 이명건's avatar 이명건
Browse files

react final connection

parent 265b9327
No related branches found
No related tags found
No related merge requests found
......@@ -12,7 +12,7 @@
<artifactId>myspringweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myspringweb</name>
<description>Favorite Music</description>
<description>favoritemusic</description>
<properties>
<java.version>17</java.version>
</properties>
......
......@@ -6,25 +6,23 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import jakarta.transaction.Transactional;
import kr.ajousw.myspringweb.dto.FavoriteMusicRequestDto;
import kr.ajousw.myspringweb.dto.MusicList;
import kr.ajousw.myspringweb.entity.FavoriteMusic;
import kr.ajousw.myspringweb.repository.FavoriteRepository;
import kr.ajousw.myspringweb.service.MusicService;
@RestController
public class MyWebcontroller {
RestTemplate restTemplate = new RestTemplate();
@Autowired
FavoriteRepository albumsRepo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
@RestController
public class MyWebController {
@Autowired
MusicService service;
......@@ -38,20 +36,21 @@ public class MyWebcontroller {
return service.searchMusic(name);
}
@GetMapping(value="/likes") //Get Favorite Music list from Database
@GetMapping(value="/likes")
public List<FavoriteMusic> getLikes(){
return service.getLikes();
}
@PostMapping(value="/likes")
@Transactional
public int postLikes(@RequestBody FavoriteMusicRequestDto favorite){
return service.saveFavorite(favorite);
}
@DeleteMapping(value = "/likes/{id}")
public String deleteFavorite(@PathVariable String id) {
public ResponseEntity<String> deleteLikes(@PathVariable String id) {
service.deleteFavorite(id);
return (id + " was deleted.");
return ResponseEntity.ok("Deleted ID: " + id);
}
}
......@@ -8,7 +8,6 @@ import lombok.ToString;
@Getter
@Setter
@ToString
public class FavoriteMusicRequestDto {
private String collectionId;
private String collectionType;
......@@ -17,6 +16,7 @@ public class FavoriteMusicRequestDto {
private String artistViewUrl;
private String collectionName;
private String collectionViewUrl;
public FavoriteMusic toEntity(){
FavoriteMusic music = new FavoriteMusic();
music.setCollectionId(this.collectionId);
......
package kr.ajousw.myspringweb.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import kr.ajousw.myspringweb.entity.FavoriteMusic;
public interface FavoriteRepository extends JpaRepository<FavoriteMusic, String> {
List<FavoriteMusic> findAll();
void deleteById(String id);
void deleteById(String collectionId);
}
......@@ -2,14 +2,14 @@ package kr.ajousw.myspringweb.service;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.transaction.Transactional;
import kr.ajousw.myspringweb.dto.FavoriteMusicRequestDto;
import kr.ajousw.myspringweb.dto.MusicList;
import kr.ajousw.myspringweb.entity.FavoriteMusic;
......@@ -22,9 +22,11 @@ import lombok.RequiredArgsConstructor;
public class MusicService {
private final FavoriteRepository albumsRepo;
RestTemplate restTemplate = new RestTemplate();
public MusicList searchMusic(String name){
try {
String url = "https://itunes.apple.com/search?term=" + name + "&entity=album";
RestTemplate restTemplate = new RestTemplate();
try {
String response = restTemplate.getForObject(url, String.class);
ObjectMapper mapper = new ObjectMapper();
MusicList list = mapper.readValue(response, MusicList.class);
......@@ -34,7 +36,6 @@ public class MusicService {
System.out.println(e.toString());
return null;
}
}
public List<FavoriteMusic> getLikes(){
......@@ -50,22 +51,18 @@ public class MusicService {
FavoriteMusic music = albumsRepo.save(favorite.toEntity());
if(music != null) {
return 1;
} else {
}
else {
return 0;
}
}
public void deleteFavorite(String id) {
try {
Optional<FavoriteMusic> favoriteMusicOptional = albumsRepo.findById(id);
if (favoriteMusicOptional.isPresent()) {
albumsRepo.deleteById(id);
} else {
throw new IllegalArgumentException("Invalid id: " + id);
}
} catch (Exception e) {
System.out.println(e.toString());
boolean exists = albumsRepo.existsById(id);
if (!exists) {
throw new IllegalArgumentException("Invalid favorite ID: " + id);
}
albumsRepo.deleteById(id);
}
}
Source diff could not be displayed: it is too large. Options to address this: view the blob.
<!doctype html>
<html>
<head>
<title> muiBasic </title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
</head>
<body>
<div id="app"></div>
<script src="./app.bundle.js" type="text/javascript"></script>
</body>
</html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment