Skip to content

Commit

Permalink
feat: 🎸 Album モデルとインポータを追加しアソシエーションを設定した (#42)
Browse files Browse the repository at this point in the history
* 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
nikukyugamer committed Dec 3, 2023
1 parent e93d566 commit 3bf4046
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/models/album.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Album < ApplicationRecord
belongs_to :artist
end
1 change: 1 addition & 0 deletions app/models/artist.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Artist < ApplicationRecord
has_many :albums, dependent: :destroy
end
39 changes: 39 additions & 0 deletions app/services/import_to_db/album_service.rb
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
27 changes: 27 additions & 0 deletions db/migrate/20231203022608_create_albums.rb
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
27 changes: 26 additions & 1 deletion db/schema.rb

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

5 changes: 5 additions & 0 deletions spec/factories/albums.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :album do

end
end
5 changes: 5 additions & 0 deletions spec/models/album_spec.rb
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

0 comments on commit 3bf4046

Please sign in to comment.