From c6a2a64bcedada5c70b2c5bae8bf6c5684825230 Mon Sep 17 00:00:00 2001 From: Karolina Date: Sun, 9 Dec 2018 22:04:09 +0100 Subject: [PATCH] zad6 --- zad6/app/controllers/customers_controller.rb | 6 +++ zad6/app/controllers/products_controller.rb | 6 +++ zad6/app/models/category.rb | 4 ++ zad6/app/models/category_product.rb | 4 ++ zad6/app/models/customer.rb | 4 ++ zad6/app/models/product.rb | 9 ++++ zad6/app/views/customers/index.html.erb | 20 ++++++++ zad6/app/views/products/index.html.erb | 21 ++++++++ zad6/config/routes.rb | 3 ++ .../migrate/20181209183630_create_products.rb | 12 +++++ .../20181209184023_create_customers.rb | 11 +++++ .../20181209184902_create_categories.rb | 10 ++++ ...20181209185037_create_category_products.rb | 9 ++++ zad6/db/schema.rb | 48 +++++++++++++++++++ 14 files changed, 167 insertions(+) create mode 100644 zad6/app/controllers/customers_controller.rb create mode 100644 zad6/app/controllers/products_controller.rb create mode 100644 zad6/app/models/category.rb create mode 100644 zad6/app/models/category_product.rb create mode 100644 zad6/app/models/customer.rb create mode 100644 zad6/app/models/product.rb create mode 100644 zad6/app/views/customers/index.html.erb create mode 100644 zad6/app/views/products/index.html.erb create mode 100644 zad6/db/migrate/20181209183630_create_products.rb create mode 100644 zad6/db/migrate/20181209184023_create_customers.rb create mode 100644 zad6/db/migrate/20181209184902_create_categories.rb create mode 100644 zad6/db/migrate/20181209185037_create_category_products.rb create mode 100644 zad6/db/schema.rb diff --git a/zad6/app/controllers/customers_controller.rb b/zad6/app/controllers/customers_controller.rb new file mode 100644 index 00000000..d95d1cd8 --- /dev/null +++ b/zad6/app/controllers/customers_controller.rb @@ -0,0 +1,6 @@ +class CustomersController < ApplicationController + def index + @customers = Customer.all + end + +end diff --git a/zad6/app/controllers/products_controller.rb b/zad6/app/controllers/products_controller.rb new file mode 100644 index 00000000..f3e40637 --- /dev/null +++ b/zad6/app/controllers/products_controller.rb @@ -0,0 +1,6 @@ +class ProductsController < ApplicationController + def index + @customer = Customer.find_by!(id: params[:customer_id]) + @products = @customer.products.sorted + end +end diff --git a/zad6/app/models/category.rb b/zad6/app/models/category.rb new file mode 100644 index 00000000..2a6eeceb --- /dev/null +++ b/zad6/app/models/category.rb @@ -0,0 +1,4 @@ +class Category < ApplicationRecord + has_many :products, through: :category_product + validates :name, presence: true +end diff --git a/zad6/app/models/category_product.rb b/zad6/app/models/category_product.rb new file mode 100644 index 00000000..3da9861b --- /dev/null +++ b/zad6/app/models/category_product.rb @@ -0,0 +1,4 @@ +class CategoryProduct < ApplicationRecord + belongs_to :product + belongs_to :category +end diff --git a/zad6/app/models/customer.rb b/zad6/app/models/customer.rb new file mode 100644 index 00000000..3c667d71 --- /dev/null +++ b/zad6/app/models/customer.rb @@ -0,0 +1,4 @@ +class Customer < ApplicationRecord + has_many :products + validates :name, presence: true +end diff --git a/zad6/app/models/product.rb b/zad6/app/models/product.rb new file mode 100644 index 00000000..dc92d10e --- /dev/null +++ b/zad6/app/models/product.rb @@ -0,0 +1,9 @@ +class Product < ApplicationRecord + belongs_to :customer + has_many :categories, through: :category_product + validates :name, presence: true + validates :price, numericality: {greater_than: 0.00} + + scope :sorted, -> { order(price: :desc, id: :asc)} + +end diff --git a/zad6/app/views/customers/index.html.erb b/zad6/app/views/customers/index.html.erb new file mode 100644 index 00000000..b3d0db6c --- /dev/null +++ b/zad6/app/views/customers/index.html.erb @@ -0,0 +1,20 @@ +
+

