refactor: Flatten directory structure
Move project files from uzdb/ subdirectory to root directory for cleaner project structure.
Changes:
- Move frontend/ to root
- Move internal/ to root
- Move build/ to root
- Move all config files (go.mod, wails.json, etc.) to root
- Remove redundant uzdb/ subdirectory nesting
Project structure is now:
├── frontend/ # React application
├── internal/ # Go backend
├── build/ # Wails build assets
├── doc/ # Design documentation
├── main.go # Entry point
└── ...
🤖 Generated with Qoder
This commit is contained in:
195
internal/handler/connection.go
Normal file
195
internal/handler/connection.go
Normal file
@@ -0,0 +1,195 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"uzdb/internal/config"
|
||||
"uzdb/internal/models"
|
||||
"uzdb/internal/services"
|
||||
"uzdb/internal/utils"
|
||||
)
|
||||
|
||||
// ConnectionHandler handles connection-related HTTP requests
|
||||
type ConnectionHandler struct {
|
||||
connectionSvc *services.ConnectionService
|
||||
}
|
||||
|
||||
// NewConnectionHandler creates a new connection handler
|
||||
func NewConnectionHandler(connectionSvc *services.ConnectionService) *ConnectionHandler {
|
||||
return &ConnectionHandler{
|
||||
connectionSvc: connectionSvc,
|
||||
}
|
||||
}
|
||||
|
||||
// GetAllConnections handles GET /api/connections
|
||||
func (h *ConnectionHandler) GetAllConnections(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
connections, err := h.connectionSvc.GetAllConnections(ctx)
|
||||
if err != nil {
|
||||
utils.ErrorResponse(c, http.StatusInternalServerError, err, "Failed to get connections")
|
||||
return
|
||||
}
|
||||
|
||||
utils.SuccessResponse(c, gin.H{
|
||||
"connections": connections,
|
||||
})
|
||||
}
|
||||
|
||||
// GetConnection handles GET /api/connections/:id
|
||||
func (h *ConnectionHandler) GetConnection(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
id := c.Param("id")
|
||||
|
||||
if id == "" {
|
||||
utils.ErrorResponse(c, http.StatusBadRequest, models.ErrValidationFailed, "Connection ID is required")
|
||||
return
|
||||
}
|
||||
|
||||
conn, err := h.connectionSvc.GetConnectionByID(ctx, id)
|
||||
if err != nil {
|
||||
if err == models.ErrNotFound {
|
||||
utils.ErrorResponse(c, http.StatusNotFound, err, "Connection not found")
|
||||
return
|
||||
}
|
||||
utils.ErrorResponse(c, http.StatusInternalServerError, err, "Failed to get connection")
|
||||
return
|
||||
}
|
||||
|
||||
utils.SuccessResponse(c, gin.H{
|
||||
"connection": conn,
|
||||
})
|
||||
}
|
||||
|
||||
// CreateConnection handles POST /api/connections
|
||||
func (h *ConnectionHandler) CreateConnection(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
var req models.CreateConnectionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.ErrorResponse(c, http.StatusBadRequest, models.ErrValidationFailed, "Invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
conn, err := h.connectionSvc.CreateConnection(ctx, &req)
|
||||
if err != nil {
|
||||
if err == models.ErrValidationFailed {
|
||||
utils.ErrorResponse(c, http.StatusBadRequest, err, "Validation failed")
|
||||
return
|
||||
}
|
||||
utils.ErrorResponse(c, http.StatusInternalServerError, err, "Failed to create connection")
|
||||
return
|
||||
}
|
||||
|
||||
config.GetLogger().Info("connection created via API",
|
||||
zap.String("id", conn.ID),
|
||||
zap.String("name", conn.Name))
|
||||
|
||||
utils.CreatedResponse(c, gin.H{
|
||||
"connection": conn,
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateConnection handles PUT /api/connections/:id
|
||||
func (h *ConnectionHandler) UpdateConnection(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
id := c.Param("id")
|
||||
|
||||
if id == "" {
|
||||
utils.ErrorResponse(c, http.StatusBadRequest, models.ErrValidationFailed, "Connection ID is required")
|
||||
return
|
||||
}
|
||||
|
||||
var req models.UpdateConnectionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.ErrorResponse(c, http.StatusBadRequest, models.ErrValidationFailed, "Invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
conn, err := h.connectionSvc.UpdateConnection(ctx, id, &req)
|
||||
if err != nil {
|
||||
if err == models.ErrNotFound {
|
||||
utils.ErrorResponse(c, http.StatusNotFound, err, "Connection not found")
|
||||
return
|
||||
}
|
||||
utils.ErrorResponse(c, http.StatusInternalServerError, err, "Failed to update connection")
|
||||
return
|
||||
}
|
||||
|
||||
utils.SuccessResponse(c, gin.H{
|
||||
"connection": conn,
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteConnection handles DELETE /api/connections/:id
|
||||
func (h *ConnectionHandler) DeleteConnection(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
id := c.Param("id")
|
||||
|
||||
if id == "" {
|
||||
utils.ErrorResponse(c, http.StatusBadRequest, models.ErrValidationFailed, "Connection ID is required")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.connectionSvc.DeleteConnection(ctx, id); err != nil {
|
||||
if err == models.ErrNotFound {
|
||||
utils.ErrorResponse(c, http.StatusNotFound, err, "Connection not found")
|
||||
return
|
||||
}
|
||||
utils.ErrorResponse(c, http.StatusInternalServerError, err, "Failed to delete connection")
|
||||
return
|
||||
}
|
||||
|
||||
utils.SuccessResponse(c, gin.H{
|
||||
"message": "Connection deleted successfully",
|
||||
})
|
||||
}
|
||||
|
||||
// TestConnection handles POST /api/connections/:id/test
|
||||
func (h *ConnectionHandler) TestConnection(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
id := c.Param("id")
|
||||
|
||||
if id == "" {
|
||||
utils.ErrorResponse(c, http.StatusBadRequest, models.ErrValidationFailed, "Connection ID is required")
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.connectionSvc.TestConnection(ctx, id)
|
||||
if err != nil {
|
||||
if err == models.ErrNotFound {
|
||||
utils.ErrorResponse(c, http.StatusNotFound, err, "Connection not found")
|
||||
return
|
||||
}
|
||||
utils.ErrorResponse(c, http.StatusInternalServerError, err, "Failed to test connection")
|
||||
return
|
||||
}
|
||||
|
||||
statusCode := http.StatusOK
|
||||
if !result.Success {
|
||||
statusCode = http.StatusBadGateway
|
||||
}
|
||||
|
||||
c.JSON(statusCode, gin.H{
|
||||
"success": result.Success,
|
||||
"message": result.Message,
|
||||
"duration_ms": result.Duration,
|
||||
"metadata": result.Metadata,
|
||||
})
|
||||
}
|
||||
|
||||
// RegisterRoutes registers connection routes
|
||||
func (h *ConnectionHandler) RegisterRoutes(router *gin.RouterGroup) {
|
||||
connections := router.Group("/connections")
|
||||
{
|
||||
connections.GET("", h.GetAllConnections)
|
||||
connections.GET("/:id", h.GetConnection)
|
||||
connections.POST("", h.CreateConnection)
|
||||
connections.PUT("/:id", h.UpdateConnection)
|
||||
connections.DELETE("/:id", h.DeleteConnection)
|
||||
connections.POST("/:id/test", h.TestConnection)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user