Module: Rawbotz::RawbotzApp::Routing::Products

Includes:
Rawbotz, RawgentoModels
Defined in:
lib/rawbotz/routes/products.rb

Constant Summary

Constants included from Rawbotz

VERSION

Class Method Summary collapse

Methods included from Rawbotz

attribute_ids, conf_file_path, conf_file_path=, configure!, configure_logger, logger, logger=, mail, mech, new_mech

Class Method Details

.registered(app) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rawbotz/routes/products.rb', line 7

def self.registered(app)
  # app.get  '/products',           &show_products
  show_products = lambda do
    @products = LocalProduct.includes(:supplier, :remote_product)
    @suppliers = Supplier.includes(:local_products).order(:name).all
    haml "products/index".to_sym
  end

  # app.post '/products/search',    &search_products
  search_products = lambda do
    @products = LocalProduct
      .where('lower(name) LIKE ?', "%#{params[:term].downcase}%").limit(20).pluck(:name, :id)
    @products.map{|p| {name: p[0], product_id: p[1]}}.to_json
  end

  # app.get  '/product/:id',        &show_product
  show_product = lambda do
    settings = RawgentoDB.settings(Rawbotz.conf_file_path)
    @product = LocalProduct.unscoped.includes(:supplier).find(params[:id])
    begin
      product_id = @product.product_id
      @sales = Models::Sales.daily_since product_id
      @sales_monthly = Models::Sales.monthly_since product_id
    rescue Exception => e
      STDERR.puts e.message
      STDERR.puts e.backtrace
      @sales = []
      @sales_monthly = []
      add_flash :error, "Cannot connect to MySQL database/ #{e.message}"
    end
    @plot_data = Rawbotz::Datapolate.create_data @sales, @product.stock_items.where(created_at: (Date.today - 30)..Date.today)
    haml "product/view".to_sym
  end

  # app.get  '/product/:id/stock_sales_plot', &show_product
  show_product_stock_sales_plot = lambda do
    @product = LocalProduct.unscoped.find(params[:id])
    begin
      @sales = Models::Sales.daily_since(@product.product_id)
    rescue
      @sales = []
      add_flash :error, 'Cannot connect to MySQL database'
    end
    @plot_data = Rawbotz::Datapolate.create_data @sales, @product.stock_items
    haml "product/_stock_sales_plot".to_sym, layout: :thin_layout, locals: {plot_data: @plot_data}
  end

  # app.post '/product/:id/hide',   &hide_product
  hide_product = lambda do
    @product = RawgentoModels::LocalProduct.find(params[:id])
    @product.hidden = true
    @product.save

    add_flash :success, "Product '#{@product.name}' is now hidden"
    redirect back
  end

  # app.post '/product/:id/unhide', &unhide_product
  unhide_product = lambda do
    @product = RawgentoModels::LocalProduct.unscoped.find(params[:id])
    @product.hidden = false
    @product.save

    add_flash :success, "Product '#{@product.name}' is now not hidden anymore"
    redirect back
  end

  # app.get  '/remote_products',    &show_remote_products
  show_remote_products = lambda do
    @products = RawgentoModels::RemoteProduct.all
    haml "remote_products/index".to_sym
  end

  # app.post '/remote_products/search', &search_remote_products
  search_remote_products = lambda do
    @products = RemoteProduct.supplied_by(settings.supplier)
      .ilike(params[:term]).limit(20).pluck(:name, :id)
    @products.map do |p|
      {name: p[0], product_id: p[1]}
    end.to_json
  end

  # app.get  '/remote_product/:id',  &show_remote_product
  show_remote_product = lambda do
    @product = RawgentoModels::RemoteProduct.find(params[:id])
    haml "remote_product/view".to_sym
  end

  app.get  '/products',                     &show_products
  app.post '/products/search',              &search_products
  app.get  '/product/:id',                  &show_product
  app.get  '/product/:id/stock_sales_plot', &show_product_stock_sales_plot
  app.post '/product/:id/hide',             &hide_product
  app.post '/product/:id/unhide',           &unhide_product

  app.get  '/remote_products',        &show_remote_products
  app.post '/remote_products/search', &search_remote_products
  app.get  '/remote_product/:id',     &show_remote_product
end