-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: adding api(get) provinces in examples
- Loading branch information
Showing
8 changed files
with
146 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package dtos | ||
|
||
type PaginationDTO struct { | ||
CurrentPage int `json:"current_page"` | ||
PerPage int `json:"per_page"` | ||
TotalItems int `json:"total_items"` | ||
TotalPages int `json:"total_pages"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package dtos | ||
|
||
type ProvinceDTO struct { | ||
Code string `json:"code"` | ||
Name string `json:"name"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package dtos | ||
|
||
type ResponseDTO struct { | ||
Pagination PaginationDTO `json:"pagination"` | ||
Data interface{} `json:"data"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
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" | ||
"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 | ||
|
||
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(&province) | ||
if result.Error != nil { | ||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) | ||
return | ||
} | ||
totalItems = int64(len(province)) | ||
|
||
var provinceDTOs []dtos.ProvinceDTO | ||
for _, r := range province { | ||
provinceDTOs = append(provinceDTOs, dtos.ProvinceDTO{ | ||
Code: r.Code, | ||
Name: r.Name, | ||
}) | ||
} | ||
|
||
response := gin.H{ | ||
"data": provinceDTOs, | ||
} | ||
ctx.JSON(http.StatusOK, response) | ||
return | ||
} | ||
|
||
result := handler.db.Model(&models.Provinces{}).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(&province) | ||
if result.Error != nil { | ||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) | ||
return | ||
} | ||
|
||
var provinceDTOs []dtos.ProvinceDTO | ||
for _, response := range province { | ||
provinceDTOs = append(provinceDTOs, dtos.ProvinceDTO{ | ||
Code: response.Code, | ||
Name: response.Name, | ||
}) | ||
} | ||
|
||
totalPages := int(totalItems) / perPage | ||
if int(totalItems)%perPage != 0 { | ||
totalPages++ | ||
} | ||
|
||
response := dtos.ResponseDTO{ | ||
Data: provinceDTOs, | ||
Pagination: dtos.PaginationDTO{ | ||
CurrentPage: page, | ||
PerPage: perPage, | ||
TotalItems: int(totalItems), | ||
TotalPages: totalPages, | ||
}, | ||
} | ||
|
||
ctx.JSON(http.StatusOK, response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package models | ||
|
||
type Provinces struct { | ||
Code string | ||
Name string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package routes | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/yuefii/NusantaraKita/examples/api-go/handlers" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func SetupRoutes(router *gin.Engine, db *gorm.DB) { | ||
handler := handlers.NewHandler(db) | ||
|
||
router.GET("/api/provinces", handler.GetProvinces) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters