82 lines
1.4 KiB
Go
82 lines
1.4 KiB
Go
|
package api
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
|
||
|
"github.com/gin-gonic/gin"
|
||
|
|
||
|
"dev.sum7.eu/genofire/golang-lib/database"
|
||
|
|
||
|
"dev.sum7.eu/genofire/meetandeat/models"
|
||
|
)
|
||
|
|
||
|
func getDateAll(c *gin.Context) {
|
||
|
list := []*models.Date{}
|
||
|
database.Read.Find(&list)
|
||
|
|
||
|
c.JSON(http.StatusOK, list)
|
||
|
}
|
||
|
|
||
|
func getDate(c *gin.Context) {
|
||
|
id, err := strconv.Atoi(c.Params.ByName("id"))
|
||
|
if err != nil {
|
||
|
c.JSON(http.StatusBadRequest, err.Error())
|
||
|
return
|
||
|
}
|
||
|
d := models.Date{
|
||
|
ID: id,
|
||
|
}
|
||
|
|
||
|
res := database.Read.Preload("Meets").First(&d)
|
||
|
if res.RecordNotFound() {
|
||
|
c.JSON(http.StatusNotFound, res.Error.Error())
|
||
|
return
|
||
|
}
|
||
|
if res.Error != nil {
|
||
|
c.JSON(http.StatusBadRequest, res.Error.Error())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, d)
|
||
|
}
|
||
|
|
||
|
func addDate(c *gin.Context) {
|
||
|
var v time.Time
|
||
|
if err := c.BindJSON(&v); err != nil {
|
||
|
c.JSON(http.StatusBadRequest, err.Error())
|
||
|
return
|
||
|
}
|
||
|
if v.Before(time.Now()) {
|
||
|
c.JSON(http.StatusBadRequest, "date has to be in the future")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
d := &models.Date{
|
||
|
Date: v,
|
||
|
}
|
||
|
|
||
|
database.Write.Save(d)
|
||
|
|
||
|
c.JSON(http.StatusOK, d)
|
||
|
}
|
||
|
|
||
|
func addDateMeet(c *gin.Context) {
|
||
|
var v models.DateMeet
|
||
|
if err := c.BindJSON(&v); err != nil {
|
||
|
c.JSON(http.StatusBadRequest, err.Error())
|
||
|
return
|
||
|
}
|
||
|
id, err := strconv.Atoi(c.Params.ByName("id"))
|
||
|
if err != nil {
|
||
|
c.JSON(http.StatusBadRequest, err.Error())
|
||
|
return
|
||
|
}
|
||
|
v.DateID = id
|
||
|
|
||
|
database.Write.Save(&v)
|
||
|
|
||
|
c.JSON(http.StatusOK, v)
|
||
|
}
|