From 3921db6166e2da895257496bb76dd115556699d3 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Sat, 29 Mar 2025 11:12:53 +0300 Subject: init --- internal/models/auth.go | 24 ++++++++++++++++++++++++ internal/models/models.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 internal/models/auth.go create mode 100644 internal/models/models.go (limited to 'internal/models') diff --git a/internal/models/auth.go b/internal/models/auth.go new file mode 100644 index 0000000..9964cd5 --- /dev/null +++ b/internal/models/auth.go @@ -0,0 +1,24 @@ +package models + +import ( + "time" +) + +// each session contains the username of the user and the time at which it expires +type Session struct { + Username string + Expiry time.Time +} + +// we'll use this method later to determine if the session has expired +func (s Session) IsExpired() bool { + return s.Expiry.Before(time.Now()) +} + +func ListUsernames(ss map[string]*Session) []string { + resp := make([]string, 0, len(ss)) + for _, s := range ss { + resp = append(resp, s.Username) + } + return resp +} diff --git a/internal/models/models.go b/internal/models/models.go new file mode 100644 index 0000000..5bc120b --- /dev/null +++ b/internal/models/models.go @@ -0,0 +1,45 @@ +package models + +import "time" + +type ActionType string + +const ( + ActionTypePlus ActionType = "ActionTypePlus" + ActionTypeMinus ActionType = "ActionTypeMinus" +) + +type ( + UserScore struct { + ID uint32 `db:"id"` + Username string `db:"username"` + Password string `db:"password"` + Actions []Action + Recommendations []Action + BurnTime time.Time `db:"burn_time"` + Score int8 `db:"score"` + CreatedAt time.Time `db:"created_at"` + } + Action struct { + ID uint32 `db:"id"` + Name string `db:"name"` + Magnitude uint8 `db:"magnitude"` + Repeatable bool `db:"repeatable"` + Type ActionType `db:"type"` + Done bool `db:"done"` + Username string `db:"username"` + CreatedAt time.Time `db:"created_at"` + } +) + +func (us *UserScore) UpdateScore(act *Action) { + switch act.Type { + case ActionTypePlus: + us.Score += int8(act.Magnitude) + if !act.Repeatable { + act.Done = true + } + case ActionTypeMinus: + us.Score -= int8(act.Magnitude) + } +} -- cgit v1.2.3