feat(display-attendies): add seeder

This commit is contained in:
nikurasu 2024-02-22 18:57:50 +01:00
parent 459ccba0e8
commit 5b247d39a3
Signed by: Nikurasu
GPG key ID: 9E7F14C03EF1F271
11 changed files with 131 additions and 43 deletions

View file

@ -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")
}

View file

@ -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,

View file

@ -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,

View file

@ -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)
}

View file

@ -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")
}

View file

@ -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
}
}*/

View file

@ -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)
}
}

View file

@ -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 {

8
src/entities/seed.go Normal file
View file

@ -0,0 +1,8 @@
package entities
import "gorm.io/gorm"
type Seed struct {
Name string
Run func(*gorm.DB) error
}

10
src/seeder/role.go Normal file
View file

@ -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
}

41
src/seeder/seeds.go Normal file
View file

@ -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")
},
},
}
}