Machines done
This commit is contained in:
37
machines/app/api/helpers.go
Normal file
37
machines/app/api/helpers.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func isError(c *gin.Context, err error) bool {
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
|
"details": err.Error(),
|
||||||
|
})
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func raiseError(c *gin.Context, errCode int, message string) {
|
||||||
|
c.AbortWithStatusJSON(errCode, gin.H{
|
||||||
|
"details": message,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func raiseBadRequestError(c *gin.Context, message string) {
|
||||||
|
raiseError(c, http.StatusBadRequest, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
func raiseNotFoundError(c *gin.Context, message string) {
|
||||||
|
raiseError(c, http.StatusNotFound, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
func raiseInternalError(c *gin.Context, message string) {
|
||||||
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
|
||||||
|
"details": "Internal server error. We will we will fix it!",
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"machines/app/db"
|
"machines/app/db"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@ -24,24 +25,44 @@ func handlePing(c *gin.Context) {
|
|||||||
func handleGetMachines(dbConn *gorm.DB) gin.HandlerFunc {
|
func handleGetMachines(dbConn *gorm.DB) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
machines := db.GetMachines(dbConn)
|
machines := db.GetMachines(dbConn)
|
||||||
fmt.Printf("%+v\n", machines)
|
|
||||||
|
|
||||||
c.JSON(
|
c.JSON(
|
||||||
http.StatusOK,
|
http.StatusOK,
|
||||||
GetMachinesResponse{
|
GetMachinesResponse{
|
||||||
Machines: machines,
|
Machines: *machines,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleGetMachine(dbConn *gorm.DB) gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
machineId, err := strconv.Atoi(c.Param("machineId"))
|
||||||
|
if err != nil {
|
||||||
|
raiseBadRequestError(c, "Invalid machineId parameter")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
machine, err := db.GetMachine(dbConn, machineId)
|
||||||
|
if err != nil {
|
||||||
|
raiseNotFoundError(c, "Machine not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(
|
||||||
|
http.StatusOK,
|
||||||
|
machine,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func initRouter(dbConn *gorm.DB) *gin.Engine {
|
func initRouter(dbConn *gorm.DB) *gin.Engine {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
||||||
router.GET("/ping", handlePing)
|
router.GET("/ping", handlePing)
|
||||||
router.GET("/", handleGetMachines(dbConn))
|
router.GET("/", handleGetMachines(dbConn))
|
||||||
// routes.GET("/machines/:machineId", handleGetMachineDetails)
|
router.GET("/:machineId", handleGetMachine(dbConn))
|
||||||
|
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,10 +2,21 @@ package db
|
|||||||
|
|
||||||
import "gorm.io/gorm"
|
import "gorm.io/gorm"
|
||||||
|
|
||||||
func GetMachines(dbConn *gorm.DB) []Machine {
|
func GetMachines(dbConn *gorm.DB) *[]Machine {
|
||||||
var machines []Machine
|
var machines []Machine
|
||||||
|
|
||||||
dbConn.Order("name").Find(&machines)
|
dbConn.Order("name").Find(&machines)
|
||||||
|
|
||||||
return machines
|
return &machines
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetMachine(dbConn *gorm.DB, id int) (*Machine, error) {
|
||||||
|
var machine Machine
|
||||||
|
|
||||||
|
result := dbConn.Order("name").Where("id = ?", id).First(&machine)
|
||||||
|
if result.Error != nil {
|
||||||
|
return nil, result.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
return &machine, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user