Skip to content

Commit

Permalink
Merge pull request #118 from go-park-mail-ru/NM-117
Browse files Browse the repository at this point in the history
Nm 117
  • Loading branch information
MatiXxD authored Dec 20, 2024
2 parents 32abefe + 706cf83 commit dd838f9
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 72 deletions.
110 changes: 55 additions & 55 deletions internal/db/postgres/migrations/20241020124735_seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
INSERT INTO genre
(name, rus_name)
VALUES
('Pop', 'Поп'),
('Rap', 'Рэп'),
('Rock', 'Рок'),
('Classical', 'Классика'),
('Country', 'Кантри'),
('Hip-Hop', 'Хип-хоп'),
('Indie', 'Инди'),
('Alternative', 'Альтернатива'),
('IDM', 'Электроника');
('pop', 'Поп'),
('rap', 'Рэп'),
('rock', 'Рок'),
('classical', 'Классика'),
('country', 'Кантри'),
('hip-hop', 'Хип-хоп'),
('indie', 'Инди'),
('alternative', 'Альтернатива'),
('idm', 'Электроника');

INSERT INTO artist
(name, bio, country, image)
Expand Down Expand Up @@ -80,54 +80,54 @@ VALUES
('The Eraser', 295, 'thom_yorke_the_eraser_5.mp3', 'thom_yorke_the_eraser.webp', (SELECT id FROM artist WHERE name = 'Thom Yorke'), (SELECT id FROM album WHERE name = 'The Eraser'), 5);

INSERT INTO genre_artist (genre_id, artist_id) VALUES
((SELECT id FROM genre WHERE name = 'Rap'), (SELECT id FROM artist WHERE name = 'EKKSTACY')),
((SELECT id FROM genre WHERE name = 'Rock'), (SELECT id FROM artist WHERE name = 'Sueco')),
((SELECT id FROM genre WHERE name = 'Pop'), (SELECT id FROM artist WHERE name = 'Причастие')),
((SELECT id FROM genre WHERE name = 'Indie'), (SELECT id FROM artist WHERE name = 'Брюки бри')),
((SELECT id FROM genre WHERE name = 'Hip-Hop'), (SELECT id FROM artist WHERE name = 'HXVRMXN')),
((SELECT id FROM genre WHERE name = 'Alternative'), (SELECT id FROM artist WHERE name = 'YONAKA')),
((SELECT id FROM genre WHERE name = 'Rap'), (SELECT id FROM artist WHERE name = 'Yung Lean')),
((SELECT id FROM genre WHERE name = 'IDM'), (SELECT id FROM artist WHERE name = 'Thom Yorke'));
((SELECT id FROM genre WHERE name = 'rap'), (SELECT id FROM artist WHERE name = 'EKKSTACY')),
((SELECT id FROM genre WHERE name = 'rock'), (SELECT id FROM artist WHERE name = 'Sueco')),
((SELECT id FROM genre WHERE name = 'pop'), (SELECT id FROM artist WHERE name = 'Причастие')),
((SELECT id FROM genre WHERE name = 'indie'), (SELECT id FROM artist WHERE name = 'Брюки бри')),
((SELECT id FROM genre WHERE name = 'hip-hop'), (SELECT id FROM artist WHERE name = 'HXVRMXN')),
((SELECT id FROM genre WHERE name = 'alternative'), (SELECT id FROM artist WHERE name = 'YONAKA')),
((SELECT id FROM genre WHERE name = 'rap'), (SELECT id FROM artist WHERE name = 'Yung Lean')),
((SELECT id FROM genre WHERE name = 'idm'), (SELECT id FROM artist WHERE name = 'Thom Yorke'));

