From cdd5e7469384fb1c482fab74cc8ada1e6859e7d0 Mon Sep 17 00:00:00 2001 From: nikurasu Date: Sat, 24 Feb 2024 11:40:11 +0100 Subject: [PATCH] feat(display-attendies): finish getting public attendies as JSON --- src/app/controller/attendies_controller.go | 36 ++------- src/app/controller/webhook_controller.go | 15 +++- src/app/routes/public_routes.go | 2 +- src/app/service/db_attendie.go | 86 ++++++++++++++++++++++ src/app/util/pretix.go | 18 ++++- src/entities/db_attendies.go | 11 ++- src/entities/db_event.go | 5 +- 7 files changed, 132 insertions(+), 41 deletions(-) create mode 100644 src/app/service/db_attendie.go diff --git a/src/app/controller/attendies_controller.go b/src/app/controller/attendies_controller.go index 38e1fa2..8b8fb26 100644 --- a/src/app/controller/attendies_controller.go +++ b/src/app/controller/attendies_controller.go @@ -1,45 +1,21 @@ package controller import ( - "encoding/json" - "fmt" - "io" - "net/http" - "github.com/gofiber/fiber/v2" "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 -func GetAttendiesByEvent(c *fiber.Ctx) error { +func GetAttendiesByEventPublic(c *fiber.Ctx) error { name := c.Params("name") event, err := service.Get_db_event_by_event(name) if err != nil { - return c.Status(404).SendString(err.Error()) + return c.Status(fiber.ErrNotFound.Code).SendString("event not found") } - req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/api/v1/organizers/%s/events/%s/orders", config.Env.Domain, *event.Organizer, *event.Event), nil) + attendies, err := service.GetAttendiesByEventPrivacy(event, false) if err != nil { - return c.Status(500).SendString("Internal Server Error") + return c.Status(fiber.ErrNotFound.Code).SendString("attendies not found") } - req.Header.Add("Authorization", fmt.Sprintf("Token %s", config.Env.ApiKeyPretix)) - resp, err := config.Client.Do(req) - if err != nil { - return c.Status(500).SendString("Internal Server Error") - } - respbody, err := io.ReadAll(resp.Body) - defer resp.Body.Close() - if err != nil { - return c.Status(500).SendString("Internal Server Error") - } - var unmarshaled_resp entities.Pretix_Event - if err := json.Unmarshal(respbody, &unmarshaled_resp); err != nil { - return c.Status(500).SendString("Internal Server Error") - } - - attendiesAtEvent := util.GetAttendieListFromPretixJson(unmarshaled_resp, event) - c.JSON(attendiesAtEvent) - return c.SendStatus(200) + c.JSON(attendies) + return c.SendStatus(fiber.StatusOK) } diff --git a/src/app/controller/webhook_controller.go b/src/app/controller/webhook_controller.go index f9027ff..5091363 100644 --- a/src/app/controller/webhook_controller.go +++ b/src/app/controller/webhook_controller.go @@ -21,11 +21,20 @@ func PostPretix(c *fiber.Ctx) error { return c.Status(500).SendString("Internal Server Error") } event, err := service.Get_db_event_by_event(data.Event) - _ = event if err != nil { return c.Status(fiber.StatusNotFound).SendString("Event not found") } - //fmt.Println(fmt.Printf("%+v\n", event)) - fmt.Println(fmt.Printf("%+v\n", pretixOrders)) + attendies, err := util.GetAttendiesFromPretixJson(pretixOrders, &event) + if err != nil { + fmt.Println(err) + } + if err := service.DeleteAttendiesByEvent(event); err != nil { + fmt.Println(err) + } + for _, attendie := range attendies { + if err := service.CreateNewAttendie(attendie); err != nil { + fmt.Print(err) + } + } return c.SendStatus(fiber.StatusOK) } diff --git a/src/app/routes/public_routes.go b/src/app/routes/public_routes.go index 13b237f..f389766 100644 --- a/src/app/routes/public_routes.go +++ b/src/app/routes/public_routes.go @@ -13,5 +13,5 @@ func PublicRoutes(app *fiber.App) { apiv1.Post("/login", controller.LoginUser) apiv1.Get("/event", controller.ReturnAllEventsPublic) apiv1.Get("/ping", controller.Ping) - attendies.Get("/:name", controller.GetAttendiesByEvent) + attendies.Get("/:name", controller.GetAttendiesByEventPublic) } diff --git a/src/app/service/db_attendie.go b/src/app/service/db_attendie.go new file mode 100644 index 0000000..e93ad9c --- /dev/null +++ b/src/app/service/db_attendie.go @@ -0,0 +1,86 @@ +package service + +import ( + "errors" + + "gorm.io/gorm" + "ulmer-furs.de/pretix-proxy/v2/config" + "ulmer-furs.de/pretix-proxy/v2/entities" +) + +func GetAttendieById(id int) (entities.Db_Attendies, error) { + var attendie entities.Db_Attendies + result := config.Database.Find(&attendie, id) + if result.RowsAffected == 0 { + return attendie, errors.New("attendie not found") + } + return attendie, nil +} + +func GetAttendieByName(name string) (entities.Db_Attendies, error) { + var attendie entities.Db_Attendies + result := config.Database.First(&attendie, "name = ?", name) + if result.RowsAffected == 0 { + return attendie, errors.New("attendie not found") + } + return attendie, nil +} + +func GetAllAttendies() ([]entities.Db_Attendies, error) { + var attendies []entities.Db_Attendies + result := config.Database.Find(&attendies) + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return attendies, errors.New("attendies not found") + } + return attendies, nil +} + +func GetAttendiesByEvent(event entities.Db_Event) ([]entities.Db_Attendies, error) { + var attendies []entities.Db_Attendies + result := config.Database.Model(&entities.User{}).Preload("Event").Preload("Role").Where("event_id = ?", event.ID).Find(&attendies) + if result.Error != nil { + return attendies, result.Error + } + return attendies, nil +} + +func GetAttendiesByEventPrivacy(event entities.Db_Event, privacy bool) ([]entities.Db_Attendies, error) { + var attendies []entities.Db_Attendies + result := config.Database.Model(&entities.Db_Attendies{}).Preload("Event").Preload("Role").Where("event_id = ?", event.ID).Where("privacy = ?", privacy).Find(&attendies) + if result.Error != nil { + return attendies, result.Error + } + return attendies, nil +} + +func CreateNewAttendie(attendie entities.Db_Attendies) error { + result := config.Database.Create(&attendie) + if result.Error != nil { + return result.Error + } + return nil +} + +func UpdateAttendie(attendie entities.Db_Attendies) error { + result := config.Database.Save(attendie) + if result.Error != nil { + return result.Error + } + return nil +} + +func DeleteAttendie(id int) error { + result := config.Database.Delete(&entities.Db_Attendies{}, id) + if result.Error != nil { + return result.Error + } + return nil +} + +func DeleteAttendiesByEvent(event entities.Db_Event) error { + result := config.Database.Unscoped().Where("event_id = ?", event.ID).Delete(entities.Db_Attendies{}) + if result.Error != nil { + return result.Error + } + return nil +} diff --git a/src/app/util/pretix.go b/src/app/util/pretix.go index 9642b9c..cfef88c 100644 --- a/src/app/util/pretix.go +++ b/src/app/util/pretix.go @@ -79,11 +79,25 @@ func GetAttendieListFromPretixJson(pretixEvent entities.Pretix_Event, event enti func GetAttendiesFromPretixJson(pretixEvent *entities.Pretix_Event, event *entities.Db_Event) ([]entities.Db_Attendies, error) { var attendies []entities.Db_Attendies for _, order := range pretixEvent.Results { + if order.Status == "c" || order.Status == "e" { + continue + } var attendie entities.Db_Attendies attendie.Event = *event + if event.ItemIdParticipation == nil { + return attendies, errors.New("no participation found") + } indexParticipation := slices.IndexFunc(order.Positions, func(p entities.Pretix_Position) bool { return p.Item == *event.ItemIdParticipation }) - indexBadge := slices.IndexFunc(order.Positions, func(p entities.Pretix_Position) bool { return p.Item == *event.ItemIdBadge }) - indexRestaurant := slices.IndexFunc(order.Positions, func(p entities.Pretix_Position) bool { return p.Item == *event.ItemIdRestaurant }) + if event.ItemIdRestaurant == nil { + attendie.AttendsRestaurant = false + } else { + indexRestaurant := slices.IndexFunc(order.Positions, func(p entities.Pretix_Position) bool { return p.Item == *event.ItemIdRestaurant }) + if indexRestaurant != -1 { + attendie.AttendsRestaurant = true + } else { + attendie.AttendsRestaurant = false + } + } indexName := slices.IndexFunc(order.Positions[indexParticipation].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == *event.QuestionIdName }) attendie.Nickname = order.Positions[indexParticipation].Answers[indexName].Answer diff --git a/src/entities/db_attendies.go b/src/entities/db_attendies.go index bdb4ba7..c85cd1e 100644 --- a/src/entities/db_attendies.go +++ b/src/entities/db_attendies.go @@ -6,8 +6,11 @@ import ( type Db_Attendies struct { gorm.Model - Nickname string `gorm:"column:nickname;not null"` - Event Db_Event `gorm:"column:event_id;not null"` - Role Role `gorm:"column:role_id;not_null"` - Privacy bool `gorm:"column:is_public;not null"` + Nickname string `gorm:"column:nickname;not null"` + EventID int `gorm:"not null"` + Event Db_Event + RoleID int `gorm:"not null"` + Role Role + AttendsRestaurant bool `gorm:"column:attends_restaurant;not null"` + Privacy bool `gorm:"column:privacy;not null"` } diff --git a/src/entities/db_event.go b/src/entities/db_event.go index 87e5bc3..42d47f8 100644 --- a/src/entities/db_event.go +++ b/src/entities/db_event.go @@ -1,6 +1,8 @@ package entities -import "gorm.io/gorm" +import ( + "gorm.io/gorm" +) type Db_Event struct { gorm.Model @@ -18,6 +20,7 @@ type Db_Event struct { OptionIdSpotter *string `gorm:"column:option_id_spotter"` OptionIdPhotograph *string `gorm:"column:option_id_photograph"` OptionIdSpecialAnimal *string `gorm:"column:option_id_special_animal"` + EventType *string `gorm:"cloumn:event_type;not null" validate:"required"` } type Db_Event_Public struct {