diff --git a/examples/api-go/handlers/districts_handler.go b/examples/api-go/handlers/districts_handler.go new file mode 100644 index 0000000..1e196a0 --- /dev/null +++ b/examples/api-go/handlers/districts_handler.go @@ -0,0 +1,95 @@ +package handlers + +import ( + "net/http" + "strconv" + + "github.com/gin-gonic/gin" + "github.com/yuefii/NusantaraKita/examples/api-go/dtos" + "github.com/yuefii/NusantaraKita/examples/api-go/models" +) + +func (handler *Handler) GetDistricts(ctx *gin.Context) { + var district []models.Districts + var totalItems int64 + + showAll := ctx.Query("show_all") == "true" + pageStr := ctx.Query("page") + perPageStr := ctx.Query("per_page") + + page := 1 + perPage := 10 + + if pageStr != "" { + if p, err := strconv.Atoi(pageStr); err == nil && p > 0 { + page = p + } + } + + if perPageStr != "" { + if pp, err := strconv.Atoi(perPageStr); err == nil && pp > 0 { + perPage = pp + } + } + + if showAll { + + result := handler.db.Find(&district) + if result.Error != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) + return + } + totalItems = int64(len(district)) + + var districtDTOs []dtos.ApiDTO + for _, response := range district { + districtDTOs = append(districtDTOs, dtos.ApiDTO{ + Code: response.Code, + Name: response.Name, + }) + } + + response := gin.H{ + "data": districtDTOs, + } + + ctx.JSON(http.StatusOK, response) + return + } + + result := handler.db.Model(&models.Districts{}).Count(&totalItems) + if result.Error != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) + return + } + + result = handler.db.Offset((page - 1) * perPage).Limit(perPage).Find(&district) + if result.Error != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) + return + } + + var districtDTOs []dtos.ApiDTO + for _, response := range district { + districtDTOs = append(districtDTOs, dtos.ApiDTO{ + Code: response.Code, + Name: response.Name, + }) + } + + totalPages := int(totalItems) / perPage + if int(totalItems)%perPage != 0 { + totalPages++ + } + + response := dtos.ResponseDTO{ + Data: districtDTOs, + Pagination: dtos.PaginationDTO{ + CurrentPage: page, + PerPage: perPage, + TotalItems: int(totalItems), + TotalPages: totalPages, + }, + } + ctx.JSON(http.StatusOK, response) +} diff --git a/examples/api-go/models/districts.go b/examples/api-go/models/districts.go new file mode 100644 index 0000000..826e167 --- /dev/null +++ b/examples/api-go/models/districts.go @@ -0,0 +1,7 @@ +package models + +type Districts struct { + Code string `gorm:"column:code"` + RegencyCode string `gorm:"column:regency_code"` + Name string `gorm:"column:name"` +} diff --git a/examples/api-go/routes/router.go b/examples/api-go/routes/router.go index 924c6d7..67c0bc2 100644 --- a/examples/api-go/routes/router.go +++ b/examples/api-go/routes/router.go @@ -11,4 +11,5 @@ func SetupRoutes(router *gin.Engine, db *gorm.DB) { router.GET("/api/provinces", handler.GetProvinces) router.GET("/api/regencies", handler.GetRegencies) + router.GET("api/districts", handler.GetDistricts) }