refactor: Update module and import paths to match new repository structure

This commit is contained in:
garionion 2025-02-28 21:40:40 +01:00 committed by garionion (aider)
parent 9213034093
commit 6f12a06a02
11 changed files with 222 additions and 763 deletions

View file

@ -4,7 +4,7 @@ import (
"github.com/labstack/echo/v4"
"go.uber.org/zap"
db "github.com/your-username/your-repo/database/sqlite/generated"
db "git.entr0py.de/garionion/inventory/database/sqlite/generated"
)
// API holds all API handlers
@ -25,7 +25,7 @@ func New(dbQueries *db.Queries, logger *zap.Logger) *API {
func (a *API) RegisterRoutes(e *echo.Echo, middleware echo.MiddlewareFunc) {
// Create API group with middleware
api := e.Group("/api/v1", middleware)
// Register routes for each handler
a.Storage.RegisterRoutes(api)
}

View file

@ -9,7 +9,7 @@ import (
"github.com/labstack/echo/v4"
"go.uber.org/zap"
db "github.com/your-username/your-repo/database/sqlite/generated"
db "git.entr0py.de/garionion/inventory/database/sqlite/generated"
)
type StorageHandler struct {
@ -27,7 +27,7 @@ func NewStorageHandler(db *db.Queries, logger *zap.Logger) *StorageHandler {
// GetStorageSpaces returns all storage spaces
func (h *StorageHandler) GetStorageSpaces(c echo.Context) error {
ctx := context.Background()
storageSpaces, err := h.db.GetAllStorageSpaces(ctx)
if err != nil {
h.logger.Error("Failed to fetch storage spaces", zap.Error(err))
@ -35,7 +35,7 @@ func (h *StorageHandler) GetStorageSpaces(c echo.Context) error {
"error": "Failed to fetch storage spaces",
})
}
// Convert to a format suitable for the frontend
result := make([]map[string]interface{}, len(storageSpaces))
for i, space := range storageSpaces {
@ -45,14 +45,14 @@ func (h *StorageHandler) GetStorageSpaces(c echo.Context) error {
"location": space.Location,
}
}
return c.JSON(http.StatusOK, result)
}
// GetObjectsInStorage returns all objects in a specific storage space
func (h *StorageHandler) GetObjectsInStorage(c echo.Context) error {
ctx := context.Background()
// Parse storage ID from URL
storageIDStr := c.Param("id")
storageID, err := strconv.ParseInt(storageIDStr, 10, 64)
@ -62,7 +62,7 @@ func (h *StorageHandler) GetObjectsInStorage(c echo.Context) error {
"error": "Invalid storage ID",
})
}
// Check if storage exists
_, err = h.db.GetStorageSpaceByID(ctx, storageID)
if err != nil {
@ -76,7 +76,7 @@ func (h *StorageHandler) GetObjectsInStorage(c echo.Context) error {
"error": "Failed to fetch storage space",
})
}
// Get objects in this storage
objects, err := h.db.GetObjectsByStorageID(ctx, storageID)
if err != nil {
@ -85,7 +85,7 @@ func (h *StorageHandler) GetObjectsInStorage(c echo.Context) error {
"error": "Failed to fetch objects in storage",
})
}
// Convert to a format suitable for the frontend
result := make([]map[string]interface{}, len(objects))
for i, obj := range objects {
@ -97,14 +97,14 @@ func (h *StorageHandler) GetObjectsInStorage(c echo.Context) error {
"serialnumber": obj.Serialnumber.String,
}
}
return c.JSON(http.StatusOK, result)
}
// GetStorageHierarchyObjects returns all objects in a storage hierarchy
func (h *StorageHandler) GetStorageHierarchyObjects(c echo.Context) error {
ctx := context.Background()
// Parse root storage ID from URL
rootStorageIDStr := c.Param("id")
rootStorageID, err := strconv.ParseInt(rootStorageIDStr, 10, 64)
@ -114,7 +114,7 @@ func (h *StorageHandler) GetStorageHierarchyObjects(c echo.Context) error {
"error": "Invalid storage ID",
})
}
// Check if root storage exists
_, err = h.db.GetStorageSpaceByID(ctx, rootStorageID)
if err != nil {
@ -128,7 +128,7 @@ func (h *StorageHandler) GetStorageHierarchyObjects(c echo.Context) error {
"error": "Failed to fetch storage space",
})
}
// Get all storage spaces to build the hierarchy
allStorageSpaces, err := h.db.GetAllStorageSpaces(ctx)
if err != nil {
@ -137,24 +137,24 @@ func (h *StorageHandler) GetStorageHierarchyObjects(c echo.Context) error {
"error": "Failed to fetch storage hierarchy",
})
}
// Build a list of all storage IDs in the hierarchy
storageIDs := []int64{rootStorageID}
storageIDs = append(storageIDs, h.getChildStorageIDs(allStorageSpaces, rootStorageID)...)
// Get objects for all storage spaces in the hierarchy
var allObjects []map[string]interface{}
for _, storageID := range storageIDs {
// Get objects in this storage
objects, err := h.db.GetObjectsByStorageID(ctx, storageID)
if err != nil {
h.logger.Error("Failed to fetch objects in storage",
h.logger.Error("Failed to fetch objects in storage",
zap.Int64("storageID", storageID),
zap.Error(err))
continue // Skip this storage if there's an error
}
// Convert to a format suitable for the frontend
for _, obj := range objects {
allObjects = append(allObjects, map[string]interface{}{
@ -166,14 +166,14 @@ func (h *StorageHandler) GetStorageHierarchyObjects(c echo.Context) error {
})
}
}
return c.JSON(http.StatusOK, allObjects)
}
// Helper function to recursively get all child storage IDs
func (h *StorageHandler) getChildStorageIDs(spaces []db.Storagespace, parentID int64) []int64 {
var childIDs []int64
for _, space := range spaces {
if space.Parent.Valid && space.Parent.Int64 == parentID {
childIDs = append(childIDs, space.ID)
@ -181,7 +181,7 @@ func (h *StorageHandler) getChildStorageIDs(spaces []db.Storagespace, parentID i
childIDs = append(childIDs, h.getChildStorageIDs(spaces, space.ID)...)
}
}
return childIDs
}