Class: MCPServer::DatabaseHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp_server/database_helper.rb

Overview

Helper class for database operations.

Class Method Summary collapse

Class Method Details

.add_or_update_firestore(product) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mcp_server/database_helper.rb', line 21

def self.add_or_update_firestore(product)
  firestore = MCPServer::Config.firestore
  query = firestore.col('inventory').where('name', '==', product[:name]).get

  if query.empty?
    firestore.col('inventory').add(product)
  else
    query.each do |doc|
      doc.ref.update(product)
    end
  end
end

.clean_firestore_collectionObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/mcp_server/database_helper.rb', line 104

def self.clean_firestore_collection
  MCPServer::Config.logger.info 'Cleaning Firestore collection...'
  firestore = MCPServer::Config.firestore
  snapshot = firestore.col('inventory').get
  return if snapshot.empty?

  batch = firestore.batch
  snapshot.each_with_index do |doc, index|
    batch.delete(doc.ref)
    if ((index + 1) % 400).zero?
      batch.commit
      batch = firestore.batch
    end
  end
  batch.commit if (snapshot.size % 400).positive?
  MCPServer::Config.logger.info 'Firestore collection cleaned.'
end

.doc_to_product(doc) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mcp_server/database_helper.rb', line 8

def self.doc_to_product(doc)
  data = doc.data
  {
    id: doc.document_id,
    name: data[:name],
    price: data[:price],
    quantity: data[:quantity],
    imgfile: data[:imgfile],
    timestamp: data[:timestamp],
    actualdateadded: data[:actualdateadded]
  }
end

.seed_databaseObject



34
35
36
37
38
# File 'lib/mcp_server/database_helper.rb', line 34

def self.seed_database
  seed_old_products
  seed_recent_products
  seed_out_of_stock_products
end

.seed_old_productsObject

rubocop:disable Metrics/MethodLength



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mcp_server/database_helper.rb', line 41

def self.seed_old_products
  names = [
    'Apples', 'Bananas', 'Milk', 'Whole Wheat Bread', 'Eggs', 'Cheddar Cheese',
    'Whole Chicken', 'Rice', 'Black Beans', 'Bottled Water', 'Apple Juice',
    'Cola', 'Coffee Beans', 'Green Tea', 'Watermelon', 'Broccoli',
    'Jasmine Rice', 'Yogurt', 'Beef', 'Shrimp', 'Walnuts',
    'Sunflower Seeds', 'Fresh Basil', 'Cinnamon'
  ]

  names.each do |name|
    product = {
      name: name,
      price: rand(1..10),
      quantity: rand(1..500),
      imgfile: "product-images/#{name.gsub(/\s+/, '').downcase}.png",
      timestamp: Time.now - rand(0..31_536_000) - 7_776_000,
      actualdateadded: Time.now
    }
    MCPServer::Config.logger.info "⬆️ Adding (or updating) product in firestore: #{product[:name]}"
    add_or_update_firestore(product)
  end
end

.seed_out_of_stock_productsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mcp_server/database_helper.rb', line 85

def self.seed_out_of_stock_products
  names = ['Wasabi Party Mix', 'Jalapeno Seasoning']

  names.each do |name|
    product = {
      name: name,
      price: rand(1..10),
      quantity: 0,
      imgfile: "product-images/#{name.gsub(/\s+/, '').downcase}.png",
      timestamp: Time.now - rand(0..518_400),
      actualdateadded: Time.now
    }
    MCPServer::Config.logger.info "😱 Adding (or updating) out of stock product in firestore: #{product[:name]}"
    add_or_update_firestore(product)
  end
end

.seed_recent_productsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mcp_server/database_helper.rb', line 64

def self.seed_recent_products
  names = [
    'Parmesan Crisps', 'Pineapple Kombucha', 'Maple Almond Butter',
    'Mint Chocolate Cookies', 'White Chocolate Caramel Corn', 'Acai Smoothie Packs',
    'Smores Cereal', 'Peanut Butter and Jelly Cups'
  ]

  names.each do |name|
    product = {
      name: name,
      price: rand(1..10),
      quantity: rand(1..100),
      imgfile: "product-images/#{name.gsub(/\s+/, '').downcase}.png",
      timestamp: Time.now - rand(0..518_400),
      actualdateadded: Time.now
    }
    MCPServer::Config.logger.info "🆕 Adding (or updating) product in firestore: #{product[:name]}"
    add_or_update_firestore(product)
  end
end