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

Feature/implement api assembly #66

Merged
merged 5 commits into from
Nov 28, 2023
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@

/public/assets
/.idea
/test
/db/schema.rb
# Ignore master key for decrypting credentials and more.
/config/master.key
47 changes: 47 additions & 0 deletions app/controllers/api/assemblies_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Api
class AssembliesController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :set_assembly, only: %i[show update destroy]
def index
@assemblies = Assembly.all
render json: @assemblies, except: %i[created_at]
end

def show
render json: @assembly
end

def create
@assembly = Assembly.new(params_assembly)

if @assembly.save
render json: @assembly, status: :created, except: %i[created_at]
else
render json: @assembly.errors.full_messages, status: :unprocessable_entity
end
end

def update
if @assembly.update(params_assembly)
render json: @assembly, except: %i[created_at]
else
render json: @assembly.errors.full_messages, status: :unprocessable_entity
end
end

def destroy
@assembly.destroy
head :no_content
end

private

def set_assembly
@assembly = Assembly.find(params[:id])
end

def params_assembly
params.require(:assembly).permit(:name, part_ids: [], book_ids: [])
end
end
end
80 changes: 30 additions & 50 deletions app/controllers/assemblies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,78 +1,58 @@
class AssembliesController < ApplicationController
before_action :set_assembly, only: %i[ show edit update destroy ]
before_action :set_part_id_options, only: %i[ new show edit update destroy ]
before_action :set_book_id_options, only: %i[ new show edit update destroy ]
before_action :set_assembly, only: %i[show edit update destroy]
before_action :set_part_id_options, only: %i[new show edit update destroy]
before_action :set_book_id_options, only: %i[new show edit update destroy]

# GET /assemblies or /assemblies.json
def index
@assemblies = Assembly.all
end

# GET /assemblies/1 or /assemblies/1.json
def show
end
def show; end

# GET /assemblies/new
def new
@assembly = Assembly.new
end

# GET /assemblies/1/edit
def edit
end
def edit; end

# POST /assemblies or /assemblies.json
def create
@assembly = Assembly.new(assembly_params)

respond_to do |format|
if @assembly.save
format.html { redirect_to assembly_url(@assembly), notice: "Assembly was successfully created." }
format.json { render :show, status: :created, location: @assembly }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @assembly.errors, status: :unprocessable_entity }
end
if @assembly.save
redirect_to assembly_url(@assembly), notice: 'Assembly was successfully created.'
else
render :new, status: :unprocessable_entity
end
end

# PATCH/PUT /assemblies/1 or /assemblies/1.json
def update
respond_to do |format|
if @assembly.update(assembly_params)
format.html { redirect_to assembly_url(@assembly), notice: "Assembly was successfully updated." }
format.json { render :show, status: :ok, location: @assembly }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @assembly.errors, status: :unprocessable_entity }
end
if @assembly.update(assembly_params)
redirect_to assembly_url(@assembly), notice: 'Assembly was successfully updated.'
else
render :edit, status: :unprocessable_entity
end
end

# DELETE /assemblies/1 or /assemblies/1.json
def destroy
@assembly.destroy

respond_to do |format|
format.html { redirect_to assemblies_url, notice: "Assembly was successfully destroyed." }
format.json { head :no_content }
end
redirect_to assemblies_url, notice: 'Assembly was successfully destroyed.'
end

private
# Use callbacks to share common setup or constraints between actions.
def set_book_id_options
@book_id_options = Book.all.pluck(:published_at, :id)
end
def set_part_id_options
@part_id_options = Part.all.pluck(:part_number, :id)
end
def set_assembly
@assembly = Assembly.find(params[:id])
end

# Only allow a list of trusted parameters through.
def assembly_params
params.require(:assembly).permit(:name, :part_id, :book_id)
end
def set_book_id_options
@book_id_options = Book.all.pluck(:published_at, :id)
end

def set_part_id_options
@part_id_options = Part.all.pluck(:part_number, :id)
end

def set_assembly
@assembly = Assembly.find(params[:id])
end

