Skip to content

Commit

Permalink
chore: adding api(get) districs in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuefii committed Aug 29, 2024
1 parent b57816b commit b84fa44
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
95 changes: 95 additions & 0 deletions examples/api-go/handlers/districts_handler.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 7 additions & 0 deletions examples/api-go/models/districts.go
Original file line number Diff line number Diff line change
@@ -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"`
}
1 change: 1 addition & 0 deletions examples/api-go/routes/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit b84fa44

Please sign in to comment.