Enha: sqlx instead of pgx

This commit is contained in:
Grail Finder
2025-07-01 16:00:17 +03:00
parent 70f83f1002
commit e989590e74
18 changed files with 631 additions and 72 deletions

View File

@ -72,6 +72,10 @@ func getStateByCtx(ctx context.Context) (*models.UserState, error) {
return us, nil
}
// func dbCreate(fi *models.FullInfo) error{
// repo.CreateRoom()
// }
func saveFullInfo(fi *models.FullInfo) error {
// INFO: no transactions; so case is possible where first object is updated but the second is not
if err := saveState(fi.State.Username, fi.State); err != nil {

View File

@ -43,6 +43,10 @@ func HandleCreateRoom(w http.ResponseWriter, r *http.Request) {
fi.State.RoomID = room.ID
fi.Room = room
fi.Room.IsPublic = true // hardcode for local test; move to form
if err := repo.CreateRoom(r.Context(), room); err != nil {
abortWithError(w, err.Error())
return
}
if err := saveFullInfo(fi); err != nil {
msg := "failed to set current room to session"
log.Error(msg, "error", err)

View File

@ -5,6 +5,7 @@ import (
"gralias/config"
"gralias/models"
"gralias/pkg/cache"
"gralias/repos"
"html/template"
"log/slog"
"net/http"
@ -17,6 +18,7 @@ var (
cfg *config.Config
memcache cache.Cache
Notifier *broker.Broker
repo repos.AllRepos
)
func init() {
@ -30,6 +32,7 @@ func init() {
cache.MemCache.StartBackupRoutine(15 * time.Second) // Reduced backup interval
// bot loader
// check the rooms if it has bot_{digits} in them, create bots if have
repo = repos.NewRepoProvider("sqlite3://../gralias.db")
recoverBots()
// if player has a roomID, but no team and role, try to recover
recoverPlayers()
@ -57,7 +60,11 @@ func HandleHome(w http.ResponseWriter, r *http.Request) {
}
}
if fi != nil && fi.Room == nil {
fi.List = listRooms(false)
rooms, err := repo.ListRooms(r.Context())
if err != nil {
log.Error("failed to list rooms;", "error", err)
}
fi.List = rooms
}
if err := tmpl.ExecuteTemplate(w, "base", fi); err != nil {
log.Error("failed to exec templ;", "error", err, "templ", "base")

View File

View File

@ -54,9 +54,9 @@ func StartTurnTimer(roomID string, duration time.Duration) {
return
}
room.Settings.TurnSecondsLeft--
if err := saveRoom(room); err != nil {
log.Error("failed to save room", "error", err)
}
// if err := saveRoom(room); err != nil {
// log.Error("failed to save room", "error", err)
// }
notify(models.NotifyRoomUpdatePrefix+room.ID, "")
}
}