feat(display-attendies): create CRUD routes for role

This commit is contained in:
nikurasu 2024-02-22 15:11:59 +01:00
parent 78968c7199
commit 459ccba0e8
Signed by: Nikurasu
GPG Key ID: 9E7F14C03EF1F271
12 changed files with 215 additions and 13 deletions

View File

@ -39,7 +39,7 @@ func GetAttendiesByEvent(c *fiber.Ctx) error {
return c.Status(500).SendString("Internal Server Error")
}
attendiesAtEvent := util.GetAttendiesFromPretixJson(unmarshaled_resp, event)
attendiesAtEvent := util.GetAttendieListFromPretixJson(unmarshaled_resp, event)
c.JSON(attendiesAtEvent)
return c.SendStatus(200)
}

View File

@ -0,0 +1,94 @@
package controller
import (
"strconv"
"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"
)
func ReturnRoleById(c *fiber.Ctx) error {
id := c.Params("id")
id_int, err := strconv.Atoi(id)
if err != nil {
return c.Status(fiber.StatusBadRequest).SendString("Bad Request")
}
role, err := service.GetRoleById(id_int)
if err != nil {
return c.Status(fiber.StatusNotFound).SendString(err.Error())
}
c.JSON(role)
return c.SendStatus(fiber.StatusOK)
}
func CreateRole(c *fiber.Ctx) error {
role := new(entities.Role)
if err := c.BodyParser(role); err != nil {
return c.Status(fiber.StatusBadRequest).SendString("Bad Request")
}
if err, errMsg := util.Validate(role); err {
return &fiber.Error{
Code: fiber.ErrBadRequest.Code,
Message: errMsg,
}
}
if err := service.CreateNewRole(*role); err != nil {
return c.Status(fiber.ErrInternalServerError.Code).SendString(err.Error())
}
return c.SendStatus(fiber.StatusOK)
}
func UpdateRoleById(c *fiber.Ctx) error {
role := new(entities.Role)
id := c.Params("id")
id_int, parseErr := strconv.Atoi(id)
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,
Message: "Event not found",
}
}
role.ID = uint(id_int)
if err := service.UpdateRole(*role); err != nil {
return c.Status(500).SendString(err.Error())
}
return c.SendStatus(200)
}
func DeleteRoleById(c *fiber.Ctx) error {
id := c.Params("id")
id_int, err := strconv.Atoi(id)
if err != nil {
return c.Status(400).SendString("Bad Request")
}
if _, err := service.GetRoleById(id_int); err != nil {
return &fiber.Error{
Code: fiber.ErrNotFound.Code,
Message: "Role not found",
}
}
if err := service.DeleteRole(id_int); err != nil {
return c.Status(404).SendString(err.Error())
}
return c.SendStatus(200)
}
func ReturnAllRoles(c *fiber.Ctx) error {
roles, err := service.GetAllRoles()
if err != nil {
return c.Status(500).SendString("Internal Server Error")
}
c.JSON(roles)
return c.SendStatus(200)
}

View File

@ -1,7 +1,10 @@
package controller
import (
"fmt"
"github.com/gofiber/fiber/v2"
"ulmer-furs.de/pretix-proxy/v2/app/util"
"ulmer-furs.de/pretix-proxy/v2/entities"
)
@ -11,5 +14,10 @@ func PostPretix(c *fiber.Ctx) error {
return c.Status(fiber.ErrBadRequest.Code).SendString(err.Error())
}
//TODO: Update attendies from pretix
pretixOrders, err := util.GetPretixOrders(&data.Organizer, &data.Event)
if err != nil {
return c.Status(500).SendString("Internal Server Error")
}
fmt.Println(pretixOrders.Results[len(pretixOrders.Results)-1].Status)
return c.SendStatus(fiber.StatusOK)
}

View File

