Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toying #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ ruby '2.6.3'
gem 'rails', '~> 5.2.3'
gem 'puma', '~> 3.11'
gem 'bootsnap', '>= 1.1.0', require: false
gem 'rack-cors', '~> 0.3.1'
gem 'rspotify'
gem 'pg', '~> 1.1', '>= 1.1.4'
gem 'active_model_serializers', '~> 0.10.8'
gem 'kaminari'

group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
Expand Down
24 changes: 24 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ GEM
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
active_model_serializers (0.10.10)
actionpack (>= 4.1, < 6.1)
activemodel (>= 4.1, < 6.1)
case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
activejob (5.2.3)
activesupport (= 5.2.3)
globalid (>= 0.3.6)
Expand All @@ -62,6 +67,8 @@ GEM
msgpack (~> 1.0)
builder (3.2.3)
byebug (11.0.1)
case_transform (0.2)
activesupport
concurrent-ruby (1.1.5)
crass (1.0.4)
database_cleaner (1.7.0)
Expand All @@ -88,7 +95,20 @@ GEM
domain_name (~> 0.5)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
jsonapi-renderer (0.2.2)
jwt (2.2.1)
kaminari (1.1.1)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.1.1)
kaminari-activerecord (= 1.1.1)
kaminari-core (= 1.1.1)
kaminari-actionview (1.1.1)
actionview
kaminari-core (= 1.1.1)
kaminari-activerecord (1.1.1)
activerecord
kaminari-core (= 1.1.1)
kaminari-core (1.1.1)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -139,6 +159,7 @@ GEM
public_suffix (3.1.0)
puma (3.12.1)
rack (2.0.7)
rack-cors (0.3.1)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (5.2.3)
Expand Down Expand Up @@ -246,15 +267,18 @@ PLATFORMS
x64-mingw32

DEPENDENCIES
active_model_serializers (~> 0.10.8)
bootsnap (>= 1.1.0)
byebug
database_cleaner
factory_bot_rails
faker!
figaro
kaminari
listen (>= 3.0.5, < 3.2)
pg (~> 1.1, >= 1.1.4)
puma (~> 3.11)
rack-cors (~> 0.3.1)
rails (~> 5.2.3)
rspec-rails (~> 3.8)
rspotify
Expand Down
30 changes: 18 additions & 12 deletions app/controllers/api/v1/albums_controller.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
module Api
module V1
class AlbumsController < ApplicationController
#get all songs of an album
def get_songs
songs = Album.find(params[:id]).songs.select(:name, :spotify_url, :preview_url, :duration_ms, :explicit).as_json(:except => :id)
if !songs.empty?
render json: {status: 'SUCCESS', data: songs}, status: :ok
else
render json: {status: 'FAILED', data: {name: 'Album not found.'}}, status: :unprocessable_entity
end
end
class Api::V1::AlbumsController < ApplicationController
#get all songs of an album
=begin
def get_songs
songs = Album.find(params[:id]).songs.select(:name, :spotify_url, :preview_url, :duration_ms, :explicit).as_json(:except => :id)
if !songs.empty?
render json: {status: 'SUCCESS', data: songs}, status: :ok
else
render json: {status: 'FAILED', data: {name: 'Album not found.'}}, status: :unprocessable_entity
end
end
=end
def index
parameters = AlbumParams.new
parameters.parse_query_string(params)

render json: Album.get_all(parameters),
status: :ok
end

end
48 changes: 27 additions & 21 deletions app/controllers/api/v1/artists_controller.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
module Api
module V1
class ArtistsController < ApplicationController
#Get artists and order them by popularity
def index
artists = Artist.select(:id, :name, :image, :genres, :popularity, :spotify_url).order('popularity DESC')
class Api::V1::ArtistsController < ApplicationController
#Get artists and order them by popularity
=begin
def index
artists = Artist.select(:id, :name, :image, :genres, :popularity, :spotify_url).order('popularity DESC')

if artists.any?
render json: {status: 'SUCCESS', data: artists}, status: :ok
else
render json: {status: 'FAILED', data: 'No artists saved'}, status: :unprocessable_entity
end
end
if artists.any?
render json: {status: 'SUCCESS', data: artists}, status: :ok
else
render json: {status: 'FAILED', data: 'No artists saved'}, status: :unprocessable_entity
end
end

# Get all artist albums
def get_albums
albums = Artist.find(params[:id]).albums.select(:id, :name, :image, :spotify_url, :total_tracks)
if !albums.empty?
render json: {status: 'SUCCESS', data: albums}, status: :ok
else
render json: {status: 'FAILED', data: {name: 'Artist not found.'}}, status: :unprocessable_entity
end
end
# Get all artist albums
def get_albums
albums = Artist.find(params[:id]).albums.select(:id, :name, :image, :spotify_url, :total_tracks)
if !albums.empty?
render json: {status: 'SUCCESS', data: albums}, status: :ok
else
render json: {status: 'FAILED', data: {name: 'Artist not found.'}}, status: :unprocessable_entity
end
end
=end
def index
parameters = AlbumParams.new
parameters.parse_query_string(params)

render json: Artist.get_all(parameters),
status: :ok
end

