mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-11-28 09:24:24 +01:00
Product model/controller
This commit is contained in:
parent
4ce68f33a8
commit
1e3e7854b2
52
app/controllers/api/products_controller.rb
Normal file
52
app/controllers/api/products_controller.rb
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# API Controller for resources of type Product
|
||||||
|
# Products are used in store
|
||||||
|
class API::ProductsController < API::ApiController
|
||||||
|
before_action :authenticate_user!, except: %i[index show]
|
||||||
|
before_action :set_product, only: %i[update destroy]
|
||||||
|
|
||||||
|
def index
|
||||||
|
@products = ProductService.list
|
||||||
|
end
|
||||||
|
|
||||||
|
def show; end
|
||||||
|
|
||||||
|
def create
|
||||||
|
authorize Product
|
||||||
|
@product = Product.new(product_params)
|
||||||
|
if @product.save
|
||||||
|
render status: :created
|
||||||
|
else
|
||||||
|
render json: @product.errors.full_messages, status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
authorize @product
|
||||||
|
|
||||||
|
if @product.update(product_params)
|
||||||
|
render status: :ok
|
||||||
|
else
|
||||||
|
render json: @product.errors.full_messages, status: :unprocessable_entity
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
authorize @product
|
||||||
|
@product.destroy
|
||||||
|
head :no_content
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def set_product
|
||||||
|
@product = Product.find(params[:id])
|
||||||
|
end
|
||||||
|
|
||||||
|
def product_params
|
||||||
|
params.require(:product).permit(:name, :slug, :sku, :description, :is_active,
|
||||||
|
:product_category_id, :amount, :quantity_min,
|
||||||
|
:low_stock_alert, :low_stock_threshold)
|
||||||
|
end
|
||||||
|
end
|
3
app/models/product.rb
Normal file
3
app/models/product.rb
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
class Product < ApplicationRecord
|
||||||
|
belongs_to :product_category
|
||||||
|
end
|
16
app/policies/product_policy.rb
Normal file
16
app/policies/product_policy.rb
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Check the access policies for API::ProductsController
|
||||||
|
class ProductPolicy < ApplicationPolicy
|
||||||
|
def create?
|
||||||
|
user.privileged?
|
||||||
|
end
|
||||||
|
|
||||||
|
def update?
|
||||||
|
user.privileged?
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy?
|
||||||
|
user.privileged?
|
||||||
|
end
|
||||||
|
end
|
8
app/services/product_service.rb
Normal file
8
app/services/product_service.rb
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Provides methods for Product
|
||||||
|
class ProductService
|
||||||
|
def self.list
|
||||||
|
Product.all
|
||||||
|
end
|
||||||
|
end
|
3
app/views/api/products/_product.json.jbuilder
Normal file
3
app/views/api/products/_product.json.jbuilder
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
json.extract! product, :id, :name, :slug, :sku, :description, :is_active, :product_category_id, :amount, :quantity_min, :stock, :low_stock_alert, :low_stock_threshold
|
3
app/views/api/products/create.json.jbuilder
Normal file
3
app/views/api/products/create.json.jbuilder
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
json.partial! 'api/products/product', product: @product
|
5
app/views/api/products/index.json.jbuilder
Normal file
5
app/views/api/products/index.json.jbuilder
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
json.array! @products do |product|
|
||||||
|
json.partial! 'api/products/product', product: product
|
||||||
|
end
|
3
app/views/api/products/show.json.jbuilder
Normal file
3
app/views/api/products/show.json.jbuilder
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
json.partial! 'api/products/product', product: @product
|
3
app/views/api/products/update.json.jbuilder
Normal file
3
app/views/api/products/update.json.jbuilder
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
json.partial! 'api/products/product', product: @product
|
@ -154,6 +154,8 @@ Rails.application.routes.draw do
|
|||||||
patch 'position', on: :member
|
patch 'position', on: :member
|
||||||
end
|
end
|
||||||
|
|
||||||
|
resources :products
|
||||||
|
|
||||||
# for admin
|
# for admin
|
||||||
resources :trainings do
|
resources :trainings do
|
||||||
get :availabilities, on: :member
|
get :availabilities, on: :member
|
||||||
|
19
db/migrate/20220712153708_create_products.rb
Normal file
19
db/migrate/20220712153708_create_products.rb
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
class CreateProducts < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
create_table :products do |t|
|
||||||
|
t.string :name
|
||||||
|
t.string :slug
|
||||||
|
t.string :sku
|
||||||
|
t.text :description
|
||||||
|
t.boolean :is_active, default: false
|
||||||
|
t.belongs_to :product_category, foreign_key: true
|
||||||
|
t.integer :amount
|
||||||
|
t.integer :quantity_min
|
||||||
|
t.jsonb :stock, default: { internal: 0, external: 0 }
|
||||||
|
t.boolean :low_stock_alert, default: false
|
||||||
|
t.integer :low_stock_threshold
|
||||||
|
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,8 @@
|
|||||||
|
class CreateJoinTableProductMachine < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
create_join_table :products, :machines do |t|
|
||||||
|
# t.index [:product_id, :machine_id]
|
||||||
|
# t.index [:machine_id, :product_id]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
23
db/schema.rb
23
db/schema.rb
@ -367,6 +367,11 @@ ActiveRecord::Schema.define(version: 2022_07_20_135828) do
|
|||||||
t.index ["machine_id"], name: "index_machines_availabilities_on_machine_id"
|
t.index ["machine_id"], name: "index_machines_availabilities_on_machine_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "machines_products", id: false, force: :cascade do |t|
|
||||||
|
t.bigint "product_id", null: false
|
||||||
|
t.bigint "machine_id", null: false
|
||||||
|
end
|
||||||
|
|
||||||
create_table "notifications", id: :serial, force: :cascade do |t|
|
create_table "notifications", id: :serial, force: :cascade do |t|
|
||||||
t.integer "receiver_id"
|
t.integer "receiver_id"
|
||||||
t.string "attached_object_type"
|
t.string "attached_object_type"
|
||||||
@ -591,6 +596,23 @@ ActiveRecord::Schema.define(version: 2022_07_20_135828) do
|
|||||||
t.index ["parent_id"], name: "index_product_categories_on_parent_id"
|
t.index ["parent_id"], name: "index_product_categories_on_parent_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "products", force: :cascade do |t|
|
||||||
|
t.string "name"
|
||||||
|
t.string "slug"
|
||||||
|
t.string "sku"
|
||||||
|
t.text "description"
|
||||||
|
t.boolean "is_active", default: false
|
||||||
|
t.bigint "product_category_id"
|
||||||
|
t.integer "amount"
|
||||||
|
t.integer "quantity_min"
|
||||||
|
t.jsonb "stock", default: {"external"=>0, "internal"=>0}
|
||||||
|
t.boolean "low_stock_alert", default: false
|
||||||
|
t.integer "low_stock_threshold"
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.index ["product_category_id"], name: "index_products_on_product_category_id"
|
||||||
|
end
|
||||||
|
|
||||||
create_table "profile_custom_fields", force: :cascade do |t|
|
create_table "profile_custom_fields", force: :cascade do |t|
|
||||||
t.string "label"
|
t.string "label"
|
||||||
t.boolean "required", default: false
|
t.boolean "required", default: false
|
||||||
@ -1112,6 +1134,7 @@ ActiveRecord::Schema.define(version: 2022_07_20_135828) do
|
|||||||
add_foreign_key "prepaid_packs", "groups"
|
add_foreign_key "prepaid_packs", "groups"
|
||||||
add_foreign_key "prices", "groups"
|
add_foreign_key "prices", "groups"
|
||||||
add_foreign_key "prices", "plans"
|
add_foreign_key "prices", "plans"
|
||||||
|
add_foreign_key "products", "product_categories"
|
||||||
add_foreign_key "project_steps", "projects"
|
add_foreign_key "project_steps", "projects"
|
||||||
add_foreign_key "project_users", "projects"
|
add_foreign_key "project_users", "projects"
|
||||||
add_foreign_key "project_users", "users"
|
add_foreign_key "project_users", "users"
|
||||||
|
Loading…
Reference in New Issue
Block a user