Skip to content

Commit

Permalink
vingo: check if logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes-dev committed Aug 13, 2024
1 parent f48a553 commit 815015d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
7 changes: 5 additions & 2 deletions vingo/database/cards.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ func GetCardsForUser(user_id int) ([]Card, error) {

func GetCardsAndStatsForUser(user_id int) ([]CardAPI, error) {
rows, err := db.Query(`
SELECT cards.id, cards.created_at, serial, name, COUNT(scans.id), (select MAX(scan_time) from scans where card_serial = cards.serial) from cards LEFT JOIN scans on scans.card_serial = serial WHERE
user_id = $1 GROUP BY cards.id;
SELECT cards.id, cards.created_at, serial, name, COUNT(scans.id), (select MAX(scan_time) from scans where card_serial = cards.serial)
FROM cards
LEFT JOIN scans on scans.card_serial = serial
WHERE user_id = $1
GROUP BY cards.id, cards.created_at, cards.serial, cards.name;
`, user_id)

if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions vingo/database/scans.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func CreateScan(card_serial string) error {
func GetScansForUser(user_id int) ([]Scan, error) {
var user User
result := gorm_db.Preload("Cards.Scans").First(&user, user_id)
if result.Error != nil {
return nil, result.Error
}

var scans []Scan
for _, card := range user.Cards {
Expand Down
37 changes: 20 additions & 17 deletions vingo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,27 @@ func main() {

api.Get("/recent_scans", handlers.PublicRecentScans)

api.Post("/logout", handlers.Logout)
api.Get("/user", handlers.User)
api.Get("/leaderboard", handlers.Leaderboard)
api.Get("/scans", handlers.Scans)

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)
authed := api.Group("", handlers.IsLoggedIn)
{
admin.Get("/days", handlers.Days{}.All)
admin.Post("/days", handlers.Days{}.CreateMultiple)
admin.Delete("/days/:id", handlers.Days{}.Delete)
authed.Post("/logout", handlers.Logout)
authed.Get("/user", handlers.User)
authed.Get("/leaderboard", handlers.Leaderboard)
authed.Get("/scans", handlers.Scans)

authed.Get("/cards", handlers.Cards{}.Get)
authed.Patch("/cards/:id", handlers.Cards{}.Update)
authed.Get("/cards/register", handlers.Cards{}.RegisterStatus)
authed.Post("/cards/register", handlers.Cards{}.StartRegister)

authed.Get("/settings", handlers.Settings{}.Get)
authed.Patch("/settings", handlers.Settings{}.Update)

admin := authed.Group("/admin", handlers.IsAdmin)
{
admin.Get("/days", handlers.Days{}.All)
admin.Post("/days", handlers.Days{}.CreateMultiple)
admin.Delete("/days/:id", handlers.Days{}.Delete)
}
}
}

Expand Down

0 comments on commit 815015d

Please sign in to comment.