feat(display-attendies): finish getting public attendies as JSON

This commit is contained in:
nikurasu 2024-02-24 11:40:11 +01:00
parent 411d51122a
commit cdd5e74693
Signed by: Nikurasu
GPG key ID: 9E7F14C03EF1F271
7 changed files with 132 additions and 41 deletions

View file

@ -1,45 +1,21 @@
package controller package controller
import ( import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"ulmer-furs.de/pretix-proxy/v2/app/service" "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 // @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") name := c.Params("name")
event, err := service.Get_db_event_by_event(name) event, err := service.Get_db_event_by_event(name)
if err != nil { 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 { 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)) c.JSON(attendies)
resp, err := config.Client.Do(req) return c.SendStatus(fiber.StatusOK)
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)
} }

View file

@ -21,11 +21,20 @@ func PostPretix(c *fiber.Ctx) error {
return c.Status(500).SendString("Internal Server Error") return c.Status(500).SendString("Internal Server Error")
} }
event, err := service.Get_db_event_by_event(data.Event) event, err := service.Get_db_event_by_event(data.Event)
_ = event
if err != nil { if err != nil {
return c.Status(fiber.StatusNotFound).SendString("Event not found") return c.Status(fiber.StatusNotFound).SendString("Event not found")
} }
//fmt.Println(fmt.Printf("%+v\n", event)) attendies, err := util.GetAttendiesFromPretixJson(pretixOrders, &event)
fmt.Println(fmt.Printf("%+v\n", pretixOrders)) 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) return c.SendStatus(fiber.StatusOK)
} }

View file

@ -13,5 +13,5 @@ func PublicRoutes(app *fiber.App) {
apiv1.Post("/login", controller.LoginUser) apiv1.Post("/login", controller.LoginUser)
apiv1.Get("/event", controller.ReturnAllEventsPublic) apiv1.Get("/event", controller.ReturnAllEventsPublic)
apiv1.Get("/ping", controller.Ping) apiv1.Get("/ping", controller.Ping)
attendies.Get("/:name", controller.GetAttendiesByEvent) attendies.Get("/:name", controller.GetAttendiesByEventPublic)
} }

View file

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

View file

@ -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) { func GetAttendiesFromPretixJson(pretixEvent *entities.Pretix_Event, event *entities.Db_Event) ([]entities.Db_Attendies, error) {
var attendies []entities.Db_Attendies var attendies []entities.Db_Attendies
for _, order := range pretixEvent.Results { for _, order := range pretixEvent.Results {
if order.Status == "c" || order.Status == "e" {
continue
}
var attendie entities.Db_Attendies var attendie entities.Db_Attendies
attendie.Event = *event 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 }) 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 }) if event.ItemIdRestaurant == nil {
attendie.AttendsRestaurant = false
} else {
indexRestaurant := slices.IndexFunc(order.Positions, func(p entities.Pretix_Position) bool { return p.Item == *event.ItemIdRestaurant }) 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 }) 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 attendie.Nickname = order.Positions[indexParticipation].Answers[indexName].Answer

View file

@ -7,7 +7,10 @@ import (
type Db_Attendies struct { type Db_Attendies struct {
gorm.Model gorm.Model
Nickname string `gorm:"column:nickname;not null"` Nickname string `gorm:"column:nickname;not null"`
Event Db_Event `gorm:"column:event_id;not null"` EventID int `gorm:"not null"`
Role Role `gorm:"column:role_id;not_null"` Event Db_Event
Privacy bool `gorm:"column:is_public;not null"` RoleID int `gorm:"not null"`
Role Role
AttendsRestaurant bool `gorm:"column:attends_restaurant;not null"`
Privacy bool `gorm:"column:privacy;not null"`
} }

View file

@ -1,6 +1,8 @@
package entities package entities
import "gorm.io/gorm" import (
"gorm.io/gorm"
)
type Db_Event struct { type Db_Event struct {
gorm.Model gorm.Model
@ -18,6 +20,7 @@ type Db_Event struct {
OptionIdSpotter *string `gorm:"column:option_id_spotter"` OptionIdSpotter *string `gorm:"column:option_id_spotter"`
OptionIdPhotograph *string `gorm:"column:option_id_photograph"` OptionIdPhotograph *string `gorm:"column:option_id_photograph"`
OptionIdSpecialAnimal *string `gorm:"column:option_id_special_animal"` OptionIdSpecialAnimal *string `gorm:"column:option_id_special_animal"`
EventType *string `gorm:"cloumn:event_type;not null" validate:"required"`
} }
type Db_Event_Public struct { type Db_Event_Public struct {