From 2e5b89eb91f7ed80b1263c1c339937843b6ec3cc Mon Sep 17 00:00:00 2001 From: Yuefii Date: Wed, 28 Aug 2024 23:45:44 +0700 Subject: [PATCH] chore: adding api(get) regencies in examples --- examples/api-go/dtos/provinces_dto.go | 6 -- examples/api-go/dtos/response.go | 5 + examples/api-go/handlers/handler.go | 11 +++ examples/api-go/handlers/provinces_handler.go | 17 +--- examples/api-go/handlers/regencies_handler.go | 95 +++++++++++++++++++ examples/api-go/models/regencies.go | 7 ++ examples/api-go/routes/router.go | 1 + 7 files changed, 123 insertions(+), 19 deletions(-) delete mode 100644 examples/api-go/dtos/provinces_dto.go create mode 100644 examples/api-go/handlers/handler.go create mode 100644 examples/api-go/handlers/regencies_handler.go create mode 100644 examples/api-go/models/regencies.go diff --git a/examples/api-go/dtos/provinces_dto.go b/examples/api-go/dtos/provinces_dto.go deleted file mode 100644 index ebf8d27..0000000 --- a/examples/api-go/dtos/provinces_dto.go +++ /dev/null @@ -1,6 +0,0 @@ -package dtos - -type ProvinceDTO struct { - Code string `json:"code"` - Name string `json:"name"` -} diff --git a/examples/api-go/dtos/response.go b/examples/api-go/dtos/response.go index bee32c3..aa1a18b 100644 --- a/examples/api-go/dtos/response.go +++ b/examples/api-go/dtos/response.go @@ -1,5 +1,10 @@ package dtos +type ApiDTO struct { + Code string `json:"code"` + Name string `json:"name"` +} + type ResponseDTO struct { Pagination PaginationDTO `json:"pagination"` Data interface{} `json:"data"` diff --git a/examples/api-go/handlers/handler.go b/examples/api-go/handlers/handler.go new file mode 100644 index 0000000..bb53ae7 --- /dev/null +++ b/examples/api-go/handlers/handler.go @@ -0,0 +1,11 @@ +package handlers + +import "gorm.io/gorm" + +type Handler struct { + db *gorm.DB +} + +func NewHandler(db *gorm.DB) *Handler { + return &Handler{db} +} diff --git a/examples/api-go/handlers/provinces_handler.go b/examples/api-go/handlers/provinces_handler.go index 3fb1d55..8d2b30b 100644 --- a/examples/api-go/handlers/provinces_handler.go +++ b/examples/api-go/handlers/provinces_handler.go @@ -7,17 +7,8 @@ import ( "github.com/gin-gonic/gin" "github.com/yuefii/NusantaraKita/examples/api-go/dtos" "github.com/yuefii/NusantaraKita/examples/api-go/models" - "gorm.io/gorm" ) -type Handler struct { - db *gorm.DB -} - -func NewHandler(db *gorm.DB) *Handler { - return &Handler{db} -} - func (handler *Handler) GetProvinces(ctx *gin.Context) { var province []models.Provinces var totalItems int64 @@ -49,9 +40,9 @@ func (handler *Handler) GetProvinces(ctx *gin.Context) { } totalItems = int64(len(province)) - var provinceDTOs []dtos.ProvinceDTO + var provinceDTOs []dtos.ApiDTO for _, r := range province { - provinceDTOs = append(provinceDTOs, dtos.ProvinceDTO{ + provinceDTOs = append(provinceDTOs, dtos.ApiDTO{ Code: r.Code, Name: r.Name, }) @@ -76,9 +67,9 @@ func (handler *Handler) GetProvinces(ctx *gin.Context) { return } - var provinceDTOs []dtos.ProvinceDTO + var provinceDTOs []dtos.ApiDTO for _, response := range province { - provinceDTOs = append(provinceDTOs, dtos.ProvinceDTO{ + provinceDTOs = append(provinceDTOs, dtos.ApiDTO{ Code: response.Code, Name: response.Name, }) diff --git a/examples/api-go/handlers/regencies_handler.go b/examples/api-go/handlers/regencies_handler.go new file mode 100644 index 0000000..4065ee5 --- /dev/null +++ b/examples/api-go/handlers/regencies_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) GetRegencies(ctx *gin.Context) { + var regency []models.Regencies + 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(®ency) + if result.Error != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) + return + } + totalItems = int64(len(regency)) + + var regenciesDTOs []dtos.ApiDTO + for _, response := range regency { + regenciesDTOs = append(regenciesDTOs, dtos.ApiDTO{ + Code: response.Code, + Name: response.Name, + }) + } + + response := gin.H{ + "data": regenciesDTOs, + } + + ctx.JSON(http.StatusOK, response) + return + } + + result := handler.db.Model(&models.Regencies{}).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(®ency) + if result.Error != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) + return + } + + var regenciesDTOs []dtos.ApiDTO + for _, response := range regency { + regenciesDTOs = append(regenciesDTOs, dtos.ApiDTO{ + Code: response.Code, + Name: response.Name, + }) + } + + totalPages := int(totalItems) / perPage + if int(totalItems)%perPage != 0 { + totalPages++ + } + + response := dtos.ResponseDTO{ + Data: regenciesDTOs, + Pagination: dtos.PaginationDTO{ + CurrentPage: page, + PerPage: perPage, + TotalItems: int(totalItems), + TotalPages: totalPages, + }, + } + ctx.JSON(http.StatusOK, response) +} diff --git a/examples/api-go/models/regencies.go b/examples/api-go/models/regencies.go new file mode 100644 index 0000000..8286c41 --- /dev/null +++ b/examples/api-go/models/regencies.go @@ -0,0 +1,7 @@ +package models + +type Regencies struct { + Code string `gorm:"column:code"` + ProvinceCode string `gorm:"column:province_code"` + Name string `gorm:"column:name"` +} diff --git a/examples/api-go/routes/router.go b/examples/api-go/routes/router.go index e666aad..924c6d7 100644 --- a/examples/api-go/routes/router.go +++ b/examples/api-go/routes/router.go @@ -10,4 +10,5 @@ func SetupRoutes(router *gin.Engine, db *gorm.DB) { handler := handlers.NewHandler(db) router.GET("/api/provinces", handler.GetProvinces) + router.GET("/api/regencies", handler.GetRegencies) }