# Only allow a list of trusted parameters through.
def assembly_params
params.require(:assembly).permit(:name, :part_id, :book_id)
end
end
2 changes: 2 additions & 0 deletions app/helpers/api/assemblies_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Api::AssembliesHelper
end
7 changes: 4 additions & 3 deletions app/models/assembly.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Assembly < ApplicationRecord
has_many :part, through: :assembly_parts
belongs_to :book
has_many :assembly_parts
has_and_belongs_to_many :part
has_one :book

validates :name, presence: true
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
resources :suppliers
resources :accounts
resources :parts
resources :assemblies
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CreateAssemblyParts < ActiveRecord::Migration[7.0]
class CreateAssembliesParts < ActiveRecord::Migration[7.0]
def change
create_table :assembly_parts do |t|
create_table :assemblies_parts do |t|
t.references :assembly, null: false, foreign_key: true
t.references :part, null: false, foreign_key: true

Expand Down
35 changes: 25 additions & 10 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,50 @@
Account.destroy_all
Part.destroy_all

#Create Author
# Create Author
authors = []
5.times do
authors << Author.create(name: FFaker::Name.name, cpf: FFaker::IdentificationBR.cpf)
end

#create Supplier
# create Supplier
5.times do
Supplier.create(name: FFaker::Company.name, cnpj: FFaker::IdentificationBR.cnpj)
end

#create Account
# create Account
suppliers_ids = Supplier.pluck(:id)

suppliers_ids.each do |supplier_id|
Account.create(digit: rand(1..50), account_number: rand(10_000..99_999).to_s, supplier_id:)
end

#create Part
part_name = ["Hard Cover", "Soft Cover", "Premium Cover", "Dust Jacket", "Comic Book Cover",
"Standard Size Page", "Large Print Page", "Deckle Edge",
"Glossy Page", "Premium Paper Page",
"Recycled Paper Page", "Gilded Edge", "Endpapers",
"Header", "Footer", "Bookmark", "Binding", "Corner Cut",
"Fold", "Creases"]
# create Part
part_name = ['Hard Cover', 'Soft Cover', 'Premium Cover', 'Dust Jacket', 'Comic Book Cover',
'Standard Size Page', 'Large Print Page', 'Deckle Edge',
'Glossy Page', 'Premium Paper Page',
'Recycled Paper Page', 'Gilded Edge', 'Endpapers',
'Header', 'Footer', 'Bookmark', 'Binding', 'Corner Cut',
'Fold', 'Creases']

part_name.each_slice(4).with_index do |name_slice, index|
name_slice.each do |name|
Part.create(name:, part_number: rand(1_000..9_999), price: rand(1.9..99.9).round(2),
supplier_id: suppliers_ids[index])
end
end

# create assembly
assemblies_descriptions = {
'paperback' => ['Soft Cover', 'Standard Size Page', 'Acid-free Page', 'Comic Book Cover', 'Bookmark'],
'hardcover' => ['Hard Cover', 'Dust Jacket', 'Acid-free Page', 'Large Print Page', 'Header'],
'deluxe edition' => ['Premium Cover', 'Glossy Page', 'Endpapers', 'Premium Paper Page', 'Footer'],
"collector's edition" => ['Hard Cover', 'Gilded Edge', 'Endpapers', 'Recycled Paper Page', 'Binding'],
'limited edition' => ['Premium Cover', 'Deckle Edge', 'Gilded Edge', 'Creases', 'Corner Cut', 'Fold']
}

assemblies_descriptions.each do |name, dscpts|
assembly_parts = dscpts.map { |dscpt| Part.find_by(name: dscpt) }.compact
assembly = Assembly.create(name:)
assembly_parts << assembly.part
end
5 changes: 0 additions & 5 deletions spec/models/assembly_part_spec.rb

This file was deleted.

7 changes: 7 additions & 0 deletions spec/requests/api/assemblies_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rails_helper'

RSpec.describe "Api::Assemblies", type: :request do
describe "GET /index" do
pending "add some examples (or delete) #{__FILE__}"
end
end
Loading