Perbaikan Lanjutan
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
@@ -12,6 +13,14 @@ import (
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// runSwagInit runs the swag init command to generate swagger docs
|
||||
func runSwagInit() error {
|
||||
cmd := exec.Command("swag", "init", "-g", "../../cmd/api/main.go", "--parseDependency", "--parseInternal")
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
// ServiceConfig represents the main configuration structure
|
||||
type ServiceConfig struct {
|
||||
Services map[string]Service `yaml:"services"`
|
||||
@@ -30,16 +39,16 @@ type GlobalConfig struct {
|
||||
|
||||
// Service represents individual service configuration
|
||||
type Service struct {
|
||||
Name string `yaml:"name"`
|
||||
Category string `yaml:"category"`
|
||||
Package string `yaml:"package"`
|
||||
Description string `yaml:"description"`
|
||||
BaseURL string `yaml:"base_url"`
|
||||
Timeout int `yaml:"timeout"`
|
||||
RetryCount int `yaml:"retry_count"`
|
||||
Endpoints map[string]Endpoint `yaml:"endpoints"`
|
||||
Middleware []string `yaml:"middleware,omitempty"` // FIXED: Changed to []string
|
||||
Dependencies []string `yaml:"dependencies,omitempty"` // FIXED: Changed to []string
|
||||
Name string `yaml:"name"`
|
||||
Category string `yaml:"category"`
|
||||
Package string `yaml:"package"`
|
||||
Description string `yaml:"description"`
|
||||
BaseURL string `yaml:"base_url"`
|
||||
Timeout int `yaml:"timeout"`
|
||||
RetryCount int `yaml:"retry_count"`
|
||||
Endpoints map[string]SubEndpoints `yaml:"endpoints"`
|
||||
Middleware []string `yaml:"middleware,omitempty"` // FIXED: Changed to []string
|
||||
Dependencies []string `yaml:"dependencies,omitempty"` // FIXED: Changed to []string
|
||||
}
|
||||
|
||||
// Endpoint represents endpoint configuration
|
||||
@@ -59,9 +68,12 @@ type Endpoint struct {
|
||||
RateLimit int `yaml:"rate_limit,omitempty"`
|
||||
CacheEnabled bool `yaml:"cache_enabled"`
|
||||
CacheTTL int `yaml:"cache_ttl,omitempty"`
|
||||
CustomHeaders map[string]string `yaml:"custom_headers,omitempty"` // ADDED: Missing field
|
||||
CustomHeaders map[string]string `yaml:"custom_headers,omitempty"`
|
||||
}
|
||||
|
||||
// SubEndpoints represents nested endpoint configuration for tree structure
|
||||
type SubEndpoints map[string]Endpoint
|
||||
|
||||
// TemplateData holds data for generating handlers
|
||||
type TemplateData struct {
|
||||
ServiceName string
|
||||
@@ -118,12 +130,11 @@ type EndpointData struct {
|
||||
RequiredFields []string
|
||||
OptionalFields []string
|
||||
CustomHeaders map[string]string
|
||||
ModelPackage string // Package name for the model (peserta, sep, etc.)
|
||||
}
|
||||
|
||||
// Template remains the same as before...
|
||||
// Updated template for merged handler structure
|
||||
const handlerTemplate = `
|
||||
// Code generated by generate-dynamic-handler.go; DO NOT EDIT.
|
||||
// Generated at: {{.Timestamp}}
|
||||
// Service: {{.ServiceName}} ({{.Category}})
|
||||
// Description: {{.Description}}
|
||||
|
||||
@@ -131,11 +142,15 @@ package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"{{.ModuleName}}/internal/config"
|
||||
"{{.ModuleName}}/internal/models/reference"
|
||||
"{{.ModuleName}}/internal/models"
|
||||
"{{.ModuleName}}/internal/models/vclaim/peserta"
|
||||
"{{.ModuleName}}/internal/models/vclaim/sep"
|
||||
"{{.ModuleName}}/internal/services/bpjs"
|
||||
"{{.ModuleName}}/pkg/logger"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -143,27 +158,9 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// {{.ServiceName}}Service defines {{.ServiceName}} service interface
|
||||
type {{.ServiceName}}Service interface {
|
||||
{{range .Endpoints}}
|
||||
{{if .HasGet}}
|
||||
Get{{.Name}}(ctx context.Context{{range .PathParams}}, {{.}} string{{end}}) (*reference.{{.Name}}Data, error)
|
||||
{{end}}
|
||||
{{if .HasPost}}
|
||||
Create{{.Name}}(ctx context.Context, req *reference.{{.Model}}) (*reference.{{.Name}}Data, error)
|
||||
{{end}}
|
||||
{{if .HasPut}}
|
||||
Update{{.Name}}(ctx context.Context{{range .PathParams}}, {{.}} string{{end}}, req *reference.{{.Model}}) (*reference.{{.Name}}Data, error)
|
||||
{{end}}
|
||||
{{if .HasDelete}}
|
||||
Delete{{.Name}}(ctx context.Context{{range .PathParams}}, {{.}} string{{end}}) error
|
||||
{{end}}
|
||||
{{end}}
|
||||
}
|
||||
|
||||
// {{.ServiceName}}Handler handles {{.ServiceName}} BPJS services
|
||||
type {{.ServiceName}}Handler struct {
|
||||
service {{.ServiceName}}Service
|
||||
service services.{{.ServiceName}}Service
|
||||
validator *validator.Validate
|
||||
logger logger.Logger
|
||||
config config.BpjsConfig
|
||||
@@ -177,35 +174,35 @@ type {{.ServiceName}}HandlerConfig struct {
|
||||
}
|
||||
|
||||
// New{{.ServiceName}}Handler creates a new {{.ServiceName}}Handler
|
||||
func New{{.ServiceName}}Handler(cfg {{.ServiceName}}HandlerConfig, service {{.ServiceName}}Service) *{{.ServiceName}}Handler {
|
||||
func New{{.ServiceName}}Handler(cfg {{.ServiceName}}HandlerConfig) *{{.ServiceName}}Handler {
|
||||
return &{{.ServiceName}}Handler{
|
||||
service: service,
|
||||
service: services.NewService(cfg.BpjsConfig),
|
||||
validator: cfg.Validator,
|
||||
logger: cfg.Logger,
|
||||
config: cfg.BpjsConfig,
|
||||
}
|
||||
}
|
||||
|
||||
{{range .Endpoints}}
|
||||
{{if .HasGet}}
|
||||
// Get{{.NameUpper}} retrieves {{.Name}} data
|
||||
{{if $.HasSwagger}}
|
||||
// @Summary Get {{.Name}} data
|
||||
// @Description {{.Description}}
|
||||
// @Tags {{join .Tags ","}}
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// Get{{.NameUpper}} godoc
|
||||
// @Summary Get {{.Name}} data
|
||||
// @Description {{.Description}}
|
||||
// @Tags {{join .Tags ","}}
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
{{if .RequireAuth}}
|
||||
// @Security ApiKeyAuth
|
||||
// @Security ApiKeyAuth
|
||||
{{end}}
|
||||
// @Param X-Request-ID header string false "Request ID for tracking"
|
||||
{{range .PathParams}}
|
||||
// @Param {{.}} path string true "{{.}}"
|
||||
{{end}}
|
||||
// @Success 200 {object} reference.{{.ResponseModel}}
|
||||
// @Failure 400 {object} reference.ErrorResponse
|
||||
// @Failure 500 {object} reference.ErrorResponse
|
||||
// @Router {{.GetPath}} [get]
|
||||
// @Param {{.}} path string true "{{.}}" example("example_value")
|
||||
{{end}}
|
||||
// @Success 200 {object} {{.ModelPackage}}.{{.ResponseModel}} "Successfully retrieved {{.Name}} data"
|
||||
// @Failure 400 {object} models.ErrorResponseBpjs "Bad request - invalid parameters"
|
||||
// @Failure 401 {object} models.ErrorResponseBpjs "Unauthorized - invalid API credentials"
|
||||
// @Failure 404 {object} models.ErrorResponseBpjs "Not found - {{.Name}} not found"
|
||||
// @Failure 500 {object} models.ErrorResponseBpjs "Internal server error"
|
||||
// @Router {{.GetPath}} [get]
|
||||
func (h *{{$.ServiceName}}Handler) Get{{.Name}}(c *gin.Context) {
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), {{$.Timeout}}*time.Second)
|
||||
defer cancel()
|
||||
@@ -236,7 +233,7 @@ func (h *{{$.ServiceName}}Handler) Get{{.Name}}(c *gin.Context) {
|
||||
"request_id": requestID,
|
||||
})
|
||||
{{end}}
|
||||
c.JSON(http.StatusBadRequest, reference.ErrorResponse{
|
||||
c.JSON(http.StatusBadRequest, models.ErrorResponseBpjs{
|
||||
Status: "error",
|
||||
Message: "Missing required parameter {{.}}",
|
||||
RequestID: requestID,
|
||||
@@ -246,11 +243,15 @@ func (h *{{$.ServiceName}}Handler) Get{{.Name}}(c *gin.Context) {
|
||||
{{end}}
|
||||
|
||||
// Call service method
|
||||
var response reference.{{.ResponseModel}}
|
||||
var response {{.ModelPackage}}.{{.ResponseModel}}
|
||||
{{if .PathParams}}
|
||||
result, err := h.service.Get{{.Name}}(ctx{{range .PathParams}}, {{.}}{{end}})
|
||||
endpoint := "{{.GetPath}}"
|
||||
{{range .PathParams}}
|
||||
endpoint = strings.Replace(endpoint, ":{{.}}", {{.}}, 1)
|
||||
{{end}}
|
||||
err := h.service.Get(ctx, endpoint, &response)
|
||||
{{else}}
|
||||
result, err := h.service.Get{{.Name}}(ctx)
|
||||
err := h.service.Get(ctx, "{{.GetPath}}", &response)
|
||||
{{end}}
|
||||
if err != nil {
|
||||
{{if $.HasLogger}}
|
||||
@@ -259,7 +260,7 @@ func (h *{{$.ServiceName}}Handler) Get{{.Name}}(c *gin.Context) {
|
||||
"request_id": requestID,
|
||||
})
|
||||
{{end}}
|
||||
c.JSON(http.StatusInternalServerError, reference.ErrorResponse{
|
||||
c.JSON(http.StatusInternalServerError, models.ErrorResponseBpjs{
|
||||
Status: "error",
|
||||
Message: "Internal server error",
|
||||
RequestID: requestID,
|
||||
@@ -270,28 +271,28 @@ func (h *{{$.ServiceName}}Handler) Get{{.Name}}(c *gin.Context) {
|
||||
// Ensure response has proper fields
|
||||
response.Status = "success"
|
||||
response.RequestID = requestID
|
||||
response.Data = result
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
{{end}}
|
||||
|
||||
{{if .HasPost}}
|
||||
// Create{{.NameUpper}} creates new {{.Name}}
|
||||
{{if $.HasSwagger}}
|
||||
// @Summary Create {{.Name}}
|
||||
// @Description {{.Description}}
|
||||
// @Tags {{join .Tags ","}}
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// Create{{.NameUpper}} godoc
|
||||
// @Summary Create new {{.Name}}
|
||||
// @Description {{.Description}}
|
||||
// @Tags {{join .Tags ","}}
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
{{if .RequireAuth}}
|
||||
// @Security ApiKeyAuth
|
||||
{{end}}
|
||||
// @Param request body reference.{{.Model}} true "{{.Name}} data"
|
||||
// @Success 201 {object} reference.{{.ResponseModel}}
|
||||
// @Failure 400 {object} reference.ErrorResponse
|
||||
// @Failure 500 {object} reference.ErrorResponse
|
||||
// @Router {{.PostPath}} [post]
|
||||
// @Security ApiKeyAuth
|
||||
{{end}}
|
||||
// @Param X-Request-ID header string false "Request ID for tracking"
|
||||
// @Param request body {{.ModelPackage}}.{{.Model}} true "{{.Name}} data"
|
||||
// @Success 201 {object} {{.ModelPackage}}.{{.ResponseModel}} "Successfully created {{.Name}}"
|
||||
// @Failure 400 {object} models.ErrorResponseBpjs "Bad request - invalid request body or validation error"
|
||||
// @Failure 401 {object} models.ErrorResponseBpjs "Unauthorized - invalid API credentials"
|
||||
// @Failure 409 {object} models.ErrorResponseBpjs "Conflict - {{.Name}} already exists"
|
||||
// @Failure 500 {object} models.ErrorResponseBpjs "Internal server error"
|
||||
// @Router {{.PostPath}} [post]
|
||||
func (h *{{$.ServiceName}}Handler) Create{{.Name}}(c *gin.Context) {
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), {{$.Timeout}}*time.Second)
|
||||
defer cancel()
|
||||
@@ -308,7 +309,7 @@ func (h *{{$.ServiceName}}Handler) Create{{.Name}}(c *gin.Context) {
|
||||
})
|
||||
{{end}}
|
||||
|
||||
var req reference.{{.Model}}
|
||||
var req {{.ModelPackage}}.{{.Model}}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
{{if $.HasLogger}}
|
||||
h.logger.Error("Invalid request body", map[string]interface{}{
|
||||
@@ -316,7 +317,7 @@ func (h *{{$.ServiceName}}Handler) Create{{.Name}}(c *gin.Context) {
|
||||
"request_id": requestID,
|
||||
})
|
||||
{{end}}
|
||||
c.JSON(http.StatusBadRequest, reference.ErrorResponse{
|
||||
c.JSON(http.StatusBadRequest, models.ErrorResponseBpjs{
|
||||
Status: "error",
|
||||
Message: "Invalid request body: " + err.Error(),
|
||||
RequestID: requestID,
|
||||
@@ -332,7 +333,7 @@ func (h *{{$.ServiceName}}Handler) Create{{.Name}}(c *gin.Context) {
|
||||
"request_id": requestID,
|
||||
})
|
||||
{{end}}
|
||||
c.JSON(http.StatusBadRequest, reference.ErrorResponse{
|
||||
c.JSON(http.StatusBadRequest, models.ErrorResponseBpjs{
|
||||
Status: "error",
|
||||
Message: "Validation failed: " + err.Error(),
|
||||
RequestID: requestID,
|
||||
@@ -341,8 +342,8 @@ func (h *{{$.ServiceName}}Handler) Create{{.Name}}(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Call service method
|
||||
var response reference.{{.ResponseModel}}
|
||||
result, err := h.service.Create{{.Name}}(ctx, &req)
|
||||
var response {{.ModelPackage}}.{{.ResponseModel}}
|
||||
err := h.service.Post(ctx, "{{.PostPath}}", &req, &response)
|
||||
if err != nil {
|
||||
{{if $.HasLogger}}
|
||||
h.logger.Error("Failed to create {{.Name}}", map[string]interface{}{
|
||||
@@ -350,7 +351,7 @@ func (h *{{$.ServiceName}}Handler) Create{{.Name}}(c *gin.Context) {
|
||||
"request_id": requestID,
|
||||
})
|
||||
{{end}}
|
||||
c.JSON(http.StatusInternalServerError, reference.ErrorResponse{
|
||||
c.JSON(http.StatusInternalServerError, models.ErrorResponseBpjs{
|
||||
Status: "error",
|
||||
Message: "Internal server error",
|
||||
RequestID: requestID,
|
||||
@@ -361,31 +362,31 @@ func (h *{{$.ServiceName}}Handler) Create{{.Name}}(c *gin.Context) {
|
||||
// Ensure response has proper fields
|
||||
response.Status = "success"
|
||||
response.RequestID = requestID
|
||||
response.Data = result
|
||||
c.JSON(http.StatusCreated, response)
|
||||
}
|
||||
{{end}}
|
||||
|
||||
{{if .HasPut}}
|
||||
// Update{{.NameUpper}} updates existing {{.Name}}
|
||||
{{if $.HasSwagger}}
|
||||
// @Summary Update {{.Name}}
|
||||
// @Description {{.Description}}
|
||||
// @Tags {{join .Tags ","}}
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// Update{{.NameUpper}} godoc
|
||||
// @Summary Update existing {{.Name}}
|
||||
// @Description {{.Description}}
|
||||
// @Tags {{join .Tags ","}}
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
{{if .RequireAuth}}
|
||||
// @Security ApiKeyAuth
|
||||
// @Security ApiKeyAuth
|
||||
{{end}}
|
||||
// @Param X-Request-ID header string false "Request ID for tracking"
|
||||
{{range .PathParams}}
|
||||
// @Param {{.}} path string true "{{.}}"
|
||||
{{end}}
|
||||
// @Param request body reference.{{.Model}} true "{{.Name}} data"
|
||||
// @Success 200 {object} reference.{{.ResponseModel}}
|
||||
// @Failure 400 {object} reference.ErrorResponse
|
||||
// @Failure 500 {object} reference.ErrorResponse
|
||||
// @Router {{.PutPath}} [put]
|
||||
// @Param {{.}} path string true "{{.}}" example("example_value")
|
||||
{{end}}
|
||||
// @Param request body {{.ModelPackage}}.{{.Model}} true "{{.Name}} data"
|
||||
// @Success 200 {object} {{.ModelPackage}}.{{.ResponseModel}} "Successfully updated {{.Name}}"
|
||||
// @Failure 400 {object} models.ErrorResponseBpjs "Bad request - invalid parameters or request body"
|
||||
// @Failure 401 {object} models.ErrorResponseBpjs "Unauthorized - invalid API credentials"
|
||||
// @Failure 404 {object} models.ErrorResponseBpjs "Not found - {{.Name}} not found"
|
||||
// @Failure 500 {object} models.ErrorResponseBpjs "Internal server error"
|
||||
// @Router {{.PutPath}} [put]
|
||||
func (h *{{$.ServiceName}}Handler) Update{{.Name}}(c *gin.Context) {
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), {{$.Timeout}}*time.Second)
|
||||
defer cancel()
|
||||
@@ -410,7 +411,7 @@ func (h *{{$.ServiceName}}Handler) Update{{.Name}}(c *gin.Context) {
|
||||
"request_id": requestID,
|
||||
})
|
||||
{{end}}
|
||||
c.JSON(http.StatusBadRequest, reference.ErrorResponse{
|
||||
c.JSON(http.StatusBadRequest, models.ErrorResponseBpjs{
|
||||
Status: "error",
|
||||
Message: "Missing required parameter {{.}}",
|
||||
RequestID: requestID,
|
||||
@@ -419,7 +420,7 @@ func (h *{{$.ServiceName}}Handler) Update{{.Name}}(c *gin.Context) {
|
||||
}
|
||||
{{end}}
|
||||
|
||||
var req reference.{{.Model}}
|
||||
var req {{.ModelPackage}}.{{.Model}}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
{{if $.HasLogger}}
|
||||
h.logger.Error("Invalid request body", map[string]interface{}{
|
||||
@@ -427,7 +428,7 @@ func (h *{{$.ServiceName}}Handler) Update{{.Name}}(c *gin.Context) {
|
||||
"request_id": requestID,
|
||||
})
|
||||
{{end}}
|
||||
c.JSON(http.StatusBadRequest, reference.ErrorResponse{
|
||||
c.JSON(http.StatusBadRequest, models.ErrorResponseBpjs{
|
||||
Status: "error",
|
||||
Message: "Invalid request body: " + err.Error(),
|
||||
RequestID: requestID,
|
||||
@@ -443,7 +444,7 @@ func (h *{{$.ServiceName}}Handler) Update{{.Name}}(c *gin.Context) {
|
||||
"request_id": requestID,
|
||||
})
|
||||
{{end}}
|
||||
c.JSON(http.StatusBadRequest, reference.ErrorResponse{
|
||||
c.JSON(http.StatusBadRequest, models.ErrorResponseBpjs{
|
||||
Status: "error",
|
||||
Message: "Validation failed: " + err.Error(),
|
||||
RequestID: requestID,
|
||||
@@ -452,11 +453,15 @@ func (h *{{$.ServiceName}}Handler) Update{{.Name}}(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Call service method
|
||||
var response reference.{{.ResponseModel}}
|
||||
var response {{.ModelPackage}}.{{.ResponseModel}}
|
||||
{{if .PathParams}}
|
||||
result, err := h.service.Update{{.Name}}(ctx{{range .PathParams}}, {{.}}{{end}}, &req)
|
||||
endpoint := "{{.PutPath}}"
|
||||
{{range .PathParams}}
|
||||
endpoint = strings.Replace(endpoint, ":{{.}}", {{.}}, 1)
|
||||
{{end}}
|
||||
err := h.service.Put(ctx, endpoint, &req, &response)
|
||||
{{else}}
|
||||
result, err := h.service.Update{{.Name}}(ctx, &req)
|
||||
err := h.service.Put(ctx, "{{.PutPath}}", &req, &response)
|
||||
{{end}}
|
||||
if err != nil {
|
||||
{{if $.HasLogger}}
|
||||
@@ -465,7 +470,7 @@ func (h *{{$.ServiceName}}Handler) Update{{.Name}}(c *gin.Context) {
|
||||
"request_id": requestID,
|
||||
})
|
||||
{{end}}
|
||||
c.JSON(http.StatusInternalServerError, reference.ErrorResponse{
|
||||
c.JSON(http.StatusInternalServerError, models.ErrorResponseBpjs{
|
||||
Status: "error",
|
||||
Message: "Internal server error",
|
||||
RequestID: requestID,
|
||||
@@ -476,30 +481,30 @@ func (h *{{$.ServiceName}}Handler) Update{{.Name}}(c *gin.Context) {
|
||||
// Ensure response has proper fields
|
||||
response.Status = "success"
|
||||
response.RequestID = requestID
|
||||
response.Data = result
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
{{end}}
|
||||
|
||||
{{if .HasDelete}}
|
||||
// Delete{{.NameUpper}} deletes existing {{.Name}}
|
||||
{{if $.HasSwagger}}
|
||||
// @Summary Delete {{.Name}}
|
||||
// @Description {{.Description}}
|
||||
// @Tags {{join .Tags ","}}
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// Delete{{.NameUpper}} godoc
|
||||
// @Summary Delete existing {{.Name}}
|
||||
// @Description {{.Description}}
|
||||
// @Tags {{join .Tags ","}}
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
{{if .RequireAuth}}
|
||||
// @Security ApiKeyAuth
|
||||
// @Security ApiKeyAuth
|
||||
{{end}}
|
||||
// @Param X-Request-ID header string false "Request ID for tracking"
|
||||
{{range .PathParams}}
|
||||
// @Param {{.}} path string true "{{.}}"
|
||||
{{end}}
|
||||
// @Success 204 {object} nil
|
||||
// @Failure 400 {object} reference.ErrorResponse
|
||||
// @Failure 500 {object} reference.ErrorResponse
|
||||
// @Router {{.DeletePath}} [delete]
|
||||
// @Param {{.}} path string true "{{.}}" example("example_value")
|
||||
{{end}}
|
||||
// @Success 200 {object} {{.ModelPackage}}.{{.ResponseModel}} "Successfully deleted {{.Name}}"
|
||||
// @Failure 400 {object} models.ErrorResponseBpjs "Bad request - invalid parameters"
|
||||
// @Failure 401 {object} models.ErrorResponseBpjs "Unauthorized - invalid API credentials"
|
||||
// @Failure 404 {object} models.ErrorResponseBpjs "Not found - {{.Name}} not found"
|
||||
// @Failure 500 {object} models.ErrorResponseBpjs "Internal server error"
|
||||
// @Router {{.DeletePath}} [delete]
|
||||
func (h *{{$.ServiceName}}Handler) Delete{{.Name}}(c *gin.Context) {
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), {{$.Timeout}}*time.Second)
|
||||
defer cancel()
|
||||
@@ -524,7 +529,7 @@ func (h *{{$.ServiceName}}Handler) Delete{{.Name}}(c *gin.Context) {
|
||||
"request_id": requestID,
|
||||
})
|
||||
{{end}}
|
||||
c.JSON(http.StatusBadRequest, reference.ErrorResponse{
|
||||
c.JSON(http.StatusBadRequest, models.ErrorResponseBpjs{
|
||||
Status: "error",
|
||||
Message: "Missing required parameter {{.}}",
|
||||
RequestID: requestID,
|
||||
@@ -534,10 +539,15 @@ func (h *{{$.ServiceName}}Handler) Delete{{.Name}}(c *gin.Context) {
|
||||
{{end}}
|
||||
|
||||
// Call service method
|
||||
var response {{.ModelPackage}}.{{.ResponseModel}}
|
||||
{{if .PathParams}}
|
||||
err := h.service.Delete{{.Name}}(ctx{{range .PathParams}}, {{.}}{{end}})
|
||||
endpoint := "{{.DeletePath}}"
|
||||
{{range .PathParams}}
|
||||
endpoint = strings.Replace(endpoint, ":{{.}}", {{.}}, 1)
|
||||
{{end}}
|
||||
err := h.service.Delete(ctx, endpoint, &response)
|
||||
{{else}}
|
||||
err := h.service.Delete{{.Name}}(ctx)
|
||||
err := h.service.Delete(ctx, "{{.DeletePath}}", &response)
|
||||
{{end}}
|
||||
if err != nil {
|
||||
{{if $.HasLogger}}
|
||||
@@ -546,7 +556,7 @@ func (h *{{$.ServiceName}}Handler) Delete{{.Name}}(c *gin.Context) {
|
||||
"request_id": requestID,
|
||||
})
|
||||
{{end}}
|
||||
c.JSON(http.StatusInternalServerError, reference.ErrorResponse{
|
||||
c.JSON(http.StatusInternalServerError, models.ErrorResponseBpjs{
|
||||
Status: "error",
|
||||
Message: "Internal server error",
|
||||
RequestID: requestID,
|
||||
@@ -554,7 +564,10 @@ func (h *{{$.ServiceName}}Handler) Delete{{.Name}}(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
// Ensure response has proper fields
|
||||
response.Status = "success"
|
||||
response.RequestID = requestID
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
||||
@@ -614,6 +627,16 @@ func main() {
|
||||
|
||||
if generated > 0 {
|
||||
fmt.Println("🎉 Generation completed successfully!")
|
||||
|
||||
// Generate Swagger documentation if enabled
|
||||
// if config.Global.EnableSwagger {
|
||||
// fmt.Println("📚 Generating Swagger documentation...")
|
||||
// if err := runSwagInit(); err != nil {
|
||||
// fmt.Printf("⚠️ Warning: Failed to generate Swagger docs: %v\n", err)
|
||||
// } else {
|
||||
// fmt.Println("✅ Swagger documentation generated successfully!")
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -675,19 +698,28 @@ func generateHandler(serviceName string, service Service, globalConfig GlobalCon
|
||||
}
|
||||
|
||||
// Check for advanced features
|
||||
for _, endpoint := range service.Endpoints {
|
||||
if endpoint.RequireAuth {
|
||||
templateData.HasAuth = true
|
||||
}
|
||||
if endpoint.CacheEnabled {
|
||||
templateData.HasCache = true
|
||||
for _, subEndpoints := range service.Endpoints {
|
||||
for _, endpoint := range subEndpoints {
|
||||
if endpoint.RequireAuth {
|
||||
templateData.HasAuth = true
|
||||
}
|
||||
if endpoint.CacheEnabled {
|
||||
templateData.HasCache = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process endpoints
|
||||
for endpointName, endpoint := range service.Endpoints {
|
||||
endpointData := processEndpoint(endpointName, endpoint)
|
||||
templateData.Endpoints = append(templateData.Endpoints, endpointData)
|
||||
for endpointName, subEndpoints := range service.Endpoints {
|
||||
for subEndpointName, endpoint := range subEndpoints {
|
||||
// Compose full endpoint name with sub-endpoint name
|
||||
fullEndpointName := endpointName
|
||||
if subEndpointName != "" {
|
||||
fullEndpointName = fullEndpointName + strings.Title(subEndpointName)
|
||||
}
|
||||
endpointData := processEndpoint(fullEndpointName, endpoint, endpointName)
|
||||
templateData.Endpoints = append(templateData.Endpoints, endpointData)
|
||||
}
|
||||
}
|
||||
|
||||
// Create output directory
|
||||
@@ -729,7 +761,7 @@ func generateHandler(serviceName string, service Service, globalConfig GlobalCon
|
||||
return nil
|
||||
}
|
||||
|
||||
func processEndpoint(name string, endpoint Endpoint) EndpointData {
|
||||
func processEndpoint(name string, endpoint Endpoint, endpointGroup string) EndpointData {
|
||||
data := EndpointData{
|
||||
Name: strings.Title(name),
|
||||
NameLower: strings.ToLower(name),
|
||||
@@ -751,6 +783,7 @@ func processEndpoint(name string, endpoint Endpoint) EndpointData {
|
||||
CacheEnabled: endpoint.CacheEnabled,
|
||||
CacheTTL: getOrDefault(endpoint.CacheTTL, 300),
|
||||
CustomHeaders: endpoint.CustomHeaders,
|
||||
ModelPackage: endpointGroup, // Set the model package based on endpoint group
|
||||
}
|
||||
|
||||
// Set method flags and extract path parameters
|
||||
|
||||
Reference in New Issue
Block a user