From 0f60be448d6d85aa20a0dfa60b88d8475cf0d5ae Mon Sep 17 00:00:00 2001 From: H3ALY Date: Thu, 23 Oct 2025 21:51:15 +0100 Subject: [PATCH] Refactoring continues. --- internal/handlers/admin/manualtriggers.go | 2 +- .../lottery/tickets/ticket_handler.go | 2 +- internal/handlers/messages.go | 4 +- internal/helpers/template/build.go | 2 +- internal/platform/bootstrap/session.go | 2 +- internal/services/tickets/engine.go | 4 +- internal/services/tickets/ticketmatching.go | 5 +- internal/storage/mysql/schema.go | 237 +++++++++++++++++ .../sqlite-db-for-reference.go} | 0 .../thunderball/statisticqueries.go | 1 + .../{sqlite => mysql/syndicate}/syndicate.go | 0 internal/storage/sqlite/schema.go | 238 ------------------ 12 files changed, 248 insertions(+), 249 deletions(-) rename internal/storage/{sqlite/db.go => mysql/sqlite-db-for-reference.go} (100%) rename internal/storage/{sqlite => mysql/statistics}/thunderball/statisticqueries.go (98%) rename internal/storage/{sqlite => mysql/syndicate}/syndicate.go (100%) delete mode 100644 internal/storage/sqlite/schema.go diff --git a/internal/handlers/admin/manualtriggers.go b/internal/handlers/admin/manualtriggers.go index 617594a..e7b8150 100644 --- a/internal/handlers/admin/manualtriggers.go +++ b/internal/handlers/admin/manualtriggers.go @@ -9,7 +9,7 @@ import ( "strconv" templateHelpers "synlotto-website/internal/helpers/template" - services "synlotto-website/services/tickets" + services "synlotto-website/internal/services/tickets" "synlotto-website/internal/models" ) diff --git a/internal/handlers/lottery/tickets/ticket_handler.go b/internal/handlers/lottery/tickets/ticket_handler.go index 204b71a..4c13ef3 100644 --- a/internal/handlers/lottery/tickets/ticket_handler.go +++ b/internal/handlers/lottery/tickets/ticket_handler.go @@ -13,7 +13,7 @@ import ( httpHelpers "synlotto-website/internal/helpers/http" securityHelpers "synlotto-website/internal/helpers/security" templateHelpers "synlotto-website/internal/helpers/template" - draws "synlotto-website/services/draws" + draws "synlotto-website/internal/services/draws" "synlotto-website/internal/helpers" "synlotto-website/internal/models" diff --git a/internal/handlers/messages.go b/internal/handlers/messages.go index 0b528ad..67a7134 100644 --- a/internal/handlers/messages.go +++ b/internal/handlers/messages.go @@ -6,12 +6,12 @@ import ( "net/http" templateHandlers "synlotto-website/internal/handlers/template" - "synlotto-website/internal/helpers" httpHelpers "synlotto-website/internal/helpers/http" securityHelpers "synlotto-website/internal/helpers/security" templateHelpers "synlotto-website/internal/helpers/template" - "synlotto-website/internal/storage" + "synlotto-website/internal/helpers" + storage "synlotto-website/internal/storage/mysql" ) func MessagesInboxHandler(db *sql.DB) http.HandlerFunc { diff --git a/internal/helpers/template/build.go b/internal/helpers/template/build.go index f5216e1..e1f80b5 100644 --- a/internal/helpers/template/build.go +++ b/internal/helpers/template/build.go @@ -7,9 +7,9 @@ import ( "strings" "time" - "synlotto-website/config" helpers "synlotto-website/internal/helpers/http" "synlotto-website/internal/models" + "synlotto-website/internal/platform/config" "github.com/gorilla/csrf" ) diff --git a/internal/platform/bootstrap/session.go b/internal/platform/bootstrap/session.go index 9dbc217..4192658 100644 --- a/internal/platform/bootstrap/session.go +++ b/internal/platform/bootstrap/session.go @@ -13,8 +13,8 @@ import ( sessionHandlers "synlotto-website/internal/handlers/session" sessionHelpers "synlotto-website/internal/helpers/session" + "synlotto-website/internal/logging" "synlotto-website/internal/models" - "synlotto-website/logging" "github.com/gorilla/sessions" ) diff --git a/internal/services/tickets/engine.go b/internal/services/tickets/engine.go index 49b0313..a0708d8 100644 --- a/internal/services/tickets/engine.go +++ b/internal/services/tickets/engine.go @@ -1,11 +1,11 @@ -package matcher +package services import ( "database/sql" "fmt" "synlotto-website/internal/helpers" "synlotto-website/internal/models" - thunderballRules "synlotto-website/rules" + thunderballRules "synlotto-website/internal/rules/thunderball" ) func MatchTicketToDraw(ticket models.MatchTicket, draw models.DrawResult, rules []models.PrizeRule, db *sql.DB) models.MatchResult { diff --git a/internal/services/tickets/ticketmatching.go b/internal/services/tickets/ticketmatching.go index ba55e5a..4843f76 100644 --- a/internal/services/tickets/ticketmatching.go +++ b/internal/services/tickets/ticketmatching.go @@ -6,12 +6,11 @@ import ( "log" lotteryTicketHandlers "synlotto-website/internal/handlers/lottery/tickets" - thunderballrules "synlotto-website/rules" - services "synlotto-website/services/draws" + thunderballrules "synlotto-website/internal/rules/thunderball" + services "synlotto-website/internal/services/draws" "synlotto-website/internal/helpers" "synlotto-website/internal/models" - "synlotto-website/matcher" ) func RunTicketMatching(db *sql.DB, triggeredBy string) (models.MatchRunStats, error) { diff --git a/internal/storage/mysql/schema.go b/internal/storage/mysql/schema.go index 82be054..e80c90b 100644 --- a/internal/storage/mysql/schema.go +++ b/internal/storage/mysql/schema.go @@ -1 +1,238 @@ package storage + +const SchemaUsers = ` +CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT NOT NULL UNIQUE, + password_hash TEXT NOT NULL, + is_admin BOOLEAN +);` + +const SchemaThunderballResults = ` +CREATE TABLE IF NOT EXISTS results_thunderball ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + draw_date TEXT NOT NULL UNIQUE, + draw_id INTEGER NOT NULL UNIQUE, + machine TEXT, + ballset TEXT, + ball1 INTEGER, + ball2 INTEGER, + ball3 INTEGER, + ball4 INTEGER, + ball5 INTEGER, + thunderball INTEGER +);` + +const SchemaThunderballPrizes = ` +CREATE TABLE IF NOT EXISTS prizes_thunderball ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + draw_id INTEGER NOT NULL, + draw_date TEXT, + prize1 TEXT, + prize1_winners INTEGER, + prize1_per_winner INTEGER, + prize1_fund INTEGER, + prize2 TEXT, + prize2_winners INTEGER, + prize2_per_winner INTEGER, + prize2_fund INTEGER, + prize3 TEXT, + prize3_winners INTEGER, + prize3_per_winner INTEGER, + prize3_fund INTEGER, + prize4 TEXT, + prize4_winners INTEGER, + prize4_per_winner INTEGER, + prize4_fund INTEGER, + prize5 TEXT, + prize5_winners INTEGER, + prize5_per_winner INTEGER, + prize5_fund INTEGER, + prize6 TEXT, + prize6_winners INTEGER, + prize6_per_winner INTEGER, + prize6_fund INTEGER, + prize7 TEXT, + prize7_winners INTEGER, + prize7_per_winner INTEGER, + prize7_fund INTEGER, + prize8 TEXT, + prize8_winners INTEGER, + prize8_per_winner INTEGER, + prize8_fund INTEGER, + prize9 TEXT, + prize9_winners INTEGER, + prize9_per_winner INTEGER, + prize9_fund INTEGER, + total_winners INTEGER, + total_prize_fund INTEGER, + FOREIGN KEY (draw_date) REFERENCES results_thunderball(draw_date) +);` + +const SchemaLottoResults = ` +CREATE TABLE IF NOT EXISTS results_lotto ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + draw_date TEXT NOT NULL UNIQUE, + draw_id INTEGER NOT NULL UNIQUE, + machine TEXT, + ballset TEXT, + ball1 INTEGER, + ball2 INTEGER, + ball3 INTEGER, + ball4 INTEGER, + ball5 INTEGER, + ball6 INTEGER, + bonusball INTEGER +);` + +const SchemaMyTickets = ` +CREATE TABLE IF NOT EXISTS my_tickets ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + userId INTEGER NOT NULL, + game_type TEXT NOT NULL, + draw_date TEXT NOT NULL, + ball1 INTEGER, + ball2 INTEGER, + ball3 INTEGER, + ball4 INTEGER, + ball5 INTEGER, + ball6 INTEGER, + bonus1 INTEGER, + bonus2 INTEGER, + duplicate BOOLEAN DEFAULT 0, + purchase_date TEXT, + purchase_method TEXT, + image_path TEXT, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + matched_main INTEGER, + matched_bonus INTEGER, + prize_tier TEXT, + is_winner BOOLEAN, + prize_amount INTEGER, + prize_label TEXT, + syndicate_id INTEGER, + FOREIGN KEY (userId) REFERENCES users(id) +);` + +const SchemaUsersMessages = ` +CREATE TABLE IF NOT EXISTS users_messages ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + senderId INTEGER NOT NULL REFERENCES users(id), + recipientId INTEGER NOT NULL REFERENCES users(id), + subject TEXT NOT NULL, + message TEXT, + is_read BOOLEAN DEFAULT FALSE, + is_archived BOOLEAN DEFAULT FALSE, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + archived_at TIMESTAMP +);` + +const SchemaUsersNotifications = ` +CREATE TABLE IF NOT EXISTS users_notification ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER NOT NULL REFERENCES users(id), + subject TEXT, + body TEXT, + is_read BOOLEAN DEFAULT FALSE, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +);` + +const SchemaAuditLog = ` +CREATE TABLE IF NOT EXISTS auditlog ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT, + success INTEGER, + timestamp TEXT +);` + +const SchemaLogTicketMatching = ` +CREATE TABLE IF NOT EXISTS log_ticket_matching ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + triggered_by TEXT, + run_at DATETIME DEFAULT CURRENT_TIMESTAMP, + tickets_matched INTEGER, + winners_found INTEGER, + notes TEXT +);` + +const SchemaAdminAccessLog = ` +CREATE TABLE IF NOT EXISTS admin_access_log ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER, + accessed_at DATETIME DEFAULT CURRENT_TIMESTAMP, + path TEXT, + ip TEXT, + user_agent TEXT +);` + +const SchemaNewAuditLog = ` +CREATE TABLE IF NOT EXISTS audit_log ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER, + username TEXT, + action TEXT, + path TEXT, + ip TEXT, + user_agent TEXT, + timestamp DATETIME DEFAULT CURRENT_TIMESTAMP +);` + +const SchemaAuditLogin = ` + CREATE TABLE IF NOT EXISTS audit_login ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username TEXT, + success BOOLEAN, + ip TEXT, + user_agent TEXT, + timestamp DATETIME DEFAULT CURRENT_TIMESTAMP + );` + +const SchemaSyndicates = ` +CREATE TABLE IF NOT EXISTS syndicates ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + description TEXT, + owner_id INTEGER NOT NULL, + join_code TEXT UNIQUE, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (owner_id) REFERENCES users(id) +);` + +const SchemaSyndicateMembers = ` +CREATE TABLE IF NOT EXISTS syndicate_members ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + syndicate_id INTEGER NOT NULL, + user_id INTEGER NOT NULL, + role TEXT DEFAULT 'member', -- owner, manager, member + status TEXT DEFAULT 'active', + joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (syndicate_id) REFERENCES syndicates(id), + FOREIGN KEY (user_id) REFERENCES users(id) +);` + +const SchemaSyndicateInvites = ` +CREATE TABLE IF NOT EXISTS syndicate_invites ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + syndicate_id INTEGER NOT NULL, + invited_user_id INTEGER NOT NULL, + sent_by_user_id INTEGER NOT NULL, + status TEXT DEFAULT 'pending', + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY(syndicate_id) REFERENCES syndicates(id), + FOREIGN KEY(invited_user_id) REFERENCES users(id) +);` + +const SchemaSyndicateInviteTokens = ` +CREATE TABLE IF NOT EXISTS syndicate_invite_tokens ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + syndicate_id INTEGER NOT NULL, + token TEXT NOT NULL UNIQUE, + invited_by_user_id INTEGER NOT NULL, + accepted_by_user_id INTEGER, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + accepted_at TIMESTAMP, + expires_at TIMESTAMP, + FOREIGN KEY (syndicate_id) REFERENCES syndicates(id), + FOREIGN KEY (invited_by_user_id) REFERENCES users(id), + FOREIGN KEY (accepted_by_user_id) REFERENCES users(id) +);` diff --git a/internal/storage/sqlite/db.go b/internal/storage/mysql/sqlite-db-for-reference.go similarity index 100% rename from internal/storage/sqlite/db.go rename to internal/storage/mysql/sqlite-db-for-reference.go diff --git a/internal/storage/sqlite/thunderball/statisticqueries.go b/internal/storage/mysql/statistics/thunderball/statisticqueries.go similarity index 98% rename from internal/storage/sqlite/thunderball/statisticqueries.go rename to internal/storage/mysql/statistics/thunderball/statisticqueries.go index 59f38d9..832ae34 100644 --- a/internal/storage/sqlite/thunderball/statisticqueries.go +++ b/internal/storage/mysql/statistics/thunderball/statisticqueries.go @@ -1,5 +1,6 @@ package storage +// ToDo: See these are queries, alot of the others are functions with queries in, remove the functions and lea\ve the sql?... // ToDo: The last seen statistic is done in days, maybe change or add in how many draws x days ways for ease. // Top 5 main numbers since inception of the game. const top5AllTime = ` diff --git a/internal/storage/sqlite/syndicate.go b/internal/storage/mysql/syndicate/syndicate.go similarity index 100% rename from internal/storage/sqlite/syndicate.go rename to internal/storage/mysql/syndicate/syndicate.go diff --git a/internal/storage/sqlite/schema.go b/internal/storage/sqlite/schema.go deleted file mode 100644 index e80c90b..0000000 --- a/internal/storage/sqlite/schema.go +++ /dev/null @@ -1,238 +0,0 @@ -package storage - -const SchemaUsers = ` -CREATE TABLE IF NOT EXISTS users ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - username TEXT NOT NULL UNIQUE, - password_hash TEXT NOT NULL, - is_admin BOOLEAN -);` - -const SchemaThunderballResults = ` -CREATE TABLE IF NOT EXISTS results_thunderball ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - draw_date TEXT NOT NULL UNIQUE, - draw_id INTEGER NOT NULL UNIQUE, - machine TEXT, - ballset TEXT, - ball1 INTEGER, - ball2 INTEGER, - ball3 INTEGER, - ball4 INTEGER, - ball5 INTEGER, - thunderball INTEGER -);` - -const SchemaThunderballPrizes = ` -CREATE TABLE IF NOT EXISTS prizes_thunderball ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - draw_id INTEGER NOT NULL, - draw_date TEXT, - prize1 TEXT, - prize1_winners INTEGER, - prize1_per_winner INTEGER, - prize1_fund INTEGER, - prize2 TEXT, - prize2_winners INTEGER, - prize2_per_winner INTEGER, - prize2_fund INTEGER, - prize3 TEXT, - prize3_winners INTEGER, - prize3_per_winner INTEGER, - prize3_fund INTEGER, - prize4 TEXT, - prize4_winners INTEGER, - prize4_per_winner INTEGER, - prize4_fund INTEGER, - prize5 TEXT, - prize5_winners INTEGER, - prize5_per_winner INTEGER, - prize5_fund INTEGER, - prize6 TEXT, - prize6_winners INTEGER, - prize6_per_winner INTEGER, - prize6_fund INTEGER, - prize7 TEXT, - prize7_winners INTEGER, - prize7_per_winner INTEGER, - prize7_fund INTEGER, - prize8 TEXT, - prize8_winners INTEGER, - prize8_per_winner INTEGER, - prize8_fund INTEGER, - prize9 TEXT, - prize9_winners INTEGER, - prize9_per_winner INTEGER, - prize9_fund INTEGER, - total_winners INTEGER, - total_prize_fund INTEGER, - FOREIGN KEY (draw_date) REFERENCES results_thunderball(draw_date) -);` - -const SchemaLottoResults = ` -CREATE TABLE IF NOT EXISTS results_lotto ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - draw_date TEXT NOT NULL UNIQUE, - draw_id INTEGER NOT NULL UNIQUE, - machine TEXT, - ballset TEXT, - ball1 INTEGER, - ball2 INTEGER, - ball3 INTEGER, - ball4 INTEGER, - ball5 INTEGER, - ball6 INTEGER, - bonusball INTEGER -);` - -const SchemaMyTickets = ` -CREATE TABLE IF NOT EXISTS my_tickets ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - userId INTEGER NOT NULL, - game_type TEXT NOT NULL, - draw_date TEXT NOT NULL, - ball1 INTEGER, - ball2 INTEGER, - ball3 INTEGER, - ball4 INTEGER, - ball5 INTEGER, - ball6 INTEGER, - bonus1 INTEGER, - bonus2 INTEGER, - duplicate BOOLEAN DEFAULT 0, - purchase_date TEXT, - purchase_method TEXT, - image_path TEXT, - created_at DATETIME DEFAULT CURRENT_TIMESTAMP, - matched_main INTEGER, - matched_bonus INTEGER, - prize_tier TEXT, - is_winner BOOLEAN, - prize_amount INTEGER, - prize_label TEXT, - syndicate_id INTEGER, - FOREIGN KEY (userId) REFERENCES users(id) -);` - -const SchemaUsersMessages = ` -CREATE TABLE IF NOT EXISTS users_messages ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - senderId INTEGER NOT NULL REFERENCES users(id), - recipientId INTEGER NOT NULL REFERENCES users(id), - subject TEXT NOT NULL, - message TEXT, - is_read BOOLEAN DEFAULT FALSE, - is_archived BOOLEAN DEFAULT FALSE, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - archived_at TIMESTAMP -);` - -const SchemaUsersNotifications = ` -CREATE TABLE IF NOT EXISTS users_notification ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - user_id INTEGER NOT NULL REFERENCES users(id), - subject TEXT, - body TEXT, - is_read BOOLEAN DEFAULT FALSE, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -);` - -const SchemaAuditLog = ` -CREATE TABLE IF NOT EXISTS auditlog ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - username TEXT, - success INTEGER, - timestamp TEXT -);` - -const SchemaLogTicketMatching = ` -CREATE TABLE IF NOT EXISTS log_ticket_matching ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - triggered_by TEXT, - run_at DATETIME DEFAULT CURRENT_TIMESTAMP, - tickets_matched INTEGER, - winners_found INTEGER, - notes TEXT -);` - -const SchemaAdminAccessLog = ` -CREATE TABLE IF NOT EXISTS admin_access_log ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - user_id INTEGER, - accessed_at DATETIME DEFAULT CURRENT_TIMESTAMP, - path TEXT, - ip TEXT, - user_agent TEXT -);` - -const SchemaNewAuditLog = ` -CREATE TABLE IF NOT EXISTS audit_log ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - user_id INTEGER, - username TEXT, - action TEXT, - path TEXT, - ip TEXT, - user_agent TEXT, - timestamp DATETIME DEFAULT CURRENT_TIMESTAMP -);` - -const SchemaAuditLogin = ` - CREATE TABLE IF NOT EXISTS audit_login ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - username TEXT, - success BOOLEAN, - ip TEXT, - user_agent TEXT, - timestamp DATETIME DEFAULT CURRENT_TIMESTAMP - );` - -const SchemaSyndicates = ` -CREATE TABLE IF NOT EXISTS syndicates ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT NOT NULL, - description TEXT, - owner_id INTEGER NOT NULL, - join_code TEXT UNIQUE, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (owner_id) REFERENCES users(id) -);` - -const SchemaSyndicateMembers = ` -CREATE TABLE IF NOT EXISTS syndicate_members ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - syndicate_id INTEGER NOT NULL, - user_id INTEGER NOT NULL, - role TEXT DEFAULT 'member', -- owner, manager, member - status TEXT DEFAULT 'active', - joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (syndicate_id) REFERENCES syndicates(id), - FOREIGN KEY (user_id) REFERENCES users(id) -);` - -const SchemaSyndicateInvites = ` -CREATE TABLE IF NOT EXISTS syndicate_invites ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - syndicate_id INTEGER NOT NULL, - invited_user_id INTEGER NOT NULL, - sent_by_user_id INTEGER NOT NULL, - status TEXT DEFAULT 'pending', - created_at DATETIME DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY(syndicate_id) REFERENCES syndicates(id), - FOREIGN KEY(invited_user_id) REFERENCES users(id) -);` - -const SchemaSyndicateInviteTokens = ` -CREATE TABLE IF NOT EXISTS syndicate_invite_tokens ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - syndicate_id INTEGER NOT NULL, - token TEXT NOT NULL UNIQUE, - invited_by_user_id INTEGER NOT NULL, - accepted_by_user_id INTEGER, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - accepted_at TIMESTAMP, - expires_at TIMESTAMP, - FOREIGN KEY (syndicate_id) REFERENCES syndicates(id), - FOREIGN KEY (invited_by_user_id) REFERENCES users(id), - FOREIGN KEY (accepted_by_user_id) REFERENCES users(id) -);`