trying around with different routing and controller structures
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
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)
|
||||
})
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"wagfarm-api/database"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
/* used columns:
|
||||
name, date, plant, equipment
|
||||
*/
|
||||
|
||||
func RegisterHarvestRoutes(router *gin.Engine) {
|
||||
db := database.DB
|
||||
|
||||
router.GET("/harvest", func(c *gin.Context) {
|
||||
var harvests []models.Log
|
||||
db.Find(&harvests)
|
||||
c.JSON(http.StatusOK, types)
|
||||
})
|
||||
|
||||
router.POST("/harvest", func(c *gin.Context) {
|
||||
})
|
||||
router.PUT("/harvest", func(c *gin.Context) {
|
||||
|
||||
})
|
||||
router.DELELTE("/harvest", func(c *gin.Context) {
|
||||
|
||||
})
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
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(group)
|
||||
|
||||
group.GET("/type", func(c *gin.Context) {
|
||||
var types []models.LogType
|
||||
db.Find(&types)
|
||||
c.JSON(http.StatusOK, types)
|
||||
})
|
||||
|
||||
group.POST("/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)
|
||||
})
|
||||
|
||||
group.GET("/", func(c *gin.Context) {
|
||||
var logs []models.Log
|
||||
db.Preload("LogType").Preload("Equipment").Preload("Substance").Find(&logs)
|
||||
c.JSON(http.StatusOK, logs)
|
||||
})
|
||||
}
|
||||
27
wagfarm-api/routes/logs.go
Normal file
27
wagfarm-api/routes/logs.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"wagfarm-api/controllers/logs"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterLogRoutes(rg *gin.RouterGroup) {
|
||||
group := rg.Group("/log")
|
||||
{
|
||||
RegisterLog(group, &logs.GenericLogController{})
|
||||
RegisterLog(group.Group("/seeding"), &logs.SeedingLogController{})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterLog(rg *gin.RouterGroup controller logs.LogController) {
|
||||
group := rg.Group(routePrefix)
|
||||
{
|
||||
group.GET("/", controller.GetAll)
|
||||
group.GET("/:id", controller.GetOne)
|
||||
group.POST("/", controller.Create)
|
||||
group.PUT("/:id", controller.Update)
|
||||
group.DELETE("/:id", controller.Delete)
|
||||
}
|
||||
}
|
||||
@@ -1,13 +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)
|
||||
func RegisterRoutes(r *gin.Engine) {
|
||||
// Use API versioning if desired, e.g., v1
|
||||
v1 := r.Group("/v1")
|
||||
{
|
||||
RegisterLogRoutes(v1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user