feat: add search and pagination to tariff period services
This commit is contained in:
118
internal/http/periode_tarif_detail.go
Normal file
118
internal/http/periode_tarif_detail.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"primaya-api/cpone-middleware/internal/repository"
|
||||
)
|
||||
|
||||
const periodeTarifLayananPerPage = 20
|
||||
|
||||
type PeriodeTarifLayananPaginationResponse struct {
|
||||
CurrentPage int `json:"current_page"`
|
||||
Data []repository.PeriodeTarifLayanan `json:"data"`
|
||||
FirstPageURL string `json:"first_page_url"`
|
||||
From *int `json:"from"`
|
||||
LastPage int `json:"last_page"`
|
||||
LastPageURL string `json:"last_page_url"`
|
||||
Links map[string]any `json:"links"`
|
||||
NextPageURL *string `json:"next_page_url"`
|
||||
Total int `json:"total"`
|
||||
PerPage int `json:"per_page"`
|
||||
}
|
||||
|
||||
func (h Handler) ListPeriodeTarifLayanan(w http.ResponseWriter, r *http.Request) {
|
||||
rawID := strings.TrimSpace(r.PathValue("periodeTarifID"))
|
||||
if rawID == "" {
|
||||
rawID = strings.TrimSpace(r.PathValue("PeriodeTarifID"))
|
||||
}
|
||||
periodeTarifID, err := strconv.ParseInt(rawID, 10, 64)
|
||||
if err != nil || periodeTarifID < 1 {
|
||||
WriteJSON(w, http.StatusUnprocessableEntity, Response{
|
||||
Success: false,
|
||||
Message: "PeriodeTarifID harus berupa angka positif",
|
||||
Data: nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
pageNumber := periodeTarifLayananPageNumber(r)
|
||||
search := strings.TrimSpace(r.URL.Query().Get("search"))
|
||||
page, err := h.periodeTarifDetailRepository(r.Context()).ListPeriodeTarifLayanan(r.Context(), repository.PeriodeTarifLayananListParams{
|
||||
PeriodeTarifID: periodeTarifID,
|
||||
Page: pageNumber,
|
||||
PerPage: periodeTarifLayananPerPage,
|
||||
Search: search,
|
||||
})
|
||||
if errors.Is(err, repository.ErrTarifTableNotFound) {
|
||||
WriteJSON(w, http.StatusNotFound, Response{
|
||||
Success: false,
|
||||
Message: "Tabel tarif tidak ditemukan: harga_layanan_" + strconv.FormatInt(periodeTarifID, 10),
|
||||
Data: nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
WriteJSON(w, http.StatusInternalServerError, Response{
|
||||
Success: false,
|
||||
Message: "internal server error",
|
||||
Data: nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if page.Items == nil {
|
||||
page.Items = []repository.PeriodeTarifLayanan{}
|
||||
}
|
||||
|
||||
lastPage := (page.Total + periodeTarifLayananPerPage - 1) / periodeTarifLayananPerPage
|
||||
if lastPage < 1 {
|
||||
lastPage = 1
|
||||
}
|
||||
var from *int
|
||||
if len(page.Items) > 0 {
|
||||
value := (pageNumber-1)*periodeTarifLayananPerPage + 1
|
||||
from = &value
|
||||
}
|
||||
var nextPageURL *string
|
||||
if pageNumber < lastPage {
|
||||
value := periodeTarifLayananPageURL(r, pageNumber+1)
|
||||
nextPageURL = &value
|
||||
}
|
||||
|
||||
WriteJSON(w, http.StatusOK, PeriodeTarifLayananPaginationResponse{
|
||||
CurrentPage: pageNumber, Data: page.Items, FirstPageURL: "", From: from,
|
||||
LastPage: lastPage, LastPageURL: "", Links: map[string]any{},
|
||||
NextPageURL: nextPageURL, Total: page.Total, PerPage: periodeTarifLayananPerPage,
|
||||
})
|
||||
}
|
||||
|
||||
func periodeTarifLayananPageNumber(r *http.Request) int {
|
||||
page, err := strconv.Atoi(r.URL.Query().Get("page"))
|
||||
if err != nil || page < 1 {
|
||||
return 1
|
||||
}
|
||||
return page
|
||||
}
|
||||
|
||||
func periodeTarifLayananPageURL(r *http.Request, page int) string {
|
||||
scheme := "http"
|
||||
if r.TLS != nil {
|
||||
scheme = "https"
|
||||
}
|
||||
if forwarded := r.Header.Get("X-Forwarded-Proto"); forwarded != "" {
|
||||
scheme = strings.TrimSpace(strings.Split(forwarded, ",")[0])
|
||||
}
|
||||
host := r.Host
|
||||
if host == "" {
|
||||
host = "localhost"
|
||||
}
|
||||
query := r.URL.Query()
|
||||
query.Set("page", strconv.Itoa(page))
|
||||
query.Set("per_page", strconv.Itoa(periodeTarifLayananPerPage))
|
||||
return (&url.URL{Scheme: scheme, Host: host, Path: r.URL.Path, RawQuery: query.Encode()}).String()
|
||||
}
|
||||
Reference in New Issue
Block a user