Skip to content

Commit

Permalink
vingo: seasons
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes-dev committed Jul 18, 2024
1 parent 61aa8fc commit ab01f84
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vingo/database/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ 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"`
}
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
}
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)
}

0 comments on commit ab01f84

Please sign in to comment.