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

View File

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

View File

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

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

View File

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

View File

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