INSERT INTO genre_track (genre_id, track_id) VALUES
((SELECT id FROM genre WHERE name = 'Rap'), (SELECT id FROM track WHERE name = 'i just want to hide my face')),
((SELECT id FROM genre WHERE name = 'Rap'), (SELECT id FROM track WHERE name = 'im so happy')),
((SELECT id FROM genre WHERE name = 'Rap'), (SELECT id FROM track WHERE name = 'i wish you were pretty on the inside')),
((SELECT id FROM genre WHERE name = 'Rap'), (SELECT id FROM track WHERE name = 'christian death')),
((SELECT id FROM genre WHERE name = 'Rap'), (SELECT id FROM track WHERE name = 'i want to die in your arms')),
((SELECT id FROM genre WHERE name = 'Rock'), (SELECT id FROM track WHERE name = 'Wreck')),
((SELECT id FROM genre WHERE name = 'Rock'), (SELECT id FROM track WHERE name = 'Wanna Feel Something')),
((SELECT id FROM genre WHERE name = 'Rock'), (SELECT id FROM track WHERE name = '452AM')),
((SELECT id FROM genre WHERE name = 'Rock'), (SELECT id FROM track WHERE name = 'Bad Idea')),
((SELECT id FROM genre WHERE name = 'Rock'), (SELECT id FROM track WHERE name = 'Never Even Left')),
((SELECT id FROM genre WHERE name = 'Pop'), (SELECT id FROM track WHERE name = 'Бренди')),
((SELECT id FROM genre WHERE name = 'Pop'), (SELECT id FROM track WHERE name = 'Вечеринка')),
((SELECT id FROM genre WHERE name = 'Pop'), (SELECT id FROM track WHERE name = 'Город')),
((SELECT id FROM genre WHERE name = 'Pop'), (SELECT id FROM track WHERE name = 'Мокрую щеку')),
((SELECT id FROM genre WHERE name = 'Pop'), (SELECT id FROM track WHERE name = 'Пустое утро')),
((SELECT id FROM genre WHERE name = 'Indie'), (SELECT id FROM track WHERE name = 'Последнее свидание')),
((SELECT id FROM genre WHERE name = 'Indie'), (SELECT id FROM track WHERE name = 'Мини купер')),
((SELECT id FROM genre WHERE name = 'Indie'), (SELECT id FROM track WHERE name = 'Мамина дочь')),
((SELECT id FROM genre WHERE name = 'Indie'), (SELECT id FROM track WHERE name = 'Права')),
((SELECT id FROM genre WHERE name = 'Indie'), (SELECT id FROM track WHERE name = 'Ы')),
((SELECT id FROM genre WHERE name = 'Hip-Hop'), (SELECT id FROM track WHERE name = 'Eclipse')),
((SELECT id FROM genre WHERE name = 'Hip-Hop'), (SELECT id FROM track WHERE name = 'Ecoboost')),
((SELECT id FROM genre WHERE name = 'Hip-Hop'), (SELECT id FROM track WHERE name = 'South')),
((SELECT id FROM genre WHERE name = 'Alternative'), (SELECT id FROM track WHERE name = 'Don''t Wait ''Til Tomorrow')),
((SELECT id FROM genre WHERE name = 'Alternative'), (SELECT id FROM track WHERE name = 'Creature')),
((SELECT id FROM genre WHERE name = 'Alternative'), (SELECT id FROM track WHERE name = 'Fired Up')),
((SELECT id FROM genre WHERE name = 'Alternative'), (SELECT id FROM track WHERE name = 'Lose Our Heads')),
((SELECT id FROM genre WHERE name = 'Alternative'), (SELECT id FROM track WHERE name = 'Wake Up')),
((SELECT id FROM genre WHERE name = 'Rap'), (SELECT id FROM track WHERE name = 'Agony')),
((SELECT id FROM genre WHERE name = 'Rap'), (SELECT id FROM track WHERE name = 'Drop It / Scooter')),
((SELECT id FROM genre WHERE name = 'Rap'), (SELECT id FROM track WHERE name = 'Push / Lost Weekend')),
((SELECT id FROM genre WHERE name = 'Rap'), (SELECT id FROM track WHERE name = 'Red Bottom Sky')),
((SELECT id FROM genre WHERE name = 'Rap'), (SELECT id FROM track WHERE name = 'Muddy Sea')),
((SELECT id FROM genre WHERE name = 'IDM'), (SELECT id FROM track WHERE name = 'The Clock')),
((SELECT id FROM genre WHERE name = 'IDM'), (SELECT id FROM track WHERE name = 'Cymbal Rush')),
((SELECT id FROM genre WHERE name = 'IDM'), (SELECT id FROM track WHERE name = 'Atoms For Peace')),
((SELECT id FROM genre WHERE name = 'IDM'), (SELECT id FROM track WHERE name = 'Analyze')),
((SELECT id FROM genre WHERE name = 'IDM'), (SELECT id FROM track WHERE name = 'The Eraser'));
((SELECT id FROM genre WHERE name = 'rap'), (SELECT id FROM track WHERE name = 'i just want to hide my face')),
((SELECT id FROM genre WHERE name = 'rap'), (SELECT id FROM track WHERE name = 'im so happy')),
((SELECT id FROM genre WHERE name = 'rap'), (SELECT id FROM track WHERE name = 'i wish you were pretty on the inside')),
((SELECT id FROM genre WHERE name = 'rap'), (SELECT id FROM track WHERE name = 'christian death')),
((SELECT id FROM genre WHERE name = 'rap'), (SELECT id FROM track WHERE name = 'i want to die in your arms')),
((SELECT id FROM genre WHERE name = 'rock'), (SELECT id FROM track WHERE name = 'Wreck')),
((SELECT id FROM genre WHERE name = 'rock'), (SELECT id FROM track WHERE name = 'Wanna Feel Something')),
((SELECT id FROM genre WHERE name = 'rock'), (SELECT id FROM track WHERE name = '452AM')),
((SELECT id FROM genre WHERE name = 'rock'), (SELECT id FROM track WHERE name = 'Bad Idea')),
((SELECT id FROM genre WHERE name = 'rock'), (SELECT id FROM track WHERE name = 'Never Even Left')),
((SELECT id FROM genre WHERE name = 'pop'), (SELECT id FROM track WHERE name = 'Бренди')),
((SELECT id FROM genre WHERE name = 'pop'), (SELECT id FROM track WHERE name = 'Вечеринка')),
((SELECT id FROM genre WHERE name = 'pop'), (SELECT id FROM track WHERE name = 'Город')),
((SELECT id FROM genre WHERE name = 'pop'), (SELECT id FROM track WHERE name = 'Мокрую щеку')),
((SELECT id FROM genre WHERE name = 'pop'), (SELECT id FROM track WHERE name = 'Пустое утро')),
((SELECT id FROM genre WHERE name = 'indie'), (SELECT id FROM track WHERE name = 'Последнее свидание')),
((SELECT id FROM genre WHERE name = 'indie'), (SELECT id FROM track WHERE name = 'Мини купер')),
((SELECT id FROM genre WHERE name = 'indie'), (SELECT id FROM track WHERE name = 'Мамина дочь')),
((SELECT id FROM genre WHERE name = 'indie'), (SELECT id FROM track WHERE name = 'Права')),
((SELECT id FROM genre WHERE name = 'indie'), (SELECT id FROM track WHERE name = 'Ы')),
((SELECT id FROM genre WHERE name = 'hip-hop'), (SELECT id FROM track WHERE name = 'Eclipse')),
((SELECT id FROM genre WHERE name = 'hip-hop'), (SELECT id FROM track WHERE name = 'Ecoboost')),
((SELECT id FROM genre WHERE name = 'hip-hop'), (SELECT id FROM track WHERE name = 'South')),
((SELECT id FROM genre WHERE name = 'alternative'), (SELECT id FROM track WHERE name = 'Don''t Wait ''Til Tomorrow')),
((SELECT id FROM genre WHERE name = 'alternative'), (SELECT id FROM track WHERE name = 'Creature')),
((SELECT id FROM genre WHERE name = 'alternative'), (SELECT id FROM track WHERE name = 'Fired Up')),
((SELECT id FROM genre WHERE name = 'alternative'), (SELECT id FROM track WHERE name = 'Lose Our Heads')),
((SELECT id FROM genre WHERE name = 'alternative'), (SELECT id FROM track WHERE name = 'Wake Up')),
((SELECT id FROM genre WHERE name = 'rap'), (SELECT id FROM track WHERE name = 'Agony')),
((SELECT id FROM genre WHERE name = 'rap'), (SELECT id FROM track WHERE name = 'Drop It / Scooter')),
((SELECT id FROM genre WHERE name = 'rap'), (SELECT id FROM track WHERE name = 'Push / Lost Weekend')),
((SELECT id FROM genre WHERE name = 'rap'), (SELECT id FROM track WHERE name = 'Red Bottom Sky')),
((SELECT id FROM genre WHERE name = 'rap'), (SELECT id FROM track WHERE name = 'Muddy Sea')),
((SELECT id FROM genre WHERE name = 'idm'), (SELECT id FROM track WHERE name = 'The Clock')),
((SELECT id FROM genre WHERE name = 'idm'), (SELECT id FROM track WHERE name = 'Cymbal Rush')),
((SELECT id FROM genre WHERE name = 'idm'), (SELECT id FROM track WHERE name = 'Atoms For Peace')),
((SELECT id FROM genre WHERE name = 'idm'), (SELECT id FROM track WHERE name = 'Analyze')),
((SELECT id FROM genre WHERE name = 'idm'), (SELECT id FROM track WHERE name = 'The Eraser'));

