first try
This commit is contained in:
45
wagfarm-api/routes/asset/asset_routes.go
Normal file
45
wagfarm-api/routes/asset/asset_routes.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package asset
|
||||
|
||||
import (
|
||||
"go-api/database"
|
||||
"go-api/models"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterRoutes(router *gin.Engine) {
|
||||
db := database.DB
|
||||
|
||||
router.GET("/asset-types", func(c *gin.Context) {
|
||||
var types []models.AssetType
|
||||
db.Find(&types)
|
||||
c.JSON(http.StatusOK, types)
|
||||
})
|
||||
|
||||
router.POST("/asset-types", func(c *gin.Context) {
|
||||
var assetType models.AssetType
|
||||
if err := c.ShouldBindJSON(&assetType); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
db.Create(&assetType)
|
||||
c.JSON(http.StatusCreated, assetType)
|
||||
})
|
||||
|
||||
router.GET("/assets", func(c *gin.Context) {
|
||||
var assets []models.Asset
|
||||
db.Preload("AssetType").Find(&assets)
|
||||
c.JSON(http.StatusOK, assets)
|
||||
})
|
||||
|
||||
router.POST("/assets", func(c *gin.Context) {
|
||||
var asset models.Asset
|
||||
if err := c.ShouldBindJSON(&asset); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
db.Create(&asset)
|
||||
c.JSON(http.StatusCreated, asset)
|
||||
})
|
||||
}
|
||||
17
wagfarm-api/routes/log/harvest.go
Normal file
17
wagfarm-api/routes/log/harvest.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"wagfarm-api/database"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterHarvestRoutes(router *gin.Engine, path string) {
|
||||
db := database.DB
|
||||
|
||||
router.GET(path+"/harvest", func(c *gin.Context) {
|
||||
})
|
||||
|
||||
router.POST(path+"/harvest", func(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
48
wagfarm-api/routes/log/log_routes.go
Normal file
48
wagfarm-api/routes/log/log_routes.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"wagfarm-api/database"
|
||||
"wagfarm-api/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterRoutes(router *gin.Engine) {
|
||||
db := database.DB
|
||||
group := router.Group("/log")
|
||||
|
||||
RegisterHarvestRoutes(router, path)
|
||||
|
||||
router.GET(path+"-type", func(c *gin.Context) {
|
||||
var types []models.LogType
|
||||
db.Find(&types)
|
||||
c.JSON(http.StatusOK, types)
|
||||
})
|
||||
|
||||
router.POST(path+"-type", func(c *gin.Context) {
|
||||
var logType models.LogType
|
||||
if err := c.ShouldBindJSON(&logType); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
db.Create(&logType)
|
||||
c.JSON(http.StatusCreated, logType)
|
||||
})
|
||||
|
||||
router.GET(path, func(c *gin.Context) {
|
||||
var logs []models.Log
|
||||
db.Preload("LogType").Preload("Equipment").Preload("Substance").Find(&logs)
|
||||
c.JSON(http.StatusOK, logs)
|
||||
})
|
||||
|
||||
router.POST(path, func(c *gin.Context) {
|
||||
var log models.Log
|
||||
if err := c.ShouldBindJSON(&log); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
db.Create(&log)
|
||||
c.JSON(http.StatusCreated, log)
|
||||
})
|
||||
}
|
||||
13
wagfarm-api/routes/routes.go
Normal file
13
wagfarm-api/routes/routes.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"wagfarm-api/routes/asset"
|
||||
"wagfarm-api/routes/log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func SetupRoutes(router *gin.Engine) {
|
||||
log.RegisterRoutes(router)
|
||||
asset.RegisterRoutes(router)
|
||||
}
|
||||
Reference in New Issue
Block a user