From 123f9c5957a4cc7e9001a458dccac895a81b81e3 Mon Sep 17 00:00:00 2001 From: nikurasu Date: Sat, 9 Mar 2024 20:19:11 +0100 Subject: [PATCH] feat(display-attendies): save in db The waiting list is now saved in the db because before doing so the wating list was calculated on each request on the ui AFTER getting only the attendies that allowed the display which leads to false results. And it is now possible to get if an attendie is on the waiting list even if we only want do get one attendie via the API --- src/app/controller/attendies_controller.go | 24 ++++------------------ src/app/util/attendiesList.go | 16 +++++++++++++++ src/app/util/pretix.go | 11 ++++++++-- src/app/util/roleHelper.go | 15 ++++++++++++++ src/app/views/table.html | 8 ++++++-- src/entities/attendiesList.go | 10 +++++++++ src/entities/db_attendies.go | 1 + 7 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 src/app/util/roleHelper.go diff --git a/src/app/controller/attendies_controller.go b/src/app/controller/attendies_controller.go index fc1fb9e..b757b65 100644 --- a/src/app/controller/attendies_controller.go +++ b/src/app/controller/attendies_controller.go @@ -1,15 +1,11 @@ package controller import ( - "sort" - "github.com/gofiber/fiber/v2" "github.com/nicksnyder/go-i18n/v2/i18n" "golang.org/x/text/language" "ulmer-furs.de/pretix-proxy/v2/app/service" - "ulmer-furs.de/pretix-proxy/v2/app/util" "ulmer-furs.de/pretix-proxy/v2/config" - "ulmer-furs.de/pretix-proxy/v2/entities" ) // @Description Get the attendies of an event by the name @@ -100,22 +96,10 @@ func GetAttendiesByEventPublicTable(c *fiber.Ctx) error { MessageID: "No", }), } - sort.Sort(entities.ByRegistrationTime(attendies)) - roleSuiter, _ := service.GetRoleByName("Suiter") - roleSpotter, _ := service.GetRoleByName("Spotter") - roleSpecialAnimal, _ := service.GetRoleByName("SpecialAnimal") - suiterWaitingList := util.CreateSuiterWaitingList( - &attendies, - event.SuiterPerSpotter, - &roleSuiter, - &roleSpotter, - &roleSpecialAnimal, - ) return c.Render("app/views/index", fiber.Map{ - "Event Name": event.Name, - "Attendies": attendies, - "SuiterWaitingList": suiterWaitingList, - "Locals": localizations, - "EventType": *(event).EventType, + "Event Name": event.Name, + "Attendies": attendies, + "Locals": localizations, + "EventType": *(event).EventType, }) } diff --git a/src/app/util/attendiesList.go b/src/app/util/attendiesList.go index c409b54..69a4f1c 100644 --- a/src/app/util/attendiesList.go +++ b/src/app/util/attendiesList.go @@ -23,3 +23,19 @@ func CreateSuiterWaitingList(attendiesList *entities.AttendiesList, suiterPerSpo } return suiterWatinglist } + +func MarkWatingListAttendies(attendiesList *entities.AttendiesList, suiterPerSpotter *int, suiter, spotter, specialAnimal *entities.Role) { + spotterCount := attendiesList.CountByRole(*spotter) + suiterCount := spotterCount * *suiterPerSpotter + var suiterCounter int + var i int + for i < len(*attendiesList) { + if (*attendiesList)[i].Role == *suiter || (*attendiesList)[i].Role == *specialAnimal { + suiterCounter++ + } + if suiterCounter > suiterCount && ((*attendiesList)[i].Role == *suiter || (*attendiesList)[i].Role == *specialAnimal) { + (*attendiesList)[i].OnWaitingList = true + } + i++ + } +} diff --git a/src/app/util/pretix.go b/src/app/util/pretix.go index e1aa249..df8e637 100644 --- a/src/app/util/pretix.go +++ b/src/app/util/pretix.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "sort" "strconv" "golang.org/x/exp/slices" @@ -78,7 +79,7 @@ func GetAttendieListFromPretixJson(pretixEvent entities.Pretix_Event, event enti } func GetAttendiesFromPretixJsonWithRoleQuestion(pretixEvent *entities.Pretix_Event, event *entities.Db_Event) ([]entities.Db_Attendies, error) { - var attendies []entities.Db_Attendies + var attendies entities.AttendiesList for _, order := range pretixEvent.Results { if order.Status == "c" || order.Status == "e" { continue @@ -129,11 +130,14 @@ func GetAttendiesFromPretixJsonWithRoleQuestion(pretixEvent *entities.Pretix_Eve attendies = append(attendies, attendie) } + _, spotter, suiter, _, specialAnimal := GetRoles() + sort.Sort(entities.ByRegistrationTime(attendies)) + MarkWatingListAttendies(&attendies, (*event).SuiterPerSpotter, &suiter, &spotter, &specialAnimal) return attendies, nil } func GetAttendiesFromPretixJsonWithRoleProduct(pretixEvent *entities.Pretix_Event, event *entities.Db_Event) ([]entities.Db_Attendies, error) { - var attendies []entities.Db_Attendies + var attendies entities.AttendiesList for _, order := range pretixEvent.Results { if order.Status == "c" || order.Status == "e" { continue @@ -182,5 +186,8 @@ func GetAttendiesFromPretixJsonWithRoleProduct(pretixEvent *entities.Pretix_Even attendies = append(attendies, attendie) } + _, spotter, suiter, _, specialAnimal := GetRoles() + sort.Sort(entities.ByRegistrationTime(attendies)) + MarkWatingListAttendies(&attendies, (*event).SuiterPerSpotter, &suiter, &spotter, &specialAnimal) return attendies, nil } diff --git a/src/app/util/roleHelper.go b/src/app/util/roleHelper.go new file mode 100644 index 0000000..405188f --- /dev/null +++ b/src/app/util/roleHelper.go @@ -0,0 +1,15 @@ +package util + +import ( + "ulmer-furs.de/pretix-proxy/v2/app/service" + "ulmer-furs.de/pretix-proxy/v2/entities" +) + +func GetRoles() (guest, spotter, suiter, photograph, specialAnimal entities.Role) { + guest, _ = service.GetRoleByName("Guest") + spotter, _ = service.GetRoleByName("Spotter") + suiter, _ = service.GetRoleByName("Suiter") + photograph, _ = service.GetRoleByName("Photograph") + specialAnimal, _ = service.GetRoleByName("SpecialAnimal") + return +} diff --git a/src/app/views/table.html b/src/app/views/table.html index 3304604..c41f343 100644 --- a/src/app/views/table.html +++ b/src/app/views/table.html @@ -13,6 +13,7 @@ {{range $suiter := .Attendies }} + {{if not $suiter.OnWaitingList }} {{ $suiter.Nickname }} {{ if eq $suiter.Role.Name "Suiter" }} @@ -35,9 +36,10 @@ {{ end }} {{end}} + {{end}} -{{ if .SuiterWaitingList }} +{{ if gt .Attendies.CountWaitingList 0 }}

