diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb new file mode 100644 index 0000000..7e70f9b --- /dev/null +++ b/app/controllers/orders_controller.rb @@ -0,0 +1,74 @@ +class OrdersController < ApplicationController + before_action :set_order, only: %i[ show edit update destroy ] + + # GET /orders or /orders.json + def index + @orders = Order.all + end + + # GET /orders/1 or /orders/1.json + def show + end + + # GET /orders/new + def new + @order = Order.new + end + + # GET /orders/1/edit + def edit + end + + # POST /orders or /orders.json + def create + @order = Order.new(order_params) + + @order.user = current_user + @order.stock = Stock.find(order_params[:stock_id]) + + + respond_to do |format| + if @order.save + format.html { redirect_to order_url(@order), notice: "Order was successfully created." } + format.json { render :show, status: :created, location: @order } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @order.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /orders/1 or /orders/1.json + def update + respond_to do |format| + if @order.update(order_params) + format.html { redirect_to order_url(@order), notice: "Order was successfully updated." } + format.json { render :show, status: :ok, location: @order } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @order.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /orders/1 or /orders/1.json + def destroy + @order.destroy! + + respond_to do |format| + format.html { redirect_to orders_url, notice: "Order was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_order + @order = Order.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def order_params + params.require(:order).permit(:student_id, :stock_id, :shares, :status) + end +end diff --git a/app/helpers/orders_helper.rb b/app/helpers/orders_helper.rb new file mode 100644 index 0000000..443227f --- /dev/null +++ b/app/helpers/orders_helper.rb @@ -0,0 +1,2 @@ +module OrdersHelper +end diff --git a/app/views/orders/_form.html.erb b/app/views/orders/_form.html.erb new file mode 100644 index 0000000..8e511e8 --- /dev/null +++ b/app/views/orders/_form.html.erb @@ -0,0 +1,28 @@ +<%= form_with(model: order) do |form| %> + <% if order.errors.any? %> +
+

<%= pluralize(order.errors.count, "error") %> prohibited this order from being saved:

+ + +
+ <% end %> + +
+ <%= form.label :stock_id, style: "display: block" %> + <%= collection_select(:order, :stock_id, Stock.all, :id, :company_name)%> + <%# <%= form.text_field :stock_id %> +
+ +
+ <%= form.label :shares, style: "display: block" %> + <%= form.number_field :shares, min:1 %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/orders/_order.html.erb b/app/views/orders/_order.html.erb new file mode 100644 index 0000000..76669c8 --- /dev/null +++ b/app/views/orders/_order.html.erb @@ -0,0 +1,19 @@ +
+

+ Stock: + + + <%= order.stock.company_name %> +

+ +

+ Shares: + <%= order.shares %> +

+ +

+ Status: + <%= order.status %> +

+ +
diff --git a/app/views/orders/_order.json.jbuilder b/app/views/orders/_order.json.jbuilder new file mode 100644 index 0000000..9198d02 --- /dev/null +++ b/app/views/orders/_order.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! order, :id, :student_id, :stock_id, :shares, :status, :created_at, :updated_at +json.url order_url(order, format: :json) diff --git a/app/views/orders/edit.html.erb b/app/views/orders/edit.html.erb new file mode 100644 index 0000000..6863c04 --- /dev/null +++ b/app/views/orders/edit.html.erb @@ -0,0 +1,10 @@ +

Editing order

+ +<%= render "form", order: @order %> + +
+ +
+ <%= link_to "Show this order", @order %> | + <%= link_to "Back to orders", orders_path %> +
diff --git a/app/views/orders/index.html.erb b/app/views/orders/index.html.erb new file mode 100644 index 0000000..1cdaf94 --- /dev/null +++ b/app/views/orders/index.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

Orders

+ +
+ <% @orders.each do |order| %> + <%= render order %> +

+ <%= link_to "Show this order", order %> +

