Skip to Content

gofasta g controller

Generates a REST controller for a given resource with full CRUD endpoint handlers. Each handler includes Swagger annotation comments for automatic API documentation generation. The controller delegates all business logic to the service layer.

Usage

gofasta g controller <ResourceName> [flags]

Flags

FlagShortDefaultDescription
--methods-mallComma-separated list of handlers to generate. Options: create, findAll, findByID, update, delete
--authtrueInclude authentication middleware annotations

Examples

Generate a controller with all CRUD handlers:

gofasta g controller Product

Generate with only specific handlers:

gofasta g controller Product --methods create,findAll,findByID

Generate without authentication annotations:

gofasta g controller Product --auth=false

What It Generates

Running gofasta g controller Product creates one file:

app/rest/controllers/product.controller.go

Generated Code

// app/rest/controllers/product.controller.go package controllers import ( "net/http" "strconv" "github.com/gin-gonic/gin" "myapp/app/dtos" svcInterfaces "myapp/app/services/interfaces" ) type ProductController struct { service svcInterfaces.ProductService } func NewProductController(service svcInterfaces.ProductService) *ProductController { return &ProductController{service: service} } // Create godoc // @Summary Create a new product // @Description Create a new product with the provided data // @Tags Products // @Accept json // @Produce json // @Param body body dtos.CreateProductRequest true "Product data" // @Success 201 {object} dtos.ProductResponse // @Failure 400 {object} dtos.ErrorResponse // @Failure 500 {object} dtos.ErrorResponse // @Security BearerAuth // @Router /api/v1/products [post] func (c *ProductController) Create(ctx *gin.Context) { var req dtos.CreateProductRequest if err := ctx.ShouldBindJSON(&req); err != nil { ctx.JSON(http.StatusBadRequest, dtos.ErrorResponse{Error: err.Error()}) return } result, err := c.service.Create(req) if err != nil { ctx.JSON(http.StatusInternalServerError, dtos.ErrorResponse{Error: err.Error()}) return } ctx.JSON(http.StatusCreated, result) } // FindAll godoc // @Summary List all products // @Description Get a paginated list of products // @Tags Products // @Produce json // @Param page query int false "Page number" default(1) // @Param limit query int false "Items per page" default(10) // @Success 200 {object} dtos.PaginatedProductResponse // @Failure 500 {object} dtos.ErrorResponse // @Security BearerAuth // @Router /api/v1/products [get] func (c *ProductController) FindAll(ctx *gin.Context) { page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1")) limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "10")) result, err := c.service.FindAll(page, limit) if err != nil { ctx.JSON(http.StatusInternalServerError, dtos.ErrorResponse{Error: err.Error()}) return } ctx.JSON(http.StatusOK, result) }

REST Endpoints

The generated controller handles these endpoints (when paired with generated routes):

MethodPathHandlerDescription
POST/api/v1/productsCreateCreate a new product
GET/api/v1/productsFindAllList all products (paginated)
GET/api/v1/products/:idFindByIDGet a single product
PUT/api/v1/products/:idUpdateUpdate a product
DELETE/api/v1/products/:idDeleteDelete a product

Architecture

The controller is the outermost layer in the application:

HTTP Request --> Controller --> Service --> Repository --> Database
  • Controllers handle HTTP request/response concerns only
  • Request body parsing and validation happen in the controller
  • All business logic is delegated to the service layer
  • Swagger annotations enable automatic API documentation
Last updated on