started work on task assignment table
This commit is contained in:
59
backend/pkg/db/availabilities/availabilities.go
Normal file
59
backend/pkg/db/availabilities/availabilities.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package availabilities
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
cache "github.com/jfarleyx/go-simple-cache"
|
||||
"github.com/johannesbuehl/golunteer/backend/pkg/db"
|
||||
)
|
||||
|
||||
type availabilitiesDB 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("availabilities"); !hit {
|
||||
refresh()
|
||||
|
||||
return nil, fmt.Errorf("availabilities not stored cached")
|
||||
} else {
|
||||
return availabilities.(map[int]Availability), nil
|
||||
}
|
||||
}
|
||||
|
||||
func refresh() {
|
||||
// get the availabilitiesRaw from the database
|
||||
var availabilitiesRaw []availabilitiesDB
|
||||
|
||||
if err := db.DB.Select(&availabilitiesRaw, "SELECT * FROM AVAILABILITIES"); err == nil {
|
||||
// convert the result in a map
|
||||
availabilities := map[int]Availability{}
|
||||
|
||||
for _, a := range availabilitiesRaw {
|
||||
availabilities[a.Id] = Availability{
|
||||
Text: a.Text,
|
||||
Disabled: a.Disabled,
|
||||
}
|
||||
}
|
||||
|
||||
c.Set("availabilities", availabilities)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
c = cache.New(24 * time.Hour)
|
||||
|
||||
c.OnExpired(refresh)
|
||||
|
||||
refresh()
|
||||
}
|
||||
36
backend/pkg/db/availabilities/userAvailabilities.go
Normal file
36
backend/pkg/db/availabilities/userAvailabilities.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package availabilities
|
||||
|
||||
import (
|
||||
"github.com/johannesbuehl/golunteer/backend/pkg/db"
|
||||
"github.com/johannesbuehl/golunteer/backend/pkg/db/users"
|
||||
)
|
||||
|
||||
type eventAvailabilities struct {
|
||||
UserName string `db:"userName"`
|
||||
AvailabilityID int `db:"availabilityID"`
|
||||
}
|
||||
|
||||
func Event(eventID int) (map[string]string, error) {
|
||||
// get the availabilities for the event
|
||||
var availabilitiesRows []eventAvailabilities
|
||||
|
||||
if err := db.DB.Select(&availabilitiesRows, "SELECT userName, availabilityID FROM USER_AVAILABILITIES WHERE eventID = ?", eventID); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
// transform the result into a map
|
||||
eventAvailabilities := map[string]string{}
|
||||
|
||||
// get the availabilities
|
||||
if availabilitiesMap, err := Keys(); err != nil {
|
||||
return nil, err
|
||||
} else if usersMap, err := users.Get(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
for _, a := range availabilitiesRows {
|
||||
eventAvailabilities[usersMap[a.UserName].Name] = availabilitiesMap[a.AvailabilityID].Text
|
||||
}
|
||||
|
||||
return eventAvailabilities, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user