feat: require hospital code and add company APIs
This commit is contained in:
65
internal/http/layanan.go
Normal file
65
internal/http/layanan.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"primaya-api/cpone-middleware/internal/repository"
|
||||
)
|
||||
|
||||
const layananPerPage = 15
|
||||
|
||||
type LayananPaginationResponse struct {
|
||||
CurrentPage int `json:"current_page"`
|
||||
Data []repository.LayananDepartemen `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) ListLayanan(w http.ResponseWriter, r *http.Request) {
|
||||
pageNumber := layananPageNumber(r)
|
||||
page, err := h.layananRepository(r.Context()).ListLayanan(r.Context(), repository.LayananListParams{
|
||||
Page: pageNumber, PerPage: layananPerPage,
|
||||
})
|
||||
if err != nil {
|
||||
WriteJSON(w, http.StatusInternalServerError, Response{
|
||||
Success: false, Message: "internal server error", Data: nil,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
lastPage := (page.Total + layananPerPage - 1) / layananPerPage
|
||||
if lastPage < 1 {
|
||||
lastPage = 1
|
||||
}
|
||||
var from *int
|
||||
if len(page.Items) > 0 {
|
||||
value := (pageNumber-1)*layananPerPage + 1
|
||||
from = &value
|
||||
}
|
||||
var nextPageURL *string
|
||||
if pageNumber < lastPage {
|
||||
value := currentRequestURL(r, pageNumber+1)
|
||||
nextPageURL = &value
|
||||
}
|
||||
|
||||
WriteJSON(w, http.StatusOK, LayananPaginationResponse{
|
||||
CurrentPage: pageNumber, Data: page.Items, FirstPageURL: "", From: from,
|
||||
LastPage: lastPage, LastPageURL: "", Links: map[string]any{},
|
||||
NextPageURL: nextPageURL, Total: page.Total, PerPage: layananPerPage,
|
||||
})
|
||||
}
|
||||
|
||||
func layananPageNumber(r *http.Request) int {
|
||||
page, err := strconv.Atoi(r.URL.Query().Get("page"))
|
||||
if err != nil || page < 1 {
|
||||
return 1
|
||||
}
|
||||
return page
|
||||
}
|
||||
Reference in New Issue
Block a user