-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from concertmate/artist
Artist
- Loading branch information
Showing
53 changed files
with
1,504 additions
and
3,531 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,5 @@ | |
|
||
# Ignore master key for decrypting credentials and more. | ||
/config/master.key | ||
|
||
coverage |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
class Api::V1::ArtistsController < ApplicationController | ||
def index | ||
if params[:name] | ||
begin | ||
artists = ArtistFacade.search_artists(params[:name]) | ||
render json: ArtistSerializer.new(artists) | ||
rescue JSON::ParserError => e | ||
render json: { errors: [{ detail: "Failed to parse response: #{e.message}" }] }, status: :internal_server_error | ||
rescue StandardError => e | ||
render json: ErrorSerializer.new(e).serialize_json, status: :internal_server_error | ||
end | ||
else | ||
error = ErrorSerializer.new(StandardError.new('Name parameter is required')) | ||
render json: error.serialize_json, status: :bad_request | ||
end | ||
end | ||
|
||
def create | ||
user = User.find(params[:user_id]) | ||
artist = Artist.find_or_create_by!(artist_params) | ||
|
||
UserArtist.find_or_create_by!(user: user, artist: artist) | ||
|
||
render json: artist, status: :ok | ||
rescue ActiveRecord::RecordNotFound => e | ||
render json: ErrorSerializer.new(e).serialize_json, status: :not_found | ||
rescue ActiveRecord::RecordInvalid => e | ||
render json: ErrorSerializer.new(e).serialize_json, status: :bad_request | ||
end | ||
|
||
def destroy | ||
user_artist = UserArtist.find_by!(user_id: params[:user_id], artist_id: params[:id]) | ||
user_artist.destroy | ||
|
||
render json: { message: "Artist removed from user's saved artists" }, status: :ok | ||
rescue ActiveRecord::RecordNotFound => e | ||
render json: ErrorSerializer.new(e).serialize_json, status: :not_found | ||
end | ||
|
||
private | ||
|
||
def artist_params | ||
params.require(:artist).permit(:name, :musicbrainz_id) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class ArtistFacade | ||
def self.search_artists(name) | ||
artists_data = ArtistService.get_artists_by_name(name) | ||
artists_data.map do |artist_data| | ||
condensed_data = { | ||
name: artist_data[:name], | ||
id: artist_data[:id] | ||
} | ||
Artist.new(condensed_data) | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Artist < ApplicationRecord | ||
has_many :user_artists | ||
has_many :users, through: :user_artists | ||
|
||
validates :name, presence: true | ||
validates :musicbrainz_id, presence: true, uniqueness: true | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class UserArtist < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :artist | ||
|
||
validates :user_id, presence: true | ||
validates :artist_id, presence: true | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Artist | ||
attr_reader :name, :musicbrainz_id | ||
|
||
def initialize(data) | ||
@name = data[:name] | ||
@musicbrainz_id = data[:id] | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class ArtistSerializer | ||
include JSONAPI::Serializer | ||
attributes :name, :musicbrainz_id | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class ArtistService | ||
def self.get_conn | ||
Faraday.new(url: 'https://musicbrainz.org/ws/2') do |faraday| | ||
faraday.headers['Content-Type'] = 'application/json' | ||
faraday.adapter Faraday.default_adapter | ||
end | ||
end | ||
|
||
def self.get_artists_by_name(name) | ||
response = get_conn.get('/artist', { query: "artist:#{name}", fmt: 'json' }) | ||
JSON.parse(response.body, symbolize_names: true)[:artists] | ||
end | ||
end |
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.