-
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.
feat: 🎸 Album モデルとインポータを追加しアソシエーションを設定した (#42)
* feat: 🎸 $ bundle exec rails g model Album * feat: 🎸 $ bundle exec rails db:migrate * feat: 🎸 db/schema.rb を更新した * feat: 🎸 マイグレーションの内容に foreign_key を追加した * feat: 🎸 albums のカラム名をより適切なものに変更した * feat: 🎸 Album のインポータを作成した * feat: 🎸 Artist と Album にアソシエーションを設定した
- Loading branch information
1 parent
e93d566
commit 3bf4046
Showing
7 changed files
with
106 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Album < ApplicationRecord | ||
belongs_to :artist | ||
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
class Artist < ApplicationRecord | ||
has_many :albums, dependent: :destroy | ||
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,39 @@ | ||
require 'csv' | ||
|
||
module ImportToDb | ||
class AlbumService | ||
def initialize | ||
@csv_data = CSV.read('db/csv_files/albums.csv', headers: true) | ||
end | ||
|
||
def execute | ||
albums = @csv_data.map do |row| | ||
artist_id_str = row['artist_id'] | ||
|
||
# Artist がインポートされていることが前提となる | ||
artist = Artist.find_by(id_str: artist_id_str) | ||
artist_id = artist.id | ||
|
||
Album.new( | ||
artist_id:, | ||
id_str: row['id'], | ||
upc: row['upc'], | ||
name: row['name'], | ||
label: row['label'], | ||
popularity: row['popularity'].to_i, | ||
total_tracks: row['total_tracks'].to_i, | ||
url: row['spotify_external_url'], | ||
image_url: row['image_url'], | ||
image_height: row['image_height'].to_i, | ||
release_date_or_year: row['release_date'], | ||
release_date_precision: row['release_date_precision'], | ||
api_href: row['api_href'] | ||
) | ||
end | ||
|
||
Album.import!(albums, on_duplicate_key_ignore: true) | ||
|
||
nil | ||
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,27 @@ | ||
class CreateAlbums < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :albums do |t| | ||
t.references :artist, null: false, foreign_key: true, comment: 'アルバムのアーティスト' | ||
t.string :id_str, null: false, comment: 'Spotify のアルバムID' | ||
t.integer :upc, null: false, comment: 'アルバムのUPC' | ||
t.string :name, null: false, comment: 'アルバム名' | ||
t.string :label, null: false, comment: 'アルバムのレーベル' | ||
t.integer :popularity, null: false, comment: 'アルバムの"人気度"' | ||
t.integer :total_tracks, null: false, comment: 'アルバムのトラック数' | ||
t.string :url, null: false, comment: 'アルバムのURL' | ||
t.string :image_url, null: false, comment: 'アルバムの画像URL' | ||
t.integer :image_height, null: false, comment: 'アルバムの画像の高さ' | ||
t.string :release_date_or_year, null: false, comment: 'アルバムのリリース日または年' | ||
t.string :release_date_precision, null: false, comment: 'アルバムのリリース日単位(年または日)' | ||
t.string :api_href, null: false, comment: 'アルバムのAPIのURL' | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :albums, :id_str, unique: true | ||
add_index :albums, :upc, unique: true | ||
add_index :albums, :url, unique: true | ||
add_index :albums, :image_url, unique: true | ||
add_index :albums, :api_href, unique: true | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,5 @@ | ||
FactoryBot.define do | ||
factory :album do | ||
|
||
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,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Album, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |