99 lines
3.1 KiB
Go
99 lines
3.1 KiB
Go
package http
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"primaya-api/cpone-middleware/internal/repository"
|
|
)
|
|
|
|
const perusahaanPerPage = 15
|
|
|
|
type PerusahaanPaginationResponse struct {
|
|
CurrentPage int `json:"current_page"`
|
|
Data []repository.PerusahaanSummary `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) ListPerusahaan(w http.ResponseWriter, r *http.Request) {
|
|
pageNumber := perusahaanPageNumber(r)
|
|
page, err := h.perusahaanRepository(r.Context()).ListPerusahaan(r.Context(), repository.PerusahaanListParams{
|
|
Search: strings.TrimSpace(r.URL.Query().Get("search")), Page: pageNumber, PerPage: perusahaanPerPage,
|
|
})
|
|
if err != nil {
|
|
writePerusahaanInternalError(w)
|
|
return
|
|
}
|
|
lastPage := (page.Total + perusahaanPerPage - 1) / perusahaanPerPage
|
|
if lastPage < 1 {
|
|
lastPage = 1
|
|
}
|
|
var from *int
|
|
if len(page.Items) > 0 {
|
|
value := (pageNumber-1)*perusahaanPerPage + 1
|
|
from = &value
|
|
}
|
|
var nextPageURL *string
|
|
if pageNumber < lastPage {
|
|
value := currentRequestURL(r, pageNumber+1)
|
|
nextPageURL = &value
|
|
}
|
|
WriteJSON(w, http.StatusOK, PerusahaanPaginationResponse{
|
|
CurrentPage: pageNumber, Data: page.Items, FirstPageURL: "", From: from,
|
|
LastPage: lastPage, LastPageURL: "", Links: map[string]any{}, NextPageURL: nextPageURL,
|
|
Total: page.Total, PerPage: perusahaanPerPage,
|
|
})
|
|
}
|
|
|
|
func (h Handler) GetPerusahaan(w http.ResponseWriter, r *http.Request) {
|
|
detail, err := h.perusahaanRepository(r.Context()).GetPerusahaan(r.Context(), strings.TrimSpace(r.PathValue("id")))
|
|
if err != nil {
|
|
writePerusahaanInternalError(w)
|
|
return
|
|
}
|
|
if detail == nil {
|
|
writePerusahaanNotFound(w)
|
|
return
|
|
}
|
|
WriteJSON(w, http.StatusOK, Response{Success: true, Message: "ok", Data: detail})
|
|
}
|
|
|
|
func (h Handler) GetPerusahaanGroupTarif(w http.ResponseWriter, r *http.Request) {
|
|
detail, err := h.perusahaanRepository(r.Context()).GetPerusahaanGroupTarif(
|
|
r.Context(), strings.TrimSpace(r.PathValue("id")), strings.TrimSpace(r.URL.Query().Get("group_tarif_id")),
|
|
)
|
|
if err != nil {
|
|
writePerusahaanInternalError(w)
|
|
return
|
|
}
|
|
if detail == nil {
|
|
writePerusahaanNotFound(w)
|
|
return
|
|
}
|
|
WriteJSON(w, http.StatusOK, Response{Success: true, Message: "ok", Data: detail})
|
|
}
|
|
|
|
func perusahaanPageNumber(r *http.Request) int {
|
|
page, err := strconv.Atoi(r.URL.Query().Get("page"))
|
|
if err != nil || page < 1 {
|
|
return 1
|
|
}
|
|
return page
|
|
}
|
|
|
|
func writePerusahaanNotFound(w http.ResponseWriter) {
|
|
WriteJSON(w, http.StatusNotFound, Response{Success: false, Message: "Data tidak ditemukan", Data: nil})
|
|
}
|
|
|
|
func writePerusahaanInternalError(w http.ResponseWriter) {
|
|
WriteJSON(w, http.StatusInternalServerError, Response{Success: false, Message: "internal server error", Data: nil})
|
|
}
|