Refactoring for Gin, NoSurf and SCS continues.

This commit is contained in:
2025-10-24 13:08:53 +01:00
parent 7276903733
commit fb07c4a5eb
61 changed files with 546 additions and 524 deletions

View File

@@ -41,7 +41,8 @@ func AdminOnly(db *sql.DB, next http.HandlerFunc) http.HandlerFunc {
})
}
func LogLoginAttempt(r *http.Request, username string, success bool) {
// Todo has to add in - db *sql.DB to make this work should this not be an import as all functions use it, more importantly no functions in storage just sql?
func LogLoginAttempt(db *sql.DB, r *http.Request, username string, success bool) {
ip := r.RemoteAddr
userAgent := r.UserAgent()

View File

@@ -0,0 +1,3 @@
// Currently no delete functions, only archiving to remove from user
// view but they can pull them back. Consider a soft delete which hides them from being unarchived for 5 years? then systematically delete after 5 years? or delete sooner but retain backup
package storage

View File

@@ -1,3 +0,0 @@
// Currently no delete functions, only archiving to remove from user
// view but they can pull them back. Consider a soft delete which hides them from being unarchived for 5 years?
// Then systematically delete after 5 years? or delete sooner but retain backup

View File

@@ -1,54 +0,0 @@
package storage
import (
"database/sql"
"log"
"synlotto-website/internal/logging"
"synlotto-website/internal/platform/config"
// ToDo: remove sqlite
_ "modernc.org/sqlite"
)
var db *sql.DB
func InitDB(filepath string) *sql.DB {
var err error
cfg := config.Get()
db, err = sql.Open("sqlite", filepath)
if err != nil {
log.Fatal("❌ Failed to open DB:", err)
}
schemas := []string{
SchemaUsers,
SchemaThunderballResults,
SchemaThunderballPrizes,
SchemaLottoResults,
SchemaMyTickets,
SchemaUsersMessages,
SchemaUsersNotifications,
SchemaAuditLog,
SchemaAuditLogin,
SchemaLogTicketMatching,
SchemaAdminAccessLog,
SchemaNewAuditLog,
SchemaSyndicates,
SchemaSyndicateMembers,
SchemaSyndicateInvites,
SchemaSyndicateInviteTokens,
}
if cfg == nil {
logging.Error("❌ config is nil — did config.Init() run before InitDB?")
panic("config not ready")
}
for _, stmt := range schemas {
if _, err := db.Exec(stmt); err != nil {
log.Fatalf("❌ Failed to apply schema: %v", err)
}
}
return db
}

View File

@@ -4,19 +4,19 @@ package storage
import (
"context"
"database/sql"
"errors"
"synlotto-website/internal/models"
)
type UsersRepo struct{ db *sql.DB}
type UsersRepo struct{ db *sql.DB }
func NewUsersRepo(db *.sql.DB) *UsersRepo { return &UsersRepo{db: db} }
func NewUsersRepo(db *sql.DB) *UsersRepo {
return &UsersRepo{db: db}
}
// ToDo: should the function be in sql?
func (r *UsersRepo) Create(ctx context.Context, username, passwordHash string, isAdmin bool) error {
_, err := r.db.ExecContext(ctx,
`INSERT INTO users (username, password_hash, is_admin) VALUES (?, ?, ?)`,
username, passwordHash, isAdmin,
)
return err
}
}