Skip to content

Commit

Permalink
chore: adding api(get) regencies in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuefii committed Aug 28, 2024
1 parent d67bea5 commit 2e5b89e
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 19 deletions.
6 changes: 0 additions & 6 deletions examples/api-go/dtos/provinces_dto.go

This file was deleted.

5 changes: 5 additions & 0 deletions examples/api-go/dtos/response.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand Down
11 changes: 11 additions & 0 deletions examples/api-go/handlers/handler.go
Original file line number Diff line number Diff line change
@@ -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}
}
17 changes: 4 additions & 13 deletions examples/api-go/handlers/provinces_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
})
Expand All @@ -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,
})
Expand Down
95 changes: 95 additions & 0 deletions examples/api-go/handlers/regencies_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) 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(&regency)
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(&regency)
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)
}
7 changes: 7 additions & 0 deletions examples/api-go/models/regencies.go
Original file line number Diff line number Diff line change
@@ -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"`
}
1 change: 1 addition & 0 deletions examples/api-go/routes/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 2e5b89e

Please sign in to comment.