mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
feat(frontend): create volume
This commit is contained in:
@@ -2,56 +2,57 @@
|
||||
package volumes
|
||||
|
||||
import (
|
||||
"ironmount/internal/core"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// SetupHandlers sets up the API routes for the application.
|
||||
func SetupHandlers(router *gin.Engine) {
|
||||
|
||||
volumeService := VolumeService{}
|
||||
|
||||
router.GET("/api/volumes", func(c *gin.Context) {
|
||||
volumes := volumeService.ListVolumes()
|
||||
|
||||
log.Debug().Msgf("Listing volumes: %v", volumes)
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"volumes": volumes,
|
||||
})
|
||||
c.JSON(200, gin.H{"volumes": volumes})
|
||||
})
|
||||
|
||||
router.POST("/api/volumes", func(c *gin.Context) {
|
||||
var req struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(400, gin.H{"error": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
volume, status, err := volumeService.CreateVolume(req.Name)
|
||||
clean := core.Slugify(req.Name)
|
||||
if clean == "" || clean != req.Name {
|
||||
c.JSON(400, gin.H{"error": "invalid volume name"})
|
||||
return
|
||||
}
|
||||
|
||||
volume, status, err := volumeService.CreateVolume(clean)
|
||||
if err != nil {
|
||||
c.JSON(status, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(status, volume)
|
||||
})
|
||||
|
||||
router.GET("/api/volumes/:name", func(c *gin.Context) {
|
||||
volume, err := volumeService.GetVolume(c.Param("name"))
|
||||
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if volume == nil {
|
||||
c.JSON(404, gin.H{"error": "Volume not found"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"name": volume.Name,
|
||||
"mountpoint": volume.Path,
|
||||
@@ -62,12 +63,10 @@ func SetupHandlers(router *gin.Engine) {
|
||||
|
||||
router.DELETE("/api/volumes/:name", func(c *gin.Context) {
|
||||
status, err := volumeService.DeleteVolume(c.Param("name"))
|
||||
|
||||
if err != nil {
|
||||
c.JSON(status, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{"message": "Volume deleted successfully"})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user