diff --git a/vingo/database/db.go b/vingo/database/db.go index 2a169dd..157c7ae 100644 --- a/vingo/database/db.go +++ b/vingo/database/db.go @@ -24,31 +24,13 @@ func OpenDatabase(db_string string) { log.Fatal(err) } - err = new_db.AutoMigrate(&Settings{}) + err = new_db.AutoMigrate() if err != nil { log.Println("Error migrating database") log.Fatal(err) } - err = new_db.AutoMigrate(&User{}) - if err != nil { - log.Println("Error migrating database") - log.Fatal(err) - } - - err = new_db.AutoMigrate(&Card{}) - if err != nil { - log.Println("Error migrating database") - log.Fatal(err) - } - - err = new_db.AutoMigrate(&Scan{}) - if err != nil { - log.Println("Error migrating database") - log.Fatal(err) - } - - err = new_db.AutoMigrate(&Day{}) + err = new_db.AutoMigrate(&User{}, &Card{}, &Scan{}, &Day{}, &Settings{}, &Season{}) if err != nil { log.Println("Error migrating database") log.Fatal(err) diff --git a/vingo/database/users.go b/vingo/database/users.go index 441df16..123d93d 100644 --- a/vingo/database/users.go +++ b/vingo/database/users.go @@ -1,6 +1,8 @@ package database -import "time" +import ( + "time" +) func CreateUserIfNew(user_id int, username string) error { var user = &User{Username: username, Settings: Settings{ScanInOut: false, Leaderboard: true, Public: false}} diff --git a/vingo/handlers/cards.go b/vingo/handlers/cards.go index a1dc0e9..accd969 100644 --- a/vingo/handlers/cards.go +++ b/vingo/handlers/cards.go @@ -10,7 +10,9 @@ import ( var register_timeout = time.Minute -func StartCardRegister(c *fiber.Ctx) error { +type Cards struct{} + +func (Cards) StartRegister(c *fiber.Ctx) error { user := getUserFromStore(c) if time.Now().Before(registering_end) { @@ -27,7 +29,7 @@ func StartCardRegister(c *fiber.Ctx) error { return c.Status(200).JSON(map[string]bool{}) } -func Cards(c *fiber.Ctx) error { +func (Cards) Get(c *fiber.Ctx) error { user := getUserFromStore(c) cards, err := database.GetCardsAndStatsForUser(user.Id) if err != nil { @@ -38,7 +40,7 @@ func Cards(c *fiber.Ctx) error { return c.JSON(cards) } -func CardRegisterStatus(c *fiber.Ctx) error { +func (Cards) RegisterStatus(c *fiber.Ctx) error { user := getUserFromStore(c) register_ongoing := time.Now().Before(registering_end) is_current_user := registering_user == user.Id @@ -47,7 +49,7 @@ func CardRegisterStatus(c *fiber.Ctx) error { return c.JSON(map[string]interface{}{"registering": register_ongoing, "isCurrentUser": is_current_user, "success": registering_success, "timeRemaining": time_remaining, "timePercentage": time_percentage}) } -func CardNameUpdate(c *fiber.Ctx) error { +func (Cards) Update(c *fiber.Ctx) error { user := getUserFromStore(c) card_id, err := strconv.Atoi(c.Params("id")) if err != nil { diff --git a/vingo/handlers/settings.go b/vingo/handlers/settings.go index 6ce76ce..dbbeb53 100644 --- a/vingo/handlers/settings.go +++ b/vingo/handlers/settings.go @@ -6,21 +6,16 @@ import ( "github.com/gofiber/fiber/v2" ) -func SettingsUpdate(c *fiber.Ctx) error { - user := getUserFromStore(c) - - scan_in_out := c.FormValue("scan_in_out") - leaderboard := c.FormValue("leaderboard") - public := c.FormValue("public") +type Settings struct{} - if scan_in_out == "" || leaderboard == "" || public == "" { - return c.Status(400).SendString("Missing fields") - } +func (Settings) Update(c *fiber.Ctx) error { + user := getUserFromStore(c) - settings := database.Settings{ - ScanInOut: scan_in_out == "on", - Leaderboard: leaderboard == "on", - Public: public == "on", + settings := database.Settings{} + err := c.BodyParser(&settings) + if err != nil { + logger.Println(err) + return c.Status(400).SendString("Invalid payload") } sess, _ := store.Get(c) @@ -29,10 +24,10 @@ func SettingsUpdate(c *fiber.Ctx) error { sess.Set(STORE_USER, &user) sess.Save() - return c.Redirect("/settings") + return c.SendStatus(200) } -func Settings(c *fiber.Ctx) error { +func (Settings) Get(c *fiber.Ctx) error { user := getUserFromStore(c) return c.JSON(user.Settings) } diff --git a/vingo/main.go b/vingo/main.go index 73e5d8d..ab8e540 100644 --- a/vingo/main.go +++ b/vingo/main.go @@ -45,11 +45,14 @@ func main() { api.Get("/user", handlers.User) api.Get("/leaderboard", handlers.Leaderboard) api.Get("/scans", handlers.Scans) - api.Get("/cards", handlers.Cards) - api.Patch("/cards/:id", handlers.CardNameUpdate) - api.Get("/cards/register", handlers.CardRegisterStatus) - api.Post("/cards/register", handlers.StartCardRegister) - api.Get("/settings", handlers.Settings) + + api.Get("/cards", handlers.Cards{}.Get) + api.Patch("/cards/:id", handlers.Cards{}.Update) + api.Get("/cards/register", handlers.Cards{}.RegisterStatus) + api.Post("/cards/register", handlers.Cards{}.StartRegister) + + api.Get("/settings", handlers.Settings{}.Get) + api.Patch("/settings", handlers.Settings{}.Update) admin := api.Group("/admin", handlers.IsAdmin) {