Skip to content

Commit

Permalink
chore: adding api(get) villages in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuefii committed Aug 29, 2024
1 parent b84fa44 commit eb388d9
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
95 changes: 95 additions & 0 deletions examples/api-go/handlers/villages_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) GetVillages(ctx *gin.Context) {
var village []models.Villages
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(&village)
if result.Error != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()})
return
}
totalItems = int64(len(village))

var villageDtos []dtos.ApiDTO
for _, response := range village {
villageDtos = append(villageDtos, dtos.ApiDTO{
Code: response.Code,
Name: response.Name,
})
}

response := gin.H{
"data": villageDtos,
}

ctx.JSON(http.StatusOK, response)
return
}

result := handler.db.Model(&models.Villages{}).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(&village)
if result.Error != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()})
return
}

var villageDTOs []dtos.ApiDTO
for _, response := range village {
villageDTOs = append(villageDTOs, dtos.ApiDTO{
Code: response.Code,
Name: response.Name,
})
}

totalPages := int(totalItems) / perPage
if int(totalItems)%perPage != 0 {
totalPages++
}

response := dtos.ResponseDTO{
Data: villageDTOs,
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/villages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package models

type Villages struct {
Code string `gorm:"column:code"`
VillageCode string `gorm:"column:village_code"`
Name string `gorm:"column:name"`
}
3 changes: 2 additions & 1 deletion examples/api-go/routes/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ 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)
router.GET("/api/districts", handler.GetDistricts)
router.GET("/api/villages", handler.GetVillages)
}

0 comments on commit eb388d9

Please sign in to comment.