Skip to content

Commit

Permalink
vinvoor: scans
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Jul 18, 2024
2 parents 656a329 + 61aa8fc commit f70fb2f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 45 deletions.
22 changes: 2 additions & 20 deletions vingo/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion vingo/database/users.go
Original file line number Diff line number Diff line change
@@ -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}}
Expand Down
10 changes: 6 additions & 4 deletions vingo/handlers/cards.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand Down
25 changes: 10 additions & 15 deletions vingo/handlers/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
13 changes: 8 additions & 5 deletions vingo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit f70fb2f

Please sign in to comment.