All customers

+ + + + + + + + + + <% @customers.each do |customer| %> + + + + + <% end %> + +
NameMail
<%= customer.name %><%= customer.mail %>
+
diff --git a/zad6/app/views/products/index.html.erb b/zad6/app/views/products/index.html.erb new file mode 100644 index 00000000..67ee40a6 --- /dev/null +++ b/zad6/app/views/products/index.html.erb @@ -0,0 +1,21 @@ +
+

Products (<%= @customer.name %>)

+ + + + + + + + + + <% @products.each do |p| %> + + + + + <% end %> + +
Product NameProduct Price
<%= p.name %><%= p.price %>
+
+ diff --git a/zad6/config/routes.rb b/zad6/config/routes.rb index 787824f8..0e75c806 100644 --- a/zad6/config/routes.rb +++ b/zad6/config/routes.rb @@ -1,3 +1,6 @@ Rails.application.routes.draw do + resources :customers do + resources :products + end # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end diff --git a/zad6/db/migrate/20181209183630_create_products.rb b/zad6/db/migrate/20181209183630_create_products.rb new file mode 100644 index 00000000..e4aeae7e --- /dev/null +++ b/zad6/db/migrate/20181209183630_create_products.rb @@ -0,0 +1,12 @@ +class CreateProducts < ActiveRecord::Migration[5.2] + def change + create_table :products do |t| + t.string :name, null: false + t.decimal :price, precision: 10, scale: 2 + t.text :description + t.belongs_to :customer, foreign_key: true + + t.timestamps + end + end +end diff --git a/zad6/db/migrate/20181209184023_create_customers.rb b/zad6/db/migrate/20181209184023_create_customers.rb new file mode 100644 index 00000000..7e727fe4 --- /dev/null +++ b/zad6/db/migrate/20181209184023_create_customers.rb @@ -0,0 +1,11 @@ +class CreateCustomers < ActiveRecord::Migration[5.2] + def change + create_table :customers do |t| + t.string :name, null: false + t.string :mail, null: false + + + t.timestamps + end + end +end diff --git a/zad6/db/migrate/20181209184902_create_categories.rb b/zad6/db/migrate/20181209184902_create_categories.rb new file mode 100644 index 00000000..cdda7bd2 --- /dev/null +++ b/zad6/db/migrate/20181209184902_create_categories.rb @@ -0,0 +1,10 @@ +class CreateCategories < ActiveRecord::Migration[5.2] + def change + create_table :categories do |t| + t.string :name + t.text :descripton + + t.timestamps + end + end +end diff --git a/zad6/db/migrate/20181209185037_create_category_products.rb b/zad6/db/migrate/20181209185037_create_category_products.rb new file mode 100644 index 00000000..61f328a8 --- /dev/null +++ b/zad6/db/migrate/20181209185037_create_category_products.rb @@ -0,0 +1,9 @@ +class CreateCategoryProducts < ActiveRecord::Migration[5.2] + def change + create_table :category_products do |t| + t.belongs_to :product, index: true + t.belongs_to :category, index: true + t.timestamps + end + end +end diff --git a/zad6/db/schema.rb b/zad6/db/schema.rb new file mode 100644 index 00000000..d3833999 --- /dev/null +++ b/zad6/db/schema.rb @@ -0,0 +1,48 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 2018_12_09_194146) do + + create_table "categories", force: :cascade do |t| + t.string "name" + t.text "descripton" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "category_products", force: :cascade do |t| + t.integer "product_id" + t.integer "category_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["category_id"], name: "index_category_products_on_category_id" + t.index ["product_id"], name: "index_category_products_on_product_id" + end + + create_table "customers", force: :cascade do |t| + t.string "name", null: false + t.string "mail", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "products", force: :cascade do |t| + t.string "name", null: false + t.decimal "price", precision: 10, scale: 2 + t.text "description" + t.integer "customer_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["customer_id"], name: "index_products_on_customer_id" + end + +end