end
19 changes: 0 additions & 19 deletions app/controllers/api/v1/genres_controller.rb

This file was deleted.

30 changes: 30 additions & 0 deletions app/controllers/api/v1/songs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Api::V1::SongsController < ApplicationController
# get ramdom song by genre
=begin
def song_by_genre
artists = Artist.where("genres ~* ?", params[:genre_name])
if !artists.empty?
song = artists.sample.albums.sample.songs.sample.as_json(:except => [:id, :album_id, :updated_at, :created_at, :spotify_id])
if song.nil?
song = artists.sample.albums.sample.songs.sample.as_json(:except => [:id, :album_id, :updated_at, :created_at, :spotify_id])
end
render json: {status: 'SUCCESS', data: song}, status: :ok
else
render json: {status: 'FAILED', data: {name: 'Genre not found.'}}, status: :unprocessable_entity
end
end
=end
def index
parameters = SongParams.new
parameters.parse_query_string(params)
render json: Song.get_all(parameters),
status: :ok
end

def random_song
render json: Song.by_genre_name_eq(params[:genre_name]).take,
serializer: SongSerializer,
status: :ok
end

end
21 changes: 21 additions & 0 deletions app/models/album.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
class Album < ApplicationRecord
belongs_to :artist
has_many :songs

validates :name, presence: true
validates :spotify_url, presence: true

scope :by_artist_id_eq, proc { |artist_id|
if artist_id.present?
where(artist_id: artist_id)
end
}

def self.get_all(params)
query = all
query = params.apply_filters(query)
PaginateList
.get_all_paginate(
query,
params.pagination_limit,
params.pagination_number,
AlbumSerializer)
end

end
17 changes: 17 additions & 0 deletions app/models/artist.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
class Artist < ApplicationRecord
has_many :albums
has_many :songs, through: :albums

validates :name, presence: true
validates :spotify_url, presence: true

default_scope {order(popularity: :desc)}

def self.get_all(params)
query = all
PaginateList
.get_all_paginate(
query,
params.pagination_limit,
params.pagination_number,
ArtistSerializer)
end

end
34 changes: 34 additions & 0 deletions app/models/song.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
class Song < ApplicationRecord
belongs_to :album
has_one :artist, :through => :album
validates :name, presence: true
validates :spotify_url, presence: true

scope :by_album_id_eq, proc { |album_id|
if album_id.present?
where(album_id: album_id)
end
}

scope :by_genre_name_eq, proc { |genre|
if genre.present?
joins(:artist).where('artists.genres like ?', "%#{genre}%")
.offset(
rand(Song.joins(:artist)
.where('artists.genres like ?', "%#{genre}%")
.count
)
)

end
}

def self.get_all(params)
query = all
query = params.apply_filters(query)
PaginateList
.get_all_paginate(
query,
params.pagination_limit,
params.pagination_number,
SongSerializer)
end

end
8 changes: 8 additions & 0 deletions app/serializers/album_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AlbumSerializer < ActiveModel::Serializer
attributes :id,
:name,
:image,
:spotify_url,
:total_tracks

end
12 changes: 12 additions & 0 deletions app/serializers/artist_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class ArtistSerializer < ActiveModel::Serializer
attributes :id,
:name,
:image,
:genres,
:popularity,
:spotify_url

def genres
JSON(object.genres)
end
end
9 changes: 9 additions & 0 deletions app/serializers/song_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class SongSerializer < ActiveModel::Serializer
attributes :id,
:name,
:spotify_url,
:preview_url,
:duration_ms,
:explicit

end
11 changes: 10 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Rails.application.routes.draw do
=begin
#namespace :api, defaluts: {format: :json} => defaults

namespace 'api', defaults: { format: :json } do
namespace 'v1', defaults: { format: :json } do
get 'artists', to: 'artists#index'
Expand All @@ -9,5 +9,14 @@
get 'genres/:genre_name/random_song', to: 'genres#song_by_genre'
end
end
=end
namespace :api do
namespace :v1 do
resources :artists, only: [:index]
get 'artists/:artist_id/albums', action: :index, controller: :albums
get 'albums/:album_id/songs', action: :index, controller: :songs
get 'genre/:genre_name/random_song', action: :random_song, controller: :songs
end
end

end
8 changes: 7 additions & 1 deletion db/migrate/20190609162147_create_artists.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
class CreateArtists < ActiveRecord::Migration[5.2]
def change
create_table :artists do |t|

t.string :name
t.string :image
t.text :genres, default: [].to_yaml
t.integer :popularity
t.string :spotify_url
t.string :spotify_id

t.timestamps
end
end
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20190609162522_create_albums.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ class CreateAlbums < ActiveRecord::Migration[5.2]
def change
create_table :albums do |t|
t.references :artist, foreign_key: true
t.string :name
t.string :image
t.string :spotify_url
t.integer :total_tracks
t.string :spotify_id

t.timestamps
end
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20190609162542_create_songs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ class CreateSongs < ActiveRecord::Migration[5.2]
def change
create_table :songs do |t|
t.references :album, foreign_key: true
t.string :name
t.string :spotify_url
t.string :preview_url
t.string :duration_ms
t.boolean :explicit
t.string :spotify_id

t.timestamps
end
Expand Down
Loading