@ -16,6 +16,12 @@ func PrivateRoutes(app *fiber.App) {
event.Delete("/:id", controller.DeleteEventById)
event.Put("/:id", controller.UpdateEventById)
event.Put("", controller.CreateEvent)
role := apiv1.Group("/role")
role.Get("", controller.ReturnAllRoles)
role.Get("/:id", controller.ReturnRoleById)
role.Delete("/:id", controller.DeleteRoleById)
role.Put("/:id", controller.UpdateRoleById)
role.Put("", controller.CreateRole)
user := apiv1.Group("/user")
user.Use(config.AuthMiddleware)
user.Put("", controller.CreateUser)

51
src/app/service/role.go Normal file
View File

@ -0,0 +1,51 @@
package service
import (
"errors"
"gorm.io/gorm"
"ulmer-furs.de/pretix-proxy/v2/config"
"ulmer-furs.de/pretix-proxy/v2/entities"
)
func GetRoleById(id int) (entities.Role, error) {
var role entities.Role
result := config.Database.Find(&role, id)
if result.RowsAffected == 0 {
return role, errors.New("role not found")
}
return role, nil
}
func GetAllRoles() ([]entities.Role, error) {
var roles []entities.Role
result := config.Database.Find(&roles)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return roles, errors.New("roles not found")
}
return roles, nil
}
func CreateNewRole(role entities.Role) error {
result := config.Database.Create(&role)
if result.Error != nil {
return result.Error
}
return nil
}
func UpdateRole(role entities.Role) error {
result := config.Database.Save(role)
if result.Error != nil {
return result.Error
}
return nil
}
func DeleteRole(id int) error {
result := config.Database.Delete(&entities.Role{}, id)
if result.Error != nil {
return result.Error
}
return nil
}

View File

@ -1,11 +1,41 @@
package util
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"golang.org/x/exp/slices"
"ulmer-furs.de/pretix-proxy/v2/config"
"ulmer-furs.de/pretix-proxy/v2/entities"
)
func GetAttendiesFromPretixJson(pretixEvent entities.Pretix_Event, event entities.Db_Event) entities.Attendies {
func GetPretixOrders(organizer *string, event *string) (*entities.Pretix_Event, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/api/v1/organizers/%s/events/%s/orders", config.Env.Domain, *organizer, *event), nil)
if err != nil {
return nil, errors.New("request error")
}
req.Header.Add("Authorization", fmt.Sprintf("Token %s", config.Env.ApiKeyPretix))
resp, err := config.Client.Do(req)
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 {
return nil, errors.New("request error")
}
var unmarshaled_resp entities.Pretix_Event
if err := json.Unmarshal(respbody, &unmarshaled_resp); err != nil {
return nil, errors.New("request error")
}
return &unmarshaled_resp, nil
}
func GetAttendieListFromPretixJson(pretixEvent entities.Pretix_Event, event entities.Db_Event) entities.Attendies {
var attendies entities.Attendies
var name string

View File

@ -16,7 +16,12 @@ func Connect() error {
panic(err)
}
Database.AutoMigrate(&entities.Db_Event{}, &entities.Db_Attendies{}, &entities.User{})
Database.AutoMigrate(
&entities.Db_Event{},
&entities.Db_Attendies{},
&entities.User{},
&entities.Role{},
)
return nil
}

View File

@ -4,6 +4,6 @@ import "net/http"
var Client *http.Client
func Create() {
func SetupHttp() {
Client = &http.Client{}
}

View File

@ -1,15 +1,13 @@
package entities
import "gorm.io/gorm"
import (
"gorm.io/gorm"
)
type Db_Attendies struct {
gorm.Model
Nickname string `gorm:"column:nickname;not null"`
Event Db_Event `gorm:"column:event_id;not null"`
IsSuiter bool `grom:"column:is_suiter;not null"`
IsGuest bool `gorm:"column:is_guest;not null"`
IsSpotter bool `gorm:"column:is_spotter;not null"`
IsPhotograph bool `gorm:"column:is_photograph;not null"`
IsSpecialAnimal bool `gorm:"column:is_special_animal;not null"`
IsPublic bool `gorm:"column:is_public;not null"`
Nickname string `gorm:"column:nickname;not null"`
Event Db_Event `gorm:"column:event_id;not null"`
Role Role `gorm:"column:role_id;not_null"`
IsPublic bool `gorm:"column:is_public;not null"`
}

View File

@ -6,6 +6,7 @@ type Pretix_Event struct {
}
type Pretix_Result struct {
Status string `json:"status"`
Positions []Pretix_Position `json:"positions"`
}

8
src/entities/roles.go Normal file
View File

@ -0,0 +1,8 @@
package entities
import "gorm.io/gorm"
type Role struct {
gorm.Model
Name string `grom:"column:role:not null" validate:"required"`
}

View File

@ -19,6 +19,7 @@ func main() {
config.SetupMiddlewares()
config.SetupFiber(viewFS)
config.SetupCors()
config.SetupHttp()
if config.Env.Debug {
config.App.Use(cors.New(cors.Config{