🎉 initial commit
This commit is contained in:
44
devices-worker/handlers/device_handler.lua
Normal file
44
devices-worker/handlers/device_handler.lua
Normal file
@@ -0,0 +1,44 @@
|
||||
local db = require("db")
|
||||
local log = require("log")
|
||||
|
||||
local DeviceHandler = {}
|
||||
|
||||
function DeviceHandler.handle(event)
|
||||
local conn, err = db.get_connection()
|
||||
if not conn then
|
||||
error("Database connection failed: " .. tostring(err))
|
||||
end
|
||||
|
||||
local ok, handler_err = pcall(function()
|
||||
conn:query([[
|
||||
CREATE TABLE IF NOT EXISTS device_events (
|
||||
id SERIAL PRIMARY KEY,
|
||||
device_id INTEGER,
|
||||
device_name VARCHAR(255),
|
||||
event_type VARCHAR(100),
|
||||
processed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
]])
|
||||
|
||||
conn:query(
|
||||
"INSERT INTO device_events (device_id, device_name, event_type) VALUES ($1, $2, $3)",
|
||||
tonumber(event.device_id) or 0,
|
||||
event.device_name or "",
|
||||
event.event_type
|
||||
)
|
||||
end)
|
||||
|
||||
conn:disconnect()
|
||||
|
||||
if not ok then
|
||||
error(handler_err)
|
||||
end
|
||||
|
||||
log.info("Device event logged", {
|
||||
component = "device_handler",
|
||||
device_name = event.device_name,
|
||||
request_id = event.request_id,
|
||||
})
|
||||
end
|
||||
|
||||
return DeviceHandler
|
||||
45
devices-worker/handlers/rating_handler.lua
Normal file
45
devices-worker/handlers/rating_handler.lua
Normal file
@@ -0,0 +1,45 @@
|
||||
local db = require("db")
|
||||
local log = require("log")
|
||||
|
||||
local RatingHandler = {}
|
||||
|
||||
function RatingHandler.handle(event)
|
||||
local conn, err = db.get_connection()
|
||||
if not conn then
|
||||
error("Database connection failed: " .. tostring(err))
|
||||
end
|
||||
|
||||
local ok, handler_err = pcall(function()
|
||||
conn:query([[
|
||||
CREATE TABLE IF NOT EXISTS rating_events (
|
||||
id SERIAL PRIMARY KEY,
|
||||
device_id INTEGER,
|
||||
user_id VARCHAR(255),
|
||||
score INTEGER,
|
||||
processed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
]])
|
||||
|
||||
conn:query(
|
||||
"INSERT INTO rating_events (device_id, user_id, score) VALUES ($1, $2, $3)",
|
||||
tonumber(event.device_id) or 0,
|
||||
event.user_id or "",
|
||||
tonumber(event.score) or 0
|
||||
)
|
||||
end)
|
||||
|
||||
conn:disconnect()
|
||||
|
||||
if not ok then
|
||||
error(handler_err)
|
||||
end
|
||||
|
||||
log.info("Rating event logged", {
|
||||
component = "rating_handler",
|
||||
device_id = event.device_id,
|
||||
score = event.score,
|
||||
request_id = event.request_id,
|
||||
})
|
||||
end
|
||||
|
||||
return RatingHandler
|
||||
42
devices-worker/handlers/review_handler.lua
Normal file
42
devices-worker/handlers/review_handler.lua
Normal file
@@ -0,0 +1,42 @@
|
||||
local db = require("db")
|
||||
local log = require("log")
|
||||
|
||||
local ReviewHandler = {}
|
||||
|
||||
function ReviewHandler.handle(event)
|
||||
local conn, err = db.get_connection()
|
||||
if not conn then
|
||||
error("Database connection failed: " .. tostring(err))
|
||||
end
|
||||
|
||||
local ok, handler_err = pcall(function()
|
||||
conn:query([[
|
||||
CREATE TABLE IF NOT EXISTS review_events (
|
||||
id SERIAL PRIMARY KEY,
|
||||
device_id INTEGER,
|
||||
user_id VARCHAR(255),
|
||||
processed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
]])
|
||||
|
||||
conn:query(
|
||||
"INSERT INTO review_events (device_id, user_id) VALUES ($1, $2)",
|
||||
tonumber(event.device_id) or 0,
|
||||
event.user_id or ""
|
||||
)
|
||||
end)
|
||||
|
||||
conn:disconnect()
|
||||
|
||||
if not ok then
|
||||
error(handler_err)
|
||||
end
|
||||
|
||||
log.info("Review event logged", {
|
||||
component = "review_handler",
|
||||
device_id = event.device_id,
|
||||
request_id = event.request_id,
|
||||
})
|
||||
end
|
||||
|
||||
return ReviewHandler
|
||||
Reference in New Issue
Block a user