INSERT INTO csat (topic) VALUES
('Общие');
Expand Down
1 change: 1 addition & 0 deletions microservices/track/delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ type Handlers interface {
GetFavoriteTracksCount(response http.ResponseWriter, request *http.Request)
GetTracksFromPlaylist(response http.ResponseWriter, request *http.Request)
GetPopular(response http.ResponseWriter, request *http.Request)
GetTracksByGenre(response http.ResponseWriter, request *http.Request)
}
33 changes: 33 additions & 0 deletions microservices/track/delivery/http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"strconv"
"strings"

uuid "github.com/google/uuid"
"github.com/mailru/easyjson"
Expand Down Expand Up @@ -504,3 +505,35 @@ func (handlers *trackHandlers) GetPopular(response http.ResponseWriter, request
return
}
}

func (handlers *trackHandlers) GetTracksByGenre(response http.ResponseWriter, request *http.Request) {
requestID := request.Context().Value(utils.RequestIDKey{})
vars := mux.Vars(request)
genre := strings.ToLower(vars["genre"])

tracks, err := handlers.usecase.GetTracksByGenre(request.Context(), genre)
if err != nil {
handlers.logger.Error(fmt.Sprintf("Failed to get tracks: %v", err), requestID)
utils.JSONError(response, http.StatusInternalServerError, fmt.Sprintf("Failed to get tracks: %v", err))
return
} else if len(tracks) == 0 {
utils.JSONError(response, http.StatusNotFound, "No tracks were found")
return
}

response.Header().Set("Content-Type", "application/json")
rawBytes, err := easyjson.Marshal(dto.TrackDTOs(tracks))
if err != nil {
handlers.logger.Error(fmt.Sprintf("Failed to encode tracks: %v", err), requestID)
utils.JSONError(response, http.StatusInternalServerError, fmt.Sprintf("Failed to encode tracks: %v", err))
return
}

response.WriteHeader(http.StatusOK)
_, err = response.Write(rawBytes)
if err != nil {
handlers.logger.Error(fmt.Sprintf("Failed to write response: %v", err), requestID)
utils.JSONError(response, http.StatusInternalServerError, "Write response fail")
return
}
}
1 change: 1 addition & 0 deletions microservices/track/delivery/http/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func BindRoutes(s *httpServer.Server, artistClient artistService.ArtistServiceCl
s.MUX.HandleFunc("/api/v1/tracks/byArtistId/{artistId:[0-9]+}", trackHandleres.GetAllByArtistID).Methods("GET")
s.MUX.HandleFunc("/api/v1/tracks/byAlbumId/{albumId:[0-9]+}", trackHandleres.GetAllByAlbumID).Methods("GET")
s.MUX.HandleFunc("/api/v1/tracks/byPlaylistId/{playlistId:[0-9]+}", trackHandleres.GetTracksFromPlaylist).Methods("GET")
s.MUX.HandleFunc("/api/v1/tracks/byGenre/{genre}", trackHandleres.GetTracksByGenre).Methods("GET")

s.MUX.Handle(
"/api/v1/tracks/favorite/byUser/{userID}",
Expand Down
15 changes: 15 additions & 0 deletions microservices/track/mock/repository_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions microservices/track/mock/usecase_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions microservices/track/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ type Repo interface {
GetFavoriteTracksCount(ctx context.Context, userID uuid.UUID) (uint64, error)
GetTracksFromPlaylist(ctx context.Context, playlistID uint64) ([]*models.PlaylistTrack, error)
GetPopular(ctx context.Context) ([]*models.Track, error)
GetTracksByGenre(ctx context.Context, genre string) ([]*models.Track, error)
}
32 changes: 32 additions & 0 deletions microservices/track/repository/pg_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,35 @@ func (r *TrackRepository) GetPopular(ctx context.Context) ([]*models.Track, erro

return tracks, nil
}

func (r *TrackRepository) GetTracksByGenre(ctx context.Context, genre string) ([]*models.Track, error) {
var tracks []*models.Track
rows, err := r.db.QueryContext(ctx, getTracksByGenre, genre)
if err != nil {
return nil, errors.Wrap(err, "GetTracksByGenre.Query")
}
defer rows.Close()

for rows.Next() {
track := &models.Track{}
err := rows.Scan(
&track.ID,
&track.Name,
&track.Duration,
&track.FilePath,
&track.Image,
&track.ArtistID,
&track.AlbumID,
&track.OrderInAlbum,
&track.ReleaseDate,
&track.CreatedAt,
&track.UpdatedAt,
)
if err != nil {
return nil, errors.Wrap(err, "GetTracksByGenre.Query")
}
tracks = append(tracks, track)
}

return tracks, nil
}
9 changes: 8 additions & 1 deletion microservices/track/repository/sql_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,12 @@ const (
ORDER BY
COUNT(ft.track_id) DESC
LIMIT 50;
`
`

getTracksByGenre = `
SELECT t.id AS id, t.name AS name, duration, filepath, image, artist_id, album_id, track_order_in_album, release_date, t.created_at AS created_at, t.updated_at AS updated_at
FROM track AS t
JOIN genre_track gt ON t.id = gt.track_id
JOIN genre g ON gt.genre_id = g.id
WHERE g.name = $1`
)
1 change: 1 addition & 0 deletions microservices/track/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ type Usecase interface {
ConvertTrackToDTO(ctx context.Context, track *models.Track) (*dto.TrackDTO, error)
GetTracksFromPlaylist(ctx context.Context, playlistID uint64) ([]*dto.TrackDTO, error)
GetPopular(ctx context.Context) ([]*dto.TrackDTO, error)
GetTracksByGenre(ctx context.Context, genre string) ([]*dto.TrackDTO, error)
}
Loading

0 comments on commit dd838f9

Please sign in to comment.