diff --git a/vingo/database/models.go b/vingo/database/models.go index ce66161..ff72350 100644 --- a/vingo/database/models.go +++ b/vingo/database/models.go @@ -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"` +} diff --git a/vingo/database/seasons.go b/vingo/database/seasons.go new file mode 100644 index 0000000..4cd36ab --- /dev/null +++ b/vingo/database/seasons.go @@ -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 +} diff --git a/vingo/handlers/seasons.go b/vingo/handlers/seasons.go new file mode 100644 index 0000000..bcfed09 --- /dev/null +++ b/vingo/handlers/seasons.go @@ -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) +}