Skip to content

Commit

Permalink
vingo: add api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes-dev committed Jun 16, 2024
1 parent d1a9035 commit 46e1f22
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 11 deletions.
4 changes: 2 additions & 2 deletions vingo/database/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
12 changes: 12 additions & 0 deletions vingo/handlers/cards.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"time"
"vingo/database"

"github.com/gofiber/fiber/v2"
)
Expand All @@ -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)
}
17 changes: 17 additions & 0 deletions vingo/handlers/leaderboard.go
Original file line number Diff line number Diff line change
@@ -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)
}
10 changes: 5 additions & 5 deletions vingo/handlers/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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")
Expand Down
11 changes: 11 additions & 0 deletions vingo/handlers/scans.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 11 additions & 0 deletions vingo/handlers/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 8 additions & 0 deletions vingo/handlers/users.go
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
package handlers

import "github.com/gofiber/fiber/v2"

func User(c *fiber.Ctx) error {
user := getUserFromStore(c)

return c.JSON(user)
}
17 changes: 13 additions & 4 deletions vingo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 46e1f22

Please sign in to comment.