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

Place orders 162 #173

Merged
merged 5 commits into from
Jun 1, 2024
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
74 changes: 74 additions & 0 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/helpers/orders_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module OrdersHelper
end
28 changes: 28 additions & 0 deletions app/views/orders/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<%= form_with(model: order) do |form| %>
<% if order.errors.any? %>
<div style="color: red">
<h2><%= pluralize(order.errors.count, "error") %> prohibited this order from being saved:</h2>

<ul>
<% order.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>

<div>
<%= form.label :stock_id, style: "display: block" %>
<%= collection_select(:order, :stock_id, Stock.all, :id, :company_name)%>
<%# <%= form.text_field :stock_id %>
</div>

<div>
<%= form.label :shares, style: "display: block" %>
<%= form.number_field :shares, min:1 %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
19 changes: 19 additions & 0 deletions app/views/orders/_order.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div id="<%= dom_id order %>">
<p>
<strong>Stock:</strong>


<%= order.stock.company_name %>
</p>

<p>
<strong>Shares:</strong>
<%= order.shares %>
</p>

<p>
<strong>Status:</strong>
<%= order.status %>
</p>

</div>
2 changes: 2 additions & 0 deletions app/views/orders/_order.json.jbuilder
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions app/views/orders/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Editing order</h1>

<%= render "form", order: @order %>

<br>

<div>
<%= link_to "Show this order", @order %> |
<%= link_to "Back to orders", orders_path %>
</div>
14 changes: 14 additions & 0 deletions app/views/orders/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>

<h1>Orders</h1>

<div id="orders">
<% @orders.each do |order| %>
<%= render order %>
<p>
<%= link_to "Show this order", order %>
</p>
<% end %>
</div>

<%= link_to "New order", new_order_path %>
1 change: 1 addition & 0 deletions app/views/orders/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @orders, partial: "orders/order", as: :order
9 changes: 9 additions & 0 deletions app/views/orders/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>New order</h1>

<%= render "form", order: @order %>

<br>

<div>
<%= link_to "Back to orders", orders_path %>
</div>
10 changes: 10 additions & 0 deletions app/views/orders/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>

<%= render @order %>

<div>
<%= link_to "Edit this order", edit_order_path(@order) %> |
<%= link_to "Back to orders", orders_path %>

<%= button_to "Destroy this order", @order, method: :delete %>
</div>
1 change: 1 addition & 0 deletions app/views/orders/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "orders/order", order: @order
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :orders
namespace :admin do
resources :schools
resources :school_years
Expand Down
57 changes: 57 additions & 0 deletions test/controllers/orders_controller_test.rb
Original file line number Diff line number Diff line change
@@ -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

47 changes: 47 additions & 0 deletions test/system/orders_test.rb
Original file line number Diff line number Diff line change
@@ -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