+ <% end %> +
+ +<%= link_to "New order", new_order_path %> diff --git a/app/views/orders/index.json.jbuilder b/app/views/orders/index.json.jbuilder new file mode 100644 index 0000000..51d68db --- /dev/null +++ b/app/views/orders/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @orders, partial: "orders/order", as: :order diff --git a/app/views/orders/new.html.erb b/app/views/orders/new.html.erb new file mode 100644 index 0000000..f428016 --- /dev/null +++ b/app/views/orders/new.html.erb @@ -0,0 +1,9 @@ +

New order

+ +<%= render "form", order: @order %> + +
+ +
+ <%= link_to "Back to orders", orders_path %> +
diff --git a/app/views/orders/show.html.erb b/app/views/orders/show.html.erb new file mode 100644 index 0000000..a065af0 --- /dev/null +++ b/app/views/orders/show.html.erb @@ -0,0 +1,10 @@ +

<%= notice %>

+ +<%= render @order %> + +
+ <%= link_to "Edit this order", edit_order_path(@order) %> | + <%= link_to "Back to orders", orders_path %> + + <%= button_to "Destroy this order", @order, method: :delete %> +
diff --git a/app/views/orders/show.json.jbuilder b/app/views/orders/show.json.jbuilder new file mode 100644 index 0000000..2ddef40 --- /dev/null +++ b/app/views/orders/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "orders/order", order: @order diff --git a/config/routes.rb b/config/routes.rb index 76ad555..2ea6f55 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :orders namespace :admin do resources :schools resources :school_years diff --git a/test/controllers/orders_controller_test.rb b/test/controllers/orders_controller_test.rb new file mode 100644 index 0000000..336470c --- /dev/null +++ b/test/controllers/orders_controller_test.rb @@ -0,0 +1,57 @@ +require "test_helper" + +class OrdersControllerTest < ActionDispatch::IntegrationTest + setup do + @order = orders(:one) + end + + test "should get index" do + get orders_url + assert_response :success + end + + test "should get new" do + get new_order_url + assert_response :success + end + + test "should show order" do + get order_url(@order) + assert_response :success + end + + test "should get edit" do + get edit_order_url(@order) + assert_response :success + end + + test "should update order" do + patch order_url(@order), params: { order: { shares: @order.shares, status: @order.status, stock_id: @order.stock_id, user_id: @order.user_id } } + assert_redirected_to order_url(@order) + end + + test "should destroy order" do + assert_difference("Order.count", -1) do + delete order_url(@order) + end + + assert_redirected_to orders_url + end + + test "" do + sign_in Student.first + + stock_id = Stock.first.id + num_shares = 5 + + assert_difference("Order.count") do + post orders_url, params: { order: { shares: num_shares, stock_id: stock_id } } + end + + + assert_equal(num_shares, Order.last.shares) + assert_redirected_to order_url(Order.last) + end + +end + diff --git a/test/system/orders_test.rb b/test/system/orders_test.rb new file mode 100644 index 0000000..6a21e32 --- /dev/null +++ b/test/system/orders_test.rb @@ -0,0 +1,47 @@ +require "application_system_test_case" + +class OrdersTest < ApplicationSystemTestCase + setup do + @order = orders(:one) + end + + test "visiting the index" do + visit orders_url + assert_selector "h1", text: "Orders" + end + + test "should create order" do + visit orders_url + click_on "New order" + + fill_in "Shares", with: @order.shares + fill_in "Status", with: @order.status + fill_in "Stock", with: @order.stock_id + fill_in "Student", with: @order.student_id + click_on "Create Order" + + assert_text "Order was successfully created" + click_on "Back" + end + + test "should update Order" do + visit order_url(@order) + click_on "Edit this order", match: :first + + fill_in "Shares", with: @order.shares + fill_in "Status", with: @order.status + fill_in "Stock", with: @order.stock_id + fill_in "Student", with: @order.student_id + click_on "Update Order" + + assert_text "Order was successfully updated" + click_on "Back" + end + + test "should destroy Order" do + visit order_url(@order) + click_on "Destroy this order", match: :first + + assert_text "Order was successfully destroyed" + end +end