veza/dev-environment/templates/backend-handler.template.go
2025-12-03 22:56:50 +01:00

87 lines
2 KiB
Go

package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"veza-backend-api/internal/types"
"veza-backend-api/internal/models"
)
// {{HANDLER_NAME}}Handlers handles HTTP requests for {{DOMAIN}}
type {{HANDLER_NAME}}Handlers struct {
{{SERVICE_LOWER}}Service types.{{SERVICE_INTERFACE}}
}
// New{{HANDLER_NAME}}Handlers creates a new {{HANDLER_NAME}}Handlers instance
func New{{HANDLER_NAME}}Handlers(
{{SERVICE_LOWER}}Service types.{{SERVICE_INTERFACE}},
) *{{HANDLER_NAME}}Handlers {
return &{{HANDLER_NAME}}Handlers{
{{SERVICE_LOWER}}Service: {{SERVICE_LOWER}}Service,
}
}
// Create{{ENTITY}} handles POST /api/v1/{{RESOURCE_PATH}}
func (h *{{HANDLER_NAME}}Handlers) Create{{ENTITY}}(c *gin.Context) {
var req Create{{ENTITY}}Request
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "invalid request",
"details": err.Error(),
})
return
}
{{ENTITY_LOWER}}, err := h.{{SERVICE_LOWER}}Service.Create{{ENTITY}}(c.Request.Context(), &req)
if err != nil {
handleError(c, err)
return
}
c.JSON(http.StatusCreated, gin.H{
"data": {{ENTITY_LOWER}},
})
}
// Get{{ENTITY}} handles GET /api/v1/{{RESOURCE_PATH}}/:id
func (h *{{HANDLER_NAME}}Handlers) Get{{ENTITY}}(c *gin.Context) {
idParam := c.Param("id")
id, err := uuid.Parse(idParam)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "invalid id format",
})
return
}
{{ENTITY_LOWER}}, err := h.{{SERVICE_LOWER}}Service.Get{{ENTITY}}ByID(c.Request.Context(), id)
if err != nil {
handleError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"data": {{ENTITY_LOWER}},
})
}
// handleError handles errors in a consistent way
func handleError(c *gin.Context, err error) {
// TODO: Implement error handling logic
// Check error type and return appropriate status code
c.JSON(http.StatusInternalServerError, gin.H{
"error": "internal server error",
})
}
// Create{{ENTITY}}Request represents the request body for creating a {{ENTITY}}
type Create{{ENTITY}}Request struct {
// TODO: Add request fields with JSON tags
}