Types
The types package provides shared DTO (Data Transfer Object) types used across Gofasta packages for pagination, sorting, API responses, and error reporting.
Import
import "github.com/gofastadev/gofasta/pkg/types"Key Types
TPaginationInputDto
Input type for pagination parameters.
type TPaginationInputDto struct {
Limit *int `json:"limit,omitempty" schema:"limit" validate:"gte=1"`
Page *int `json:"page,omitempty" schema:"page" validate:"gte=1"`
}TSortingInputDto
Input type for sorting parameters.
type TSortingInputDto struct {
SortByField string `json:"sortByField" schema:"sortByField" validate:"required"`
SortOrientation *SortOrientation `json:"sortOrientation,omitempty" schema:"sortOrientation" validate:"omitempty"`
}SortOrientation
type SortOrientation string
const (
SortOrientationAsc SortOrientation = "ASC"
SortOrientationDesc SortOrientation = "DESC"
)TCommonAPIErrorDto
Standard error structure returned in API responses.
type TCommonAPIErrorDto struct {
FieldName *string `json:"fieldName,omitempty"`
Message string `json:"message"`
}TCommonResponseDto
Standard API response wrapper.
type TCommonResponseDto struct {
Status int `json:"status"`
Message *string `json:"message,omitempty"`
Errors []*TCommonAPIErrorDto `json:"errors,omitempty"`
}TPaginationObjectDto
Pagination metadata returned in list responses.
type TPaginationObjectDto struct {
TotalRecords *int `json:"totalRecords,omitempty"`
RecordsPerPage *int `json:"recordsPerPage,omitempty"`
TotalPages *int `json:"totalPages,omitempty"`
CurrentPage *int `json:"currentPage,omitempty"`
}Usage
Pagination in Controllers
func (c *UserController) List(w http.ResponseWriter, r *http.Request) {
var pagination types.TPaginationInputDto
if limitStr := r.URL.Query().Get("limit"); limitStr != "" {
limit, _ := strconv.Atoi(limitStr)
pagination.Limit = &limit
}
if pageStr := r.URL.Query().Get("page"); pageStr != "" {
page, _ := strconv.Atoi(pageStr)
pagination.Page = &page
}
users, total, err := c.service.List(r.Context(), &pagination)
if err != nil {
httputil.Error(w, err)
return
}
// Build pagination metadata
paginationObj := types.TPaginationObjectDto{
TotalRecords: &total,
CurrentPage: pagination.Page,
}
httputil.JSON(w, http.StatusOK, map[string]interface{}{
"data": users,
"pagination": paginationObj,
})
}Error Responses
func handleValidationErrors(w http.ResponseWriter, errs []*types.TCommonAPIErrorDto) {
msg := "Validation failed"
httputil.JSON(w, http.StatusBadRequest, types.TCommonResponseDto{
Status: http.StatusBadRequest,
Message: &msg,
Errors: errs,
})
}Sorting
sorting := types.TSortingInputDto{
SortByField: "createdAt",
SortOrientation: &types.SortOrientationDesc,
}SortOrientation implements JSON and GQL marshaling/unmarshaling, making it compatible with both REST and GraphQL APIs.
Related Pages
- Validators — ValidateStruct returns []*TCommonAPIErrorDto
- HTTP Utilities — Pagination helpers complement these types
- Utils — Utility functions for working with these types
Last updated on