require_dependency "bouquet/gate/application_controller" module Bouquet class Gate::ProductsController < ApplicationController before_action :set_product, only: [:show, :update, :destroy] # GET /products def index products = Bouquet::Product.all @products = products.map do |product| { id: product.id, name: product.name, price: product.price, assemblies: product.assemblies.map do |assembly| { material_name: assembly.material.name, quantity: assembly.quantity } end } end render json: @products end # GET /products/1 def show render json: @product end # POST /products def create @product = Bouquet::Product.new(product_params) if @product.save render json: @product, status: :created, location: @product else render json: @product.errors, status: :unprocessable_entity end end # PATCH/PUT /products/1 def update if @product.update(product_params) render json: @product else render json: @product.errors, status: :unprocessable_entity end end # DELETE /products/1 def destroy @product.destroy end private # Use callbacks to share common setup or constraints between actions. def set_product @product = Bouquet::Product.find(params[:id]) end # Only allow a trusted parameter "white list" through. def product_params params.require(:product).permit(:name) end end end