{{ .Locals.SuiterWaitinglist }}

{{ .Locals.WaitinglistDesc}}

@@ -51,7 +53,8 @@ - {{range $suiter := .SuiterWaitingList }} + {{range $suiter := .Attendies }} + {{if $suiter.OnWaitingList }} {{ if eq $suiter.Role.Name "Suiter" }} @@ -74,6 +77,7 @@ {{ end }} {{end}} + {{end}}
{{ $suiter.Nickname }}
{{ end }} diff --git a/src/entities/attendiesList.go b/src/entities/attendiesList.go index fd49b78..595b2ea 100644 --- a/src/entities/attendiesList.go +++ b/src/entities/attendiesList.go @@ -11,3 +11,13 @@ func (a AttendiesList) CountByRole(role Role) int { } return count } + +func (a AttendiesList) CountWaitingList() int { + var count int + for _, attendie := range a { + if attendie.OnWaitingList { + count++ + } + } + return count +} diff --git a/src/entities/db_attendies.go b/src/entities/db_attendies.go index e998df9..c5bdd99 100644 --- a/src/entities/db_attendies.go +++ b/src/entities/db_attendies.go @@ -30,4 +30,5 @@ type Db_Attendies struct { AttendsRestaurant bool `gorm:"column:attends_restaurant;not null"` Privacy bool `gorm:"column:privacy;not null"` RegistrationTime time.Time `gorm:"column:registration_time;not null;default:CURRENT_TIMESTAMP"` + OnWaitingList bool `gorm:"column:on_wating_list;not null;default:false"` }