46 lines
999 B
Go
46 lines
999 B
Go
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)
|
|
})
|
|
}
|