started working on the backend

This commit is contained in:
z1glr
2025-01-07 16:37:37 +00:00
parent 03b2b0e206
commit c3bc06fe82
34 changed files with 1437 additions and 54 deletions

View File

@@ -0,0 +1,59 @@
package availabilites
import (
"fmt"
"time"
cache "github.com/jfarleyx/go-simple-cache"
"github.com/johannesbuehl/golunteer/backend/pkg/db"
)
type availabilitesDB struct {
Id int `db:"id"`
Text string `db:"text"`
Disabled bool `db:"disabled"`
}
type Availability struct {
Text string
Disabled bool
}
var c *cache.Cache
func Keys() (map[int]Availability, error) {
if availabilities, hit := c.Get("availabilites"); !hit {
refresh()
return nil, fmt.Errorf("availabilites not stored cached")
} else {
return availabilities.(map[int]Availability), nil
}
}
func refresh() {
// get the availabilitesRaw from the database
var availabilitesRaw []availabilitesDB
if err := db.DB.Select(&availabilitesRaw, "SELECT * FROM AVAILABILITIES"); err == nil {
// convert the result in a map
availabilites := map[int]Availability{}
for _, a := range availabilitesRaw {
availabilites[a.Id] = Availability{
Text: a.Text,
Disabled: a.Disabled,
}
}
c.Set("availabilites", availabilites)
}
}
func init() {
c = cache.New(24 * time.Hour)
c.OnExpired(refresh)
refresh()
}

View File

@@ -0,0 +1,36 @@
package availabilites
import (
"github.com/johannesbuehl/golunteer/backend/pkg/db"
"github.com/johannesbuehl/golunteer/backend/pkg/db/users"
)
type eventAvailabilites struct {
userName string `db:"userName"`
AvailabilityID int `db:"availabilityID"`
}
func Event(eventID int) (map[string]string, error) {
// get the availabilites for the event
var availabilitesRows []eventAvailabilites
if err := db.DB.Select(&availabilitesRows, "SELECT (userID, availabilityID) FROM USER_AVAILABILITES WHERE eventID = ?", eventID); err != nil {
return nil, err
} else {
// transform the result into a map
eventAvailabilities := map[string]string{}
// get the availabilites
if availabilitesMap, err := Keys(); err != nil {
return nil, err
} else if usersMap, err := users.Get(); err != nil {
return nil, err
} else {
for _, a := range availabilitesRows {
eventAvailabilities[usersMap[a.userName].Name] = availabilitesMap[a.AvailabilityID].Text
}
return eventAvailabilities, nil
}
}
}