diff --git a/src/app/controller/attendies_controller.go b/src/app/controller/attendies_controller.go index b82ecb4..38e1fa2 100644 --- a/src/app/controller/attendies_controller.go +++ b/src/app/controller/attendies_controller.go @@ -16,11 +16,11 @@ import ( // @Description Get the attendies of an event by the name func GetAttendiesByEvent(c *fiber.Ctx) error { name := c.Params("name") - event, err := service.Get_db_event_by_name(name) + event, err := service.Get_db_event_by_event(name) if err != nil { return c.Status(404).SendString(err.Error()) } - req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/api/v1/organizers/%s/events/%s/orders", config.Env.Domain, event.Organizer, event.Event), nil) + req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/api/v1/organizers/%s/events/%s/orders", config.Env.Domain, *event.Organizer, *event.Event), nil) if err != nil { return c.Status(500).SendString("Internal Server Error") } diff --git a/src/app/controller/events_controller.go b/src/app/controller/events_controller.go index bcd4a98..f368b80 100644 --- a/src/app/controller/events_controller.go +++ b/src/app/controller/events_controller.go @@ -84,7 +84,6 @@ func CreateEvent(c *fiber.Ctx) error { if err := c.BodyParser(event); err != nil { return c.Status(fiber.ErrBadRequest.Code).SendString("Bad Request") } - event.EnforceRequired = true if err, errMsg := util.Validate(event); err { return &fiber.Error{ @@ -105,12 +104,6 @@ func UpdateEventById(c *fiber.Ctx) error { if err := c.BodyParser(event); err != nil || parseErr != nil { return c.Status(400).SendString("Bad Request") } - if err, errMsg := util.Validate(event); err { - return &fiber.Error{ - Code: fiber.ErrBadRequest.Code, - Message: errMsg, - } - } if _, err := service.Get_db_event_by_id(id_int); err != nil { return &fiber.Error{ Code: fiber.ErrNotFound.Code, diff --git a/src/app/controller/role_controller.go b/src/app/controller/role_controller.go index ad943f5..04bfb2e 100644 --- a/src/app/controller/role_controller.go +++ b/src/app/controller/role_controller.go @@ -47,12 +47,6 @@ func UpdateRoleById(c *fiber.Ctx) error { if err := c.BodyParser(role); err != nil || parseErr != nil { return c.Status(400).SendString("Bad Request") } - if err, errMsg := util.Validate(role); err { - return &fiber.Error{ - Code: fiber.ErrBadRequest.Code, - Message: errMsg, - } - } if _, err := service.GetRoleById(id_int); err != nil { return &fiber.Error{ Code: fiber.ErrNotFound.Code, diff --git a/src/app/controller/webhook_controller.go b/src/app/controller/webhook_controller.go index c575b74..f9027ff 100644 --- a/src/app/controller/webhook_controller.go +++ b/src/app/controller/webhook_controller.go @@ -4,6 +4,7 @@ import ( "fmt" "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/entities" ) @@ -15,9 +16,16 @@ func PostPretix(c *fiber.Ctx) error { } //TODO: Update attendies from pretix pretixOrders, err := util.GetPretixOrders(&data.Organizer, &data.Event) + _ = pretixOrders if err != nil { return c.Status(500).SendString("Internal Server Error") } - fmt.Println(pretixOrders.Results[len(pretixOrders.Results)-1].Status) + 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)) return c.SendStatus(fiber.StatusOK) } diff --git a/src/app/service/db_event.go b/src/app/service/db_event.go index ec014ed..0ab51d0 100644 --- a/src/app/service/db_event.go +++ b/src/app/service/db_event.go @@ -17,9 +17,9 @@ func Get_db_event_by_id(id int) (entities.Db_Event, error) { return db_event, nil } -func Get_db_event_by_name(name string) (entities.Db_Event, error) { +func Get_db_event_by_event(name string) (entities.Db_Event, error) { var db_event entities.Db_Event - result := config.Database.First(&db_event, "name = ?", name) + result := config.Database.First(&db_event, "event = ?", name) if result.RowsAffected == 0 { return db_event, errors.New("event not found") } diff --git a/src/app/util/pretix.go b/src/app/util/pretix.go index 7a23a0f..db728fa 100644 --- a/src/app/util/pretix.go +++ b/src/app/util/pretix.go @@ -22,7 +22,6 @@ func GetPretixOrders(organizer *string, event *string) (*entities.Pretix_Event, if err != nil { return nil, errors.New("request error") } - fmt.Println(resp.StatusCode) respbody, err := io.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { @@ -40,29 +39,29 @@ func GetAttendieListFromPretixJson(pretixEvent entities.Pretix_Event, event enti var name string for _, order := range pretixEvent.Results { - 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 }) + 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 }) // Get the Name of the Attendie. We take the Name from the Badge, if none Badge ordered == anonymous if indexBadge != -1 { - indexName := slices.IndexFunc(order.Positions[indexBadge].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == event.QuestionIdName }) + indexName := slices.IndexFunc(order.Positions[indexBadge].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == *event.QuestionIdName }) name = order.Positions[indexBadge].Answers[indexName].Answer } else { name = "Anonymous" } - indexRole := slices.IndexFunc(order.Positions[indexParticipation].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == event.QuestionIdRole }) + indexRole := slices.IndexFunc(order.Positions[indexParticipation].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == *event.QuestionIdRole }) role := order.Positions[indexParticipation].Answers[indexRole].OptionIdentifiers - if slices.Contains(role, event.OptionIdSuiter) { + if slices.Contains(role, *event.OptionIdSuiter) { attendies.Suiter = append(attendies.Suiter, name) - } else if slices.Contains(role, event.OptionIdSpotter) { + } else if slices.Contains(role, *event.OptionIdSpotter) { attendies.Spotter = append(attendies.Spotter, name) - } else if slices.Contains(role, event.OptionIdPhotograph) { + } else if slices.Contains(role, *event.OptionIdPhotograph) { attendies.Photographers = append(attendies.Photographers, name) - } else if slices.Contains(role, event.OptionIdGuest) { + } else if slices.Contains(role, *event.OptionIdGuest) { attendies.Guests = append(attendies.Guests, name) - } else if slices.Contains(role, event.OptionIdSpecialAnimal) { + } else if slices.Contains(role, *event.OptionIdSpecialAnimal) { attendies.Suiter = append(attendies.Suiter, name) attendies.Photographers = append(attendies.Photographers, name) } @@ -74,3 +73,15 @@ func GetAttendieListFromPretixJson(pretixEvent entities.Pretix_Event, event enti return attendies } + +/*func GetAttendiesFromPretixJson(pretixEvent *entities.Pretix_Event, event *entities.Db_Event) []entities.Db_Attendies { + var attendies []entities.Db_Attendies + for _, order := range pretixEvent.Results { + 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 }) + + indexName := slices.IndexFunc(order.Positions[indexBadge].Answers, func(a entities.Pretix_Answer) bool { return a.QuestionsIdentififer == *event.QuestionIdName }) + name := order.Positions[indexBadge].Answers[indexName].Answer + } +}*/ diff --git a/src/config/database.go b/src/config/database.go index e2c7c5a..f9b2e6f 100644 --- a/src/config/database.go +++ b/src/config/database.go @@ -1,9 +1,13 @@ package config import ( + "fmt" + "log" + "gorm.io/driver/sqlite" "gorm.io/gorm" "ulmer-furs.de/pretix-proxy/v2/entities" + "ulmer-furs.de/pretix-proxy/v2/seeder" ) var Database *gorm.DB @@ -23,5 +27,24 @@ func Connect() error { &entities.Role{}, ) + if isEmpty(&entities.Role{}) { + for _, seed := range seeder.Roles() { + runSeeder(seed) + } + } + return nil } + +func isEmpty(entity interface{}) bool { + var count int64 + Database.Find(entity).Count(&count) + return count == 0 +} + +func runSeeder(seed entities.Seed) { + fmt.Printf("Run Migration: %s\n", seed.Name) + if err := seed.Run(Database); err != nil { + log.Fatalf("Running seed '%s', failed with error: %s", seed.Name, err) + } +} diff --git a/src/entities/db_event.go b/src/entities/db_event.go index d386ab6..7e30582 100644 --- a/src/entities/db_event.go +++ b/src/entities/db_event.go @@ -4,20 +4,20 @@ import "gorm.io/gorm" type Db_Event struct { gorm.Model - Name string `gorm:"column:name;unique;not null" validate:"required_if=EnforceRequired true"` - Organizer string `gorm:"column:organizer;not null" validate:"required_if=EnforceRequired true"` - Event string `gorm:"column:event;not null" validate:"required_if=EnforceRequired true"` - ItemIdBadge int `gorm:"column:item_id_badge;not null" validate:"required_if=EnforceRequired true"` - ItemIdRestaurant int `gorm:"column:item_id_restaurant;not null" validate:"required_if=EnforceRequired true"` - ItemIdParticipation int `gorm:"column:item_id_participation;not null" validate:"required_if=EnforceRequired true"` - QuestionIdRole string `gorm:"column:question_id_role;not null" validate:"required_if=EnforceRequired true"` - QuestionIdName string `gorm:"column:question_id_name;not null" validate:"required_if=EnforceRequired true"` - OptionIdSuiter string `gorm:"column:option_id_suiter;not null" validate:"required_if=EnforceRequired true"` - OptionIdGuest string `gorm:"column:option_id_guest;not null" validate:"required_if=EnforceRequired true"` - OptionIdSpotter string `gorm:"column:option_id_spotter;not null" validate:"required_if=EnforceRequired true"` - OptionIdPhotograph string `gorm:"column:option_id_photograph;not null" validate:"required_if=EnforceRequired true"` - OptionIdSpecialAnimal string `gorm:"column:option_id_special_animal;not null" validate:"required_if=EnforceRequired true"` - EnforceRequired bool `gorm:"-"` + Name *string `gorm:"column:name;unique;not null" validate:"required"` + Organizer *string `gorm:"column:organizer;not null" validate:"required"` + Event *string `gorm:"column:event;not null" validate:"required"` + ItemIdBadge *int `gorm:"column:item_id_badge"` + ItemIdRestaurant *int `gorm:"column:item_id_restaurant"` + ItemIdParticipation *int `gorm:"column:item_id_participation"` + QuestionIdRole *string `gorm:"column:question_id_role"` + QuestionIdName *string `gorm:"column:question_id_name"` + QuestionIdIsPrivate *string `gorm:"column:question_id_is_private"` + OptionIdSuiter *string `gorm:"column:option_id_suiter"` + OptionIdGuest *string `gorm:"column:option_id_guest"` + OptionIdSpotter *string `gorm:"column:option_id_spotter"` + OptionIdPhotograph *string `gorm:"column:option_id_photograph"` + OptionIdSpecialAnimal *string `gorm:"column:option_id_special_animal"` } type Db_Event_Public struct { diff --git a/src/entities/seed.go b/src/entities/seed.go new file mode 100644 index 0000000..bc03296 --- /dev/null +++ b/src/entities/seed.go @@ -0,0 +1,8 @@ +package entities + +import "gorm.io/gorm" + +type Seed struct { + Name string + Run func(*gorm.DB) error +} diff --git a/src/seeder/role.go b/src/seeder/role.go new file mode 100644 index 0000000..7f46249 --- /dev/null +++ b/src/seeder/role.go @@ -0,0 +1,10 @@ +package seeder + +import ( + "gorm.io/gorm" + "ulmer-furs.de/pretix-proxy/v2/entities" +) + +func CreateRole(db *gorm.DB, name string) error { + return db.Create(&entities.Role{Name: name}).Error +} diff --git a/src/seeder/seeds.go b/src/seeder/seeds.go new file mode 100644 index 0000000..cd0c75e --- /dev/null +++ b/src/seeder/seeds.go @@ -0,0 +1,41 @@ +package seeder + +import ( + "gorm.io/gorm" + "ulmer-furs.de/pretix-proxy/v2/entities" +) + +func Roles() []entities.Seed { + return []entities.Seed{ + { + Name: "CreateGuestRole", + Run: func(db *gorm.DB) error { + return CreateRole(db, "Guest") + }, + }, + { + Name: "CreatePhotographRole", + Run: func(db *gorm.DB) error { + return CreateRole(db, "Photograph") + }, + }, + { + Name: "CreateSpotterRole", + Run: func(db *gorm.DB) error { + return CreateRole(db, "Spotter") + }, + }, + { + Name: "CreateSuiterRole", + Run: func(db *gorm.DB) error { + return CreateRole(db, "Suiter") + }, + }, + { + Name: "CreateSpecialAnimalRole", + Run: func(db *gorm.DB) error { + return CreateRole(db, "SpecialAnimal") + }, + }, + } +}