Skip to content

Commit

Permalink
Merge pull request #29 from No-Country-simulation/ale-back
Browse files Browse the repository at this point in the history
Ale back
  • Loading branch information
alelex10 authored Dec 2, 2024
2 parents c6f9767 + b27658b commit 633cb8f
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.web.app.dto.ExtendedBaseResponse;
import com.web.app.dto.search.SearchDeezerResponse;
import com.web.app.dto.search.db.SearchDBResponseProjection;
import com.web.app.service.impl.SearchServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
Expand All @@ -12,15 +13,17 @@
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "Buscador", description = "Buscar canciones, album, artist tanto desde la API de Deezar como la nuestra")
@RestController
@RequestMapping("/search")
@RequiredArgsConstructor
public class SearchController {
public final SearchServiceImpl searchService;
// Implementación de los métodos para buscar canciones, albums y artistas
@Operation(summary = "Buscador",
description = "Al Buscar canciones, album, artist, hacerlo entre comillas"
@Operation(summary = "Buscador API Deezer",
description ="Al Buscar canciones, album, artist, hacerlo entre comillas"
)
@ApiResponses( value = {
@ApiResponse(
Expand All @@ -32,10 +35,28 @@ public class SearchController {
})
})
@GetMapping
public ExtendedBaseResponse<SearchDeezerResponse> findTracksByAlbumId(
public ExtendedBaseResponse<SearchDeezerResponse> findBySearchDeezer(
@RequestParam(required = false) String artist,
@Schema(example="'nueva era'")
@RequestParam(required = false) String track,
@RequestParam(required = false) String album) {
return searchService.searchDeezaer(artist,track,album);
}

@Operation(summary = "Buscador DB http://localhost:8080/search/{busqueda}",
description ="Al Buscar canciones, album, artist, puede buscar por " +
"palabras parciles ej: nuev o nueva y te un album, cancion o artista " +
"que coincidan en la DB"
)
@ApiResponses( value = {
@ApiResponse(
responseCode = "200",
description = "Busqueda por DB.")
})
@GetMapping("/{search}")
public ExtendedBaseResponse<List<SearchDBResponseProjection>> findBySearchDB(
@Schema(example = "nuev")
@PathVariable() String search) {
return searchService.searchDB(search);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.web.app.dto.search.db;

public record SearchDBResponse(
String track,
String album,
String picture_url,
String type,
Long id
) {
// public SearchDBResponse(String track, String album, String pictureUrl, String type, Long id) {
// this.track = track;
// this.album = album;
// this.pictureUrl = pictureUrl;
// this.type = type;
// this.id = id;
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.web.app.dto.search.db;

import io.swagger.v3.oas.annotations.media.Schema;

public interface SearchDBResponseProjection {
@Schema(example = "Nueva Era")
String getTrack();
@Schema(example = "AMERI")
String getAlbum();
@Schema(example = "DUKI")
String getArtist();
@Schema(example = "https://api.deezer.com/album/664113131/image")
String getPicture_url();
@Schema(example = "track")
String getType();
@Schema(example = "3069012151")
Long getId();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,63 @@
package com.web.app.repository;

import com.web.app.dto.search.db.SearchDBResponseProjection;
import com.web.app.model.Track;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface TrackRepository extends JpaRepository<Track, Long> {
List<Track> findAllByAlbumId(Long albumId);

@Query(value = """
SELECT
t.name AS track,
al.name AS album,
ar.name AS artist,
al.picture_url,
'track' AS type,
t.id AS id
FROM
Track t
JOIN
Album al ON t.album_id = al.id
JOIN
Artist ar ON al.artist_id = ar.id
WHERE
t.name LIKE %:searchTerm%
UNION SELECT
NULL AS track,
al.name AS album,
NULL AS artist,
al.picture_url,
'album' AS type,
al.id AS id
FROM
Track t
JOIN
Album al ON t.album_id = al.id
JOIN
Artist ar ON al.artist_id = ar.id
WHERE
al.name LIKE %:searchTerm%
UNION SELECT
NULL AS track,
NULL AS album,
ar.name AS artist,
ar.pictureUrl AS picture_url,
'artist' AS type,
ar.id AS id
FROM
Track t
JOIN
Album al ON t.album_id = al.id
JOIN
Artist ar ON al.artist_id = ar.id
WHERE
ar.name LIKE %:searchTerm%;
""", nativeQuery = true)
List<SearchDBResponseProjection> findMusicBySearchTerm(@Param("searchTerm") String searchTerm);

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
package com.web.app.service.impl;

import com.web.app.dto.search.db.SearchDBResponse;
import com.web.app.dto.search.db.SearchDBResponseProjection;
import com.web.app.repository.TrackRepository;
import com.web.app.service.api.DeezerClient;
import com.web.app.dto.BaseResponse;
import com.web.app.dto.ExtendedBaseResponse;
import com.web.app.dto.search.SearchDeezerResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@AllArgsConstructor
@Service
public class SearchServiceImpl {
public final DeezerClient deezerClient;
private final DeezerClient deezerClient;
private final TrackRepository trackRepository;

public ExtendedBaseResponse<SearchDeezerResponse> searchDeezaer(String artist, String track, String album) {
SearchDeezerResponse response=deezerClient.searchDeezerAPI(artist, track, album);
SearchDeezerResponse response = deezerClient.searchDeezerAPI(artist, track, album);
return ExtendedBaseResponse.of(BaseResponse.ok("Busqueda por Deezer Exitoso"), response);
}

public ExtendedBaseResponse<List<SearchDBResponseProjection>> searchDB(String data) {
List<SearchDBResponseProjection> response = trackRepository.findMusicBySearchTerm(data);
System.out.println("canciones"+response);
return ExtendedBaseResponse.of(BaseResponse.ok("Busqueda por Base de Datos Exitosa"), response);
}
}

0 comments on commit 633cb8f

Please sign in to comment.