diff --git a/vingo/database/settings.go b/vingo/database/settings.go index 06b7f9c..6c37a98 100644 --- a/vingo/database/settings.go +++ b/vingo/database/settings.go @@ -23,9 +23,9 @@ func CreateSettings(user_id int) error { } func GetSettings(user_id int) (*Settings, error) { - row := db.QueryRow("SELECT scan_in_out FROM scans WHERE user_id = $1;", user_id) + row := db.QueryRow("SELECT scan_in_out, leaderboard, public FROM settings WHERE user_id = $1;", user_id) settings := new(Settings) - err := row.Scan(&settings.ScanInOut) + err := row.Scan(&settings.ScanInOut, &settings.Leaderboard, &settings.Public) return settings, err } diff --git a/vingo/handlers/cards.go b/vingo/handlers/cards.go index d07ceb0..2dd06a3 100644 --- a/vingo/handlers/cards.go +++ b/vingo/handlers/cards.go @@ -2,6 +2,7 @@ package handlers import ( "time" + "vingo/database" "github.com/gofiber/fiber/v2" ) @@ -22,3 +23,14 @@ func StartCardRegister(c *fiber.Ctx) error { return c.Status(200).Redirect("/cards") } + +func Cards(c *fiber.Ctx) error { + user := getUserFromStore(c) + cards, err := database.GetCardsForUser(user.Id) + if err != nil { + logger.Println("", err) + return c.Status(500).SendString("Error getting cards") + } + + return c.JSON(cards) +} diff --git a/vingo/handlers/leaderboard.go b/vingo/handlers/leaderboard.go new file mode 100644 index 0000000..f99f672 --- /dev/null +++ b/vingo/handlers/leaderboard.go @@ -0,0 +1,17 @@ +package handlers + +import ( + "vingo/database" + + "github.com/gofiber/fiber/v2" +) + +func Leaderboard(c *fiber.Ctx) error { + users, err := database.TotalDaysPerUser() + if err != nil { + logger.Println("Error getting leaderboard:", err) + return c.Status(500).SendString("Error getting leaderboard") + } + + return c.JSON(users) +} diff --git a/vingo/handlers/pages.go b/vingo/handlers/pages.go index 35346f6..56067f2 100644 --- a/vingo/handlers/pages.go +++ b/vingo/handlers/pages.go @@ -30,7 +30,7 @@ func stats(c *fiber.Ctx, user *database.User) error { return c.Render("stats", fiber.Map{"user": user, "days_present_7": days}, "main") } -func Scans(c *fiber.Ctx) error { +func ScansPage(c *fiber.Ctx) error { current_user := getUserFromStore(c) scans, err := database.GetScansForUser(current_user.Id) @@ -42,7 +42,7 @@ func Scans(c *fiber.Ctx) error { return c.Render("scans", fiber.Map{"user": current_user, "scans": scans}, "main") } -func Cards(c *fiber.Ctx) error { +func CardsPage(c *fiber.Ctx) error { current_user := getUserFromStore(c) cards, err := database.GetCardsForUser(current_user.Id) @@ -57,7 +57,7 @@ func Cards(c *fiber.Ctx) error { return c.Render("cards", fiber.Map{"user": current_user, "cards": cards, "registering": registering, "reg_user": registering_is_user}, "main") } -func Days(c *fiber.Ctx) error { +func DaysPage(c *fiber.Ctx) error { current_user := getUserFromStore(c) days, err := database.GetDays() @@ -69,7 +69,7 @@ func Days(c *fiber.Ctx) error { return c.Render("days", fiber.Map{"user": current_user, "days": days}, "main") } -func Leaderboard(c *fiber.Ctx) error { +func LeaderboardPage(c *fiber.Ctx) error { current_user := getUserFromStore(c) leaderboard, err := database.TotalDaysPerUser() @@ -81,7 +81,7 @@ func Leaderboard(c *fiber.Ctx) error { return c.Render("leaderboard", fiber.Map{"user": current_user, "leaderboard": leaderboard}, "main") } -func Settings(c *fiber.Ctx) error { +func SettingsPage(c *fiber.Ctx) error { current_user := getUserFromStore(c) return c.Render("settings", fiber.Map{"user": current_user}, "main") diff --git a/vingo/handlers/scans.go b/vingo/handlers/scans.go index aeb33ef..670b387 100644 --- a/vingo/handlers/scans.go +++ b/vingo/handlers/scans.go @@ -62,3 +62,14 @@ func ScanRegister(c *fiber.Ctx) error { return c.SendString(user.Username) } + +func Scans(c *fiber.Ctx) error { + user := getUserFromStore(c) + scans, err := database.GetScansForUser(user.Id) + if err != nil { + logger.Println("Error get scans:", err) + return c.Status(500).SendString("Error getting scans") + } + + return c.JSON(scans) +} diff --git a/vingo/handlers/settings.go b/vingo/handlers/settings.go index ebd0e42..99f26a6 100644 --- a/vingo/handlers/settings.go +++ b/vingo/handlers/settings.go @@ -31,3 +31,14 @@ func SettingsUpdate(c *fiber.Ctx) error { return c.Redirect("/settings") } + +func Settings(c *fiber.Ctx) error { + user := getUserFromStore(c) + settings, err := database.GetSettings(user.Id) + if err != nil { + logger.Println(err) + return c.Status(500).SendString("Error getting settings") + } + + return c.JSON(settings) +} diff --git a/vingo/handlers/users.go b/vingo/handlers/users.go index 5ac8282..0190a08 100644 --- a/vingo/handlers/users.go +++ b/vingo/handlers/users.go @@ -1 +1,9 @@ package handlers + +import "github.com/gofiber/fiber/v2" + +func User(c *fiber.Ctx) error { + user := getUserFromStore(c) + + return c.JSON(user) +} diff --git a/vingo/main.go b/vingo/main.go index 8e080cf..9308a04 100644 --- a/vingo/main.go +++ b/vingo/main.go @@ -41,21 +41,30 @@ func main() { { logged.Get("/logout", handlers.Logout) - logged.Get("/scans", handlers.Scans) + logged.Get("/scans", handlers.ScansPage) - logged.Get("/cards", handlers.Cards) + logged.Get("/cards", handlers.CardsPage) logged.Post("/cards/register", handlers.StartCardRegister) - logged.Get("/leaderboard", handlers.Leaderboard) + logged.Get("/leaderboard", handlers.LeaderboardPage) logged.Get("/settings", handlers.Settings) logged.Post("/settings", handlers.SettingsUpdate) } + api := logged.Group("/api", handlers.IsLoggedIn) + { + api.Get("/user", handlers.User) + api.Get("/leaderboard", handlers.Leaderboard) + api.Get("/scans", handlers.Scans) + api.Get("/cards", handlers.Cards) + api.Get("/settings", handlers.Settings) + } + // Admin routes admin := logged.Group("/", handlers.IsAdmin) { - admin.Get("/days", handlers.Days) + admin.Get("/days", handlers.DaysPage) admin.Post("/days", handlers.DaysRegister) admin.Post("/days/:id", handlers.DaysDelete) }