Skip to content
Merged
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
8 changes: 8 additions & 0 deletions app/controllers/api/v3/public/illustrators_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module API
module V3
module Public
class Api::V3::Public::IllustratorsController < JSONAPI::ResourceController
end
end
end
end
6 changes: 6 additions & 0 deletions app/models/illustrator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class Illustrator < ApplicationRecord
has_many :illustrator_printings
has_many :printings, :through => :illustrator_printings
end
12 changes: 12 additions & 0 deletions app/models/illustrator_printing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class IllustratorPrinting < ApplicationRecord
self.table_name = "illustrators_printings"

belongs_to :illustrator,
primary_key: :id,
foreign_key: :illustrator_id
belongs_to :printing,
primary_key: :id,
foreign_key: :printing_id
end
2 changes: 2 additions & 0 deletions app/models/printing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ class Printing < ApplicationRecord
has_one :faction, :through => :card
has_one :card_cycle, :through => :card_set
has_one :side, :through => :card
has_many :illustrator_printings
has_many :illustrators, :through => :illustrator_printings
end
16 changes: 16 additions & 0 deletions app/resources/api/v3/public/illustrator_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module API
module V3
module Public
class Api::V3::Public::IllustratorResource < JSONAPI::Resource
immutable

attributes :name, :updated_at
key_type :string

paginator :none

has_many :printings
end
end
end
end
9 changes: 5 additions & 4 deletions app/resources/api/v3/public/printing_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Api::V3::Public::PrintingResource < JSONAPI::Resource

# Direct printing attributes
attributes :card_id, :card_set_id, :printed_text, :stripped_printed_text
attributes :printed_is_unique, :flavor, :illustrator, :position
attributes :printed_is_unique, :flavor, :display_illustrators, :position
attributes :quantity, :date_release, :updated_at

# Parent Card attributes, included inline to make printings a bit more useful.
Expand All @@ -21,14 +21,15 @@ class Api::V3::Public::PrintingResource < JSONAPI::Resource

key_type :string

has_one :card
has_one :card_cycle
has_one :card_set
has_one :side
has_one :faction
has_one :card
has_many :illustrators
has_one :side

# Printing direct attribute filters
filters :card_id, :card_set_id, :printed_is_unique, :illustrator, :position
filters :card_id, :card_set_id, :printed_is_unique, :display_illustrators, :position
filters :quantity, :date_release

# Card attribute filters
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
jsonapi_resources :card_types, only: [:index, :show]
jsonapi_resources :cards, only: [:index, :show]
jsonapi_resources :factions, only: [:index, :show]
jsonapi_resources :illustrators, only: [:index, :show]
jsonapi_resources :printings, only: [:index, :show]
jsonapi_resources :sides, only: [:index, :show]
end
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20220625035145_create_illustrator_tables.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateIllustratorTables < ActiveRecord::Migration[7.0]
def change
create_table :illustrators, id: :string do |t|
t.string :name
t.timestamps
end

create_table :illustrators_printings, id: false, force: :cascade do |t|
t.string :illustrator_id, null: false
t.string :printing_id, null: false
t.index [:illustrator_id, :printing_id], name: "index_illustrators_printings_on_illustrator_id_and_printing_id", unique: true
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20220625170812_rename_printing_illustrator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenamePrintingIllustrator < ActiveRecord::Migration[7.0]
def change
rename_column :printings, :illustrator, :display_illustrators
end
end
16 changes: 14 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2022_06_18_233411) do
ActiveRecord::Schema[7.0].define(version: 2022_06_25_170812) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -97,14 +97,26 @@
t.string "description"
end

create_table "illustrators", id: :string, force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "illustrators_printings", id: false, force: :cascade do |t|
t.string "illustrator_id", null: false
t.string "printing_id", null: false
t.index ["illustrator_id", "printing_id"], name: "index_illustrators_printings_on_illustrator_id_and_printing_id", unique: true
end

create_table "printings", id: :string, force: :cascade do |t|
t.text "card_id"
t.text "card_set_id"
t.text "printed_text"
t.text "stripped_printed_text"
t.boolean "printed_is_unique"
t.text "flavor"
t.text "illustrator"
t.text "display_illustrators"
t.integer "position"
t.integer "quantity"
t.date "date_release"
Expand Down
62 changes: 60 additions & 2 deletions lib/tasks/cards.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
namespace :cards do
desc 'import card data - json_dir defaults to /netrunner-cards-json/ if not specified.'

def text_to_id(t)
t.downcase
.unicode_normalize(:nfd)
.gsub(/\P{ASCII}/, '')
.gsub(/'s(\p{Space}|\z)/, 's\1')
.split(/[\p{Space}\p{Punct}]+/)
.reject { |s| s&.strip&.empty? }
.join("_")
end

def load_multiple_json_files(path)
cards = []
Dir.glob(path) do |f|
Expand Down Expand Up @@ -124,7 +134,7 @@ namespace :cards do
ActiveRecord::Base.transaction do
puts 'Clear out existing card -> subtype mappings'
unless ActiveRecord::Base.connection.delete("DELETE FROM cards_card_subtypes")
puts 'Hit an error while delete card -> subtype mappings. rolling back.'
puts 'Hit an error while deleting card -> subtype mappings. rolling back.'
raise ActiveRecord::Rollback
end

Expand Down Expand Up @@ -205,7 +215,7 @@ namespace :cards do
printed_is_unique: printing["printed_is_unique"],
id: printing["id"],
flavor: printing["flavor"],
illustrator: printing["illustrator"],
display_illustrators: printing["illustrator"],
position: printing["position"],
quantity: printing["quantity"],
card_id: printing["card_id"],
Expand All @@ -222,6 +232,51 @@ namespace :cards do
}
end

def import_illustrators()
# Use a transaction since we are deleting the illustrator and mapping tables.
ActiveRecord::Base.transaction do
puts 'Clear out existing illustrator -> printing mappings'
unless ActiveRecord::Base.connection.delete("DELETE FROM illustrators_printings")
puts 'Hit an error while deleting illustrator -> printing mappings. rolling back.'
raise ActiveRecord::Rollback
end

puts 'Clear out existing illustrators'
unless ActiveRecord::Base.connection.delete("DELETE FROM illustrators")
puts 'Hit an error while deleting illustrators. rolling back.'
raise ActiveRecord::Rollback
end

illustrators = Set[]
illustrators_to_printings = []
num_its = 0
printings = Printing.all
printings.each { |printing|
if printing.display_illustrators then
printing.display_illustrators.split(', ').each { |i|
illustrators.add(i)
num_its += 1
illustrators_to_printings << {
"illustrator_id": text_to_id(i),
"printing_id": printing.id
}
}
end
}

ill = []
illustrators.each { |i|
ill << {
"id": text_to_id(i),
"name": i
}
}

Illustrator.import ill, on_duplicate_key_update: { conflict_target: [ :id ], columns: :all }
IllustratorPrinting.import illustrators_to_printings, on_duplicate_key_update: { conflict_target: [ :illustrator_id, :printing_id ], columns: :all }
end
end

task :import, [:json_dir] => [:environment] do |t, args|
args.with_defaults(:json_dir => '/netrunner-cards-json/')
puts 'Import card data...'
Expand Down Expand Up @@ -262,6 +317,9 @@ namespace :cards do
puts 'Importing Printings...'
import_printings(load_multiple_json_files(args[:json_dir] + '/v2/printings/*.json'))

puts 'Importing Illustrators...'
import_illustrators()

puts 'Done!'
end
end