Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin panel #36

Merged
merged 22 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 22 additions & 36 deletions vingo/database/days.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,43 @@
package database

import "time"
import (
"time"

type Day struct {
Id int
Date time.Time
}
"gorm.io/gorm"
)

func CreateDays(first_day time.Time, last_day time.Time) error {
tx, err := db.Begin()
if err != nil {
return err
}

defer tx.Rollback()

for d := first_day; !d.After(last_day); d = d.AddDate(0, 0, 1) {
// Ignore weekends
if d.Weekday() == time.Saturday || d.Weekday() == time.Sunday {
continue
err := gorm_db.Transaction(func(tx *gorm.DB) error {
for d := first_day; !d.After(last_day); d = d.AddDate(0, 0, 1) {
// Ignore weekends
if d.Weekday() == time.Saturday || d.Weekday() == time.Sunday {
continue
}

if err := tx.Create(&StreakDay{Date: d}).Error; err != nil {
return err
}
}

_, err := db.Exec("INSERT INTO days (date) VALUES ($1);", d)
if err != nil {
return err
}
}
return nil
})

if err = tx.Commit(); err != nil {
if err != nil {
return err
}

return nil
}

func GetDays() ([]Day, error) {
rows, err := db.Query("SELECT id, date FROM days ORDER BY date;")
if err != nil {
return nil, err
}

days := make([]Day, 0)
for rows.Next() {
var day Day
rows.Scan(&day.Id, &day.Date)
days = append(days, day)
}
func GetDays() ([]StreakDay, error) {
var days []StreakDay
err := gorm_db.Find(&days).Error

return days, nil
return days, err
}

func DeleteDay(dayId string) error {
_, err := db.Exec("DELETE FROM days WHERE id = $1;", dayId)
_, err := db.Exec("DELETE FROM streak_days WHERE id = $1;", dayId)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion vingo/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func OpenDatabase(db_string string) {
log.Fatal(err)
}

err = new_db.AutoMigrate(&User{}, &Card{}, &Scan{}, &Day{}, &Settings{}, &Season{})
err = new_db.AutoMigrate(&User{}, &Card{}, &Scan{}, &StreakDay{}, &Settings{}, &Season{})
if err != nil {
log.Println("Error migrating database")
log.Fatal(err)
Expand Down
11 changes: 11 additions & 0 deletions vingo/database/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,14 @@ type Scan struct {
CardSerial string `json:"cardSerial" gorm:"index"`
Card Card `json:"-" gorm:"foreignKey:CardSerial;references:Serial"`
}

type Season struct {
BaseModel
StartDate time.Time `json:"startDate"`
EndDate time.Time `json:"endDate"`
}

type StreakDay struct {
BaseModel
Date time.Time `json:"date"`
}
11 changes: 11 additions & 0 deletions vingo/database/seasons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package database

func (Season) GetAll() ([]Season, error) {
var seasons []Season
result := gorm_db.Find(&seasons)
return seasons, result.Error
}

func (s Season) Create() error {
return gorm_db.Create(s).Error
}
27 changes: 18 additions & 9 deletions vingo/database/settings.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package database

func CreateSettings(user_id int) error {
_, err := db.Exec("INSERT INTO settings (user_id) VALUES ($1) ON CONFLICT DO NOTHING;", user_id)
return err
}
import "log"

func GetSettings(user_id int) (*Settings, error) {
var settings Settings
result := gorm_db.First(&settings, "user_id = ?", user_id)
return &settings, result.Error
var user User
result := gorm_db.Preload("Settings").First(&user, user_id)
log.Println(user)
return &user.Settings, result.Error
}

func UpdateSettings(user_id int, settings Settings) error {
_, err := db.Exec("UPDATE settings SET scan_in_out = $1, leaderboard = $2, public = $3 WHERE user_id = $4;", settings.ScanInOut, settings.Leaderboard, settings.Public, user_id)
return err
var user User
if err := gorm_db.Preload("Settings").First(&user, user_id).Error; err != nil {
return err
}

user.Settings.ScanInOut = settings.ScanInOut
user.Settings.Leaderboard = settings.Leaderboard
user.Settings.Public = settings.Public
if err := gorm_db.Save(&user.Settings).Error; err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion vingo/database/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func CreateUserIfNew(user_id int, username string) error {

func GetUser(user_id int) (*User, error) {
var user User
result := gorm_db.First(&user, user_id)
result := gorm_db.Preload("Settings").First(&user, user_id)
return &user, result.Error
}

Expand Down
4 changes: 2 additions & 2 deletions vingo/handlers/cards.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (Cards) StartRegister(c *fiber.Ctx) error {

logger.Println("Card registration started by user", registering_user)

return c.Status(200).JSON(map[string]bool{})
return c.SendStatus(200)
}

func (Cards) Get(c *fiber.Ctx) error {
Expand Down Expand Up @@ -68,5 +68,5 @@ func (Cards) Update(c *fiber.Ctx) error {
return c.Status(500).SendString("Error updating card name")
}

return c.Status(200).JSON(map[string]bool{})
return c.SendStatus(200)
}
41 changes: 26 additions & 15 deletions vingo/handlers/days.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,46 @@ import (
"github.com/gofiber/fiber/v2"
)

func DaysRegister(c *fiber.Ctx) error {
form_start_date := c.FormValue("start_date")
form_end_date := c.FormValue("end_date")

start_date, start_err := time.Parse("2006-01-02", form_start_date)
end_date, end_err := time.Parse("2006-01-02", form_end_date)
if start_err != nil || end_err != nil {
logger.Println("Error parsing dates:", start_err, end_err)
return c.Status(400).SendString("Error parsing dates")
type Days struct{}

type DaysBody struct {
StartDate time.Time `json:"startDate"`
EndDate time.Time `json:"endDate"`
}

func (Days) CreateMultiple(c *fiber.Ctx) error {
payload := new(DaysBody)
if err := c.BodyParser(payload); err != nil {
logger.Println("Error parsing body:", err)
return c.Status(400).SendString("Error parsing body")
}

logger.Println("Registering days from", start_date, "to", end_date)
err := database.CreateDays(start_date, end_date)
err := database.CreateDays(payload.StartDate, payload.EndDate)
if err != nil {
logger.Println("Error creating days:", err)
return c.Status(500).SendString("Error creating days")
}

return c.Redirect("/days")
return c.SendStatus(200)
}

func DaysDelete(c *fiber.Ctx) error {
func (Days) Delete(c *fiber.Ctx) error {
day_id := c.Params("id")
logger.Println("Deleting day", day_id)
err := database.DeleteDay(day_id)
if err != nil {
logger.Println("Error deleting day:", err)
return c.Status(500).SendString("Error deleting day")
}

return c.Redirect("/days")
return c.SendStatus(200)
}

func (Days) All(c *fiber.Ctx) error {
days, err := database.GetDays()
if err != nil {
logger.Println("Error getting days:", err)
return c.Status(500).SendString("Error getting days")
}

return c.JSON(days)
}
36 changes: 36 additions & 0 deletions vingo/handlers/seasons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package handlers

import (
"vingo/database"

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

type Seasons struct{}

func (Seasons) Get(c *fiber.Ctx) error {
seasons, err := database.Season{}.GetAll()
if err != nil {
logger.Println(err)
return c.Status(500).SendString("Error getting seasons")
}

return c.JSON(seasons)
}

func (Seasons) Create(c *fiber.Ctx) error {
season := database.Season{}
err := c.BodyParser(&season)
if err != nil {
logger.Println(err)
return c.Status(400).SendString("Invalid payload")
}

err = season.Create()
if err != nil {
logger.Println(err)
return c.Status(500).SendString("Error creating season")
}

return c.SendStatus(200)
}
7 changes: 6 additions & 1 deletion vingo/handlers/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ func (Settings) Update(c *fiber.Ctx) error {
return c.Status(400).SendString("Invalid payload")
}

err = database.UpdateSettings(user.Id, settings)
if err != nil {
logger.Println(err)
return c.Status(500).SendString("Error updating settings")
}

sess, _ := store.Get(c)
database.UpdateSettings(user.Id, settings)
user, _ = database.GetUser(user.Id)
sess.Set(STORE_USER, &user)
sess.Save()
Expand Down
5 changes: 3 additions & 2 deletions vingo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ func main() {

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

Expand Down
2 changes: 2 additions & 0 deletions vinvoor/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ dist-ssr
bin/
pkg/
.env

test.js
2 changes: 1 addition & 1 deletion vinvoor/.tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nodejs 22.2.0
nodejs 20.15.1
2 changes: 1 addition & 1 deletion vinvoor/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:22.2.0-alpine3.19 as build-stage
FROM node:20.15.1-alpine3.20 as build-stage

WORKDIR /app

Expand Down
2 changes: 1 addition & 1 deletion vinvoor/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:22.2.0-alpine3.19
FROM node:20.15.1-alpine3.20

WORKDIR /frontend

Expand Down
3 changes: 2 additions & 1 deletion vinvoor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@
"@fontsource/roboto": "^5.0.13",
"@mui/icons-material": "^5.15.19",
"@mui/material": "^5.15.19",
"@mui/x-date-pickers": "^7.11.0",
"@types/js-cookie": "^3.0.6",
"@types/react-router-dom": "^5.3.3",
"@types/react-router-hash-link": "^2.4.9",
"apexcharts": "^3.50.0",
"dayjs": "^1.11.12",
"js-cookie": "^3.0.5",
"material-ui-confirm": "^3.0.16",
"mdi-material-ui": "^7.9.1",
"notistack": "^3.0.1",
"react": "^18.2.0",
"react-apexcharts": "^1.4.1",
"react-device-detect": "^2.2.3",
"react-dom": "^18.2.0",
"react-router-dom": "^6.23.1",
"react-router-hash-link": "^2.4.3",
Expand Down
2 changes: 1 addition & 1 deletion vinvoor/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Navigate, Outlet, useOutlet } from "react-router-dom";
import { LoadingSkeleton } from "./components/LoadingSkeleton";
import { NavBar } from "./navbar/NavBar";
import { Overview } from "./overview/Overview";
import { UserContext } from "./user/UserProvider";
import { UserContext } from "./providers/UserProvider";
import { WelcomePage } from "./WelcomePage";

export const App = () => {
Expand Down
35 changes: 15 additions & 20 deletions vinvoor/src/WelcomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,21 @@ export const WelcomePage = () => {
>
<TypographyG variant="h3">Welcome to Vinvoor!</TypographyG>
<TypographyG variant="h4">Log in to start scanning</TypographyG>
<Login variant="contained">
<Typography>Log in with Zauth</Typography>
<ShakerOutline sx={{ ml: 1 }} />
</Login>
<Button
variant="contained"
color="github"
onClick={handleClick}
sx={{ mt: 3 }}
>
<GitHub sx={{ mr: 1 }} />
<Typography>Github Repository</Typography>
</Button>
<Box mt={2}>
<Login variant="contained" fullWidth>
<Typography>Log in with Zauth</Typography>
<ShakerOutline sx={{ ml: 1 }} />
</Login>
<Button
variant="outlined"
onClick={handleClick}
fullWidth
sx={{ mt: 1 }}
>
<GitHub sx={{ mr: 1 }} />
<Typography>Github Repository</Typography>
</Button>
</Box>
</Box>
);
};

// <UnstyledLink to="/login">
// <Button variant="contained">
// <Typography>Log in with Zauth</Typography>
// <ShakerOutline sx={{ ml: 1 }} />
// </Button>
// </UnstyledLink>
Loading