feat: improve layanan search and pagination
This commit is contained in:
@@ -3,11 +3,15 @@ package http
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"primaya-api/cpone-middleware/internal/repository"
|
"primaya-api/cpone-middleware/internal/repository"
|
||||||
)
|
)
|
||||||
|
|
||||||
const layananPerPage = 15
|
const (
|
||||||
|
defaultLayananPerPage = 15
|
||||||
|
maxLayananPerPage = 1000
|
||||||
|
)
|
||||||
|
|
||||||
type LayananPaginationResponse struct {
|
type LayananPaginationResponse struct {
|
||||||
CurrentPage int `json:"current_page"`
|
CurrentPage int `json:"current_page"`
|
||||||
@@ -24,8 +28,11 @@ type LayananPaginationResponse struct {
|
|||||||
|
|
||||||
func (h Handler) ListLayanan(w http.ResponseWriter, r *http.Request) {
|
func (h Handler) ListLayanan(w http.ResponseWriter, r *http.Request) {
|
||||||
pageNumber := layananPageNumber(r)
|
pageNumber := layananPageNumber(r)
|
||||||
|
perPage := layananPageSize(r)
|
||||||
|
search := strings.TrimSpace(r.URL.Query().Get("search"))
|
||||||
|
departemenID := strings.TrimSpace(r.URL.Query().Get("departemen_id"))
|
||||||
page, err := h.layananRepository(r.Context()).ListLayanan(r.Context(), repository.LayananListParams{
|
page, err := h.layananRepository(r.Context()).ListLayanan(r.Context(), repository.LayananListParams{
|
||||||
Page: pageNumber, PerPage: layananPerPage,
|
Page: pageNumber, PerPage: perPage, Search: search, DepartemenID: departemenID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
WriteJSON(w, http.StatusInternalServerError, Response{
|
WriteJSON(w, http.StatusInternalServerError, Response{
|
||||||
@@ -34,13 +41,13 @@ func (h Handler) ListLayanan(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
lastPage := (page.Total + layananPerPage - 1) / layananPerPage
|
lastPage := (page.Total + perPage - 1) / perPage
|
||||||
if lastPage < 1 {
|
if lastPage < 1 {
|
||||||
lastPage = 1
|
lastPage = 1
|
||||||
}
|
}
|
||||||
var from *int
|
var from *int
|
||||||
if len(page.Items) > 0 {
|
if len(page.Items) > 0 {
|
||||||
value := (pageNumber-1)*layananPerPage + 1
|
value := (pageNumber-1)*perPage + 1
|
||||||
from = &value
|
from = &value
|
||||||
}
|
}
|
||||||
var nextPageURL *string
|
var nextPageURL *string
|
||||||
@@ -52,10 +59,21 @@ func (h Handler) ListLayanan(w http.ResponseWriter, r *http.Request) {
|
|||||||
WriteJSON(w, http.StatusOK, LayananPaginationResponse{
|
WriteJSON(w, http.StatusOK, LayananPaginationResponse{
|
||||||
CurrentPage: pageNumber, Data: page.Items, FirstPageURL: "", From: from,
|
CurrentPage: pageNumber, Data: page.Items, FirstPageURL: "", From: from,
|
||||||
LastPage: lastPage, LastPageURL: "", Links: map[string]any{},
|
LastPage: lastPage, LastPageURL: "", Links: map[string]any{},
|
||||||
NextPageURL: nextPageURL, Total: page.Total, PerPage: layananPerPage,
|
NextPageURL: nextPageURL, Total: page.Total, PerPage: perPage,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func layananPageSize(r *http.Request) int {
|
||||||
|
perPage, err := strconv.Atoi(r.URL.Query().Get("per_page"))
|
||||||
|
if err != nil || perPage < 1 {
|
||||||
|
return defaultLayananPerPage
|
||||||
|
}
|
||||||
|
if perPage > maxLayananPerPage {
|
||||||
|
return maxLayananPerPage
|
||||||
|
}
|
||||||
|
return perPage
|
||||||
|
}
|
||||||
|
|
||||||
func layananPageNumber(r *http.Request) int {
|
func layananPageNumber(r *http.Request) int {
|
||||||
page, err := strconv.Atoi(r.URL.Query().Get("page"))
|
page, err := strconv.Atoi(r.URL.Query().Get("page"))
|
||||||
if err != nil || page < 1 {
|
if err != nil || page < 1 {
|
||||||
|
|||||||
@@ -28,29 +28,29 @@ func (f *fakeLayananRepository) MappingHarga(context.Context, repository.Mapping
|
|||||||
|
|
||||||
func TestListLayananResponseParity(t *testing.T) {
|
func TestListLayananResponseParity(t *testing.T) {
|
||||||
group, department := "MCU", "MEDICAL CHECK UP"
|
group, department := "MCU", "MEDICAL CHECK UP"
|
||||||
layananID, name, price := "6413", "DARAH LENGKAP", "125000.00"
|
layananID, code, name := "6413", "DL", "DARAH LENGKAP"
|
||||||
repo := &fakeLayananRepository{page: repository.LayananPage{
|
repo := &fakeLayananRepository{page: repository.LayananPage{
|
||||||
Total: 16,
|
Total: 16,
|
||||||
Items: []repository.LayananDepartemen{{
|
Items: []repository.LayananDepartemen{{
|
||||||
GroupLayananID: &group, Departemen: &department, LayananDepartemenID: 987,
|
GroupLayananID: &group, Departemen: &department, LayananDepartemenID: 987,
|
||||||
LayananID: &layananID, NamaLayanan: &name, Harga: &price,
|
LayananID: &layananID, LayananCode: &code, NamaLayanan: &name,
|
||||||
}},
|
}},
|
||||||
}}
|
}}
|
||||||
handler := Handler{layananRepo: repo}
|
handler := Handler{layananRepo: repo}
|
||||||
request := httptest.NewRequest(http.MethodGet, "http://localhost/api/cpone/layanan?page=1&kode_rs=AWALBROS_DEV", nil)
|
request := httptest.NewRequest(http.MethodGet, "http://localhost/api/cpone/layanan?page=1&search=tes&departemen_id=MCU&kode_rs=AWALBROS_DEV", nil)
|
||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
handler.ListLayanan(recorder, request)
|
handler.ListLayanan(recorder, request)
|
||||||
|
|
||||||
if recorder.Code != http.StatusOK {
|
if recorder.Code != http.StatusOK {
|
||||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||||
}
|
}
|
||||||
if repo.params.Page != 1 || repo.params.PerPage != 15 {
|
if repo.params.Page != 1 || repo.params.PerPage != defaultLayananPerPage || repo.params.Search != "tes" || repo.params.DepartemenID != "MCU" {
|
||||||
t.Fatalf("params = %+v", repo.params)
|
t.Fatalf("params = %+v", repo.params)
|
||||||
}
|
}
|
||||||
for _, expected := range []string{
|
for _, expected := range []string{
|
||||||
`"GroupLayananID":"MCU"`, `"Departemen":"MEDICAL CHECK UP"`,
|
`"GroupLayananID":"MCU"`, `"Departemen":"MEDICAL CHECK UP"`,
|
||||||
`"LayananDepartemenID":987`, `"LayananID":"6413"`,
|
`"LayananDepartemenID":987`, `"LayananID":"6413"`,
|
||||||
`"NamaLayanan":"DARAH LENGKAP"`, `"Harga":"125000.00"`,
|
`"NamaLayanan":"DARAH LENGKAP"`, `"LayananCode":"DL"`,
|
||||||
`"last_page":2`, `"per_page":15`,
|
`"last_page":2`, `"per_page":15`,
|
||||||
`"next_page_url":"http://localhost/api/cpone/layanan?kode_rs=AWALBROS_DEV\u0026page=2"`,
|
`"next_page_url":"http://localhost/api/cpone/layanan?kode_rs=AWALBROS_DEV\u0026page=2"`,
|
||||||
} {
|
} {
|
||||||
@@ -66,12 +66,27 @@ func TestListLayananUsesLaravelPageFallback(t *testing.T) {
|
|||||||
for _, target := range []string{"/api/cpone/layanan", "/api/cpone/layanan?page=0", "/api/cpone/layanan?page=invalid"} {
|
for _, target := range []string{"/api/cpone/layanan", "/api/cpone/layanan?page=0", "/api/cpone/layanan?page=invalid"} {
|
||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
handler.ListLayanan(recorder, httptest.NewRequest(http.MethodGet, target, nil))
|
handler.ListLayanan(recorder, httptest.NewRequest(http.MethodGet, target, nil))
|
||||||
if recorder.Code != http.StatusOK || repo.params.Page != 1 || repo.params.PerPage != layananPerPage {
|
if recorder.Code != http.StatusOK || repo.params.Page != 1 || repo.params.PerPage != defaultLayananPerPage {
|
||||||
t.Fatalf("target=%s status=%d params=%+v", target, recorder.Code, repo.params)
|
t.Fatalf("target=%s status=%d params=%+v", target, recorder.Code, repo.params)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestListLayananAcceptsBoundedPerPage(t *testing.T) {
|
||||||
|
repo := &fakeLayananRepository{page: repository.LayananPage{Items: []repository.LayananDepartemen{}}}
|
||||||
|
handler := Handler{layananRepo: repo}
|
||||||
|
for target, expected := range map[string]int{
|
||||||
|
"/api/cpone/layanan?per_page=250": 250,
|
||||||
|
"/api/cpone/layanan?per_page=5000": maxLayananPerPage,
|
||||||
|
} {
|
||||||
|
recorder := httptest.NewRecorder()
|
||||||
|
handler.ListLayanan(recorder, httptest.NewRequest(http.MethodGet, target, nil))
|
||||||
|
if recorder.Code != http.StatusOK || repo.params.PerPage != expected {
|
||||||
|
t.Fatalf("target=%s status=%d per_page=%d", target, recorder.Code, repo.params.PerPage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestListLayananInternalError(t *testing.T) {
|
func TestListLayananInternalError(t *testing.T) {
|
||||||
handler := Handler{layananRepo: &fakeLayananRepository{err: errors.New("query failed")}}
|
handler := Handler{layananRepo: &fakeLayananRepository{err: errors.New("query failed")}}
|
||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type LayananListParams struct {
|
type LayananListParams struct {
|
||||||
Page int
|
Page int
|
||||||
PerPage int
|
PerPage int
|
||||||
|
Search string
|
||||||
|
DepartemenID string
|
||||||
}
|
}
|
||||||
|
|
||||||
type LayananPage struct {
|
type LayananPage struct {
|
||||||
@@ -21,29 +23,34 @@ type LayananDepartemen struct {
|
|||||||
LayananDepartemenID int64 `json:"LayananDepartemenID"`
|
LayananDepartemenID int64 `json:"LayananDepartemenID"`
|
||||||
LayananID *string `json:"LayananID"`
|
LayananID *string `json:"LayananID"`
|
||||||
NamaLayanan *string `json:"NamaLayanan"`
|
NamaLayanan *string `json:"NamaLayanan"`
|
||||||
Harga *string `json:"Harga"`
|
LayananCode *string `json:"LayananCode"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r MySQLLayananRepository) ListLayanan(ctx context.Context, params LayananListParams) (LayananPage, error) {
|
func (r MySQLLayananRepository) ListLayanan(ctx context.Context, params LayananListParams) (LayananPage, error) {
|
||||||
var total int
|
var total int
|
||||||
if err := r.db.QueryRowContext(ctx, `
|
if err := r.db.QueryRowContext(ctx, `
|
||||||
SELECT COUNT(*)
|
SELECT COUNT(*)
|
||||||
FROM layanandepartemen
|
FROM layanandepartemen ld
|
||||||
WHERE NA = 'N' AND DepartemenID = 'MCU'
|
LEFT JOIN masterlayanan ml ON ml.LayananID = ld.LayananID
|
||||||
`).Scan(&total); err != nil {
|
WHERE ld.NA = 'N'
|
||||||
|
AND (? = '' OR ld.DepartemenID = ?)
|
||||||
|
AND (? = '' OR ld.LayananID LIKE ? OR ml.LayananKode LIKE ? OR ml.Nama LIKE ?)
|
||||||
|
`, params.DepartemenID, params.DepartemenID, params.Search, "%"+params.Search+"%", "%"+params.Search+"%", "%"+params.Search+"%").Scan(&total); err != nil {
|
||||||
return LayananPage{}, err
|
return LayananPage{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := r.db.QueryContext(ctx, `
|
rows, err := r.db.QueryContext(ctx, `
|
||||||
SELECT ld.GroupLayananID, d.Nama, ld.LayananDepartemenID,
|
SELECT ld.GroupLayananID, d.Nama, ld.LayananDepartemenID,
|
||||||
ld.LayananID, ml.Nama, CAST(ld.Harga AS CHAR)
|
ld.LayananID, ml.LayananKode, ml.Nama
|
||||||
FROM layanandepartemen ld
|
FROM layanandepartemen ld
|
||||||
LEFT JOIN departemen d ON d.DepartemenID = ld.DepartemenID
|
LEFT JOIN departemen d ON d.DepartemenID = ld.DepartemenID
|
||||||
LEFT JOIN masterlayanan ml ON ml.LayananID = ld.LayananID
|
LEFT JOIN masterlayanan ml ON ml.LayananID = ld.LayananID
|
||||||
WHERE ld.NA = 'N' AND ld.DepartemenID = 'MCU'
|
WHERE ld.NA = 'N'
|
||||||
|
AND (? = '' OR ld.DepartemenID = ?)
|
||||||
|
AND (? = '' OR ld.LayananID LIKE ? OR ml.LayananKode LIKE ? OR ml.Nama LIKE ?)
|
||||||
ORDER BY ld.TanggalBuat DESC
|
ORDER BY ld.TanggalBuat DESC
|
||||||
LIMIT ? OFFSET ?
|
LIMIT ? OFFSET ?
|
||||||
`, params.PerPage, (params.Page-1)*params.PerPage)
|
`, params.DepartemenID, params.DepartemenID, params.Search, "%"+params.Search+"%", "%"+params.Search+"%", "%"+params.Search+"%", params.PerPage, (params.Page-1)*params.PerPage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return LayananPage{}, err
|
return LayananPage{}, err
|
||||||
}
|
}
|
||||||
@@ -52,16 +59,16 @@ func (r MySQLLayananRepository) ListLayanan(ctx context.Context, params LayananL
|
|||||||
items := make([]LayananDepartemen, 0)
|
items := make([]LayananDepartemen, 0)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var item LayananDepartemen
|
var item LayananDepartemen
|
||||||
var groupLayananID, departemen, layananID, namaLayanan, harga sql.NullString
|
var groupLayananID, departemen, layananID, layananCode, namaLayanan sql.NullString
|
||||||
if err := rows.Scan(&groupLayananID, &departemen, &item.LayananDepartemenID,
|
if err := rows.Scan(&groupLayananID, &departemen, &item.LayananDepartemenID,
|
||||||
&layananID, &namaLayanan, &harga); err != nil {
|
&layananID, &layananCode, &namaLayanan); err != nil {
|
||||||
return LayananPage{}, err
|
return LayananPage{}, err
|
||||||
}
|
}
|
||||||
item.GroupLayananID = nullStringPtr(groupLayananID)
|
item.GroupLayananID = nullStringPtr(groupLayananID)
|
||||||
item.Departemen = nullStringPtr(departemen)
|
item.Departemen = nullStringPtr(departemen)
|
||||||
item.LayananID = nullStringPtr(layananID)
|
item.LayananID = nullStringPtr(layananID)
|
||||||
|
item.LayananCode = nullStringPtr(layananCode)
|
||||||
item.NamaLayanan = nullStringPtr(namaLayanan)
|
item.NamaLayanan = nullStringPtr(namaLayanan)
|
||||||
item.Harga = nullStringPtr(harga)
|
|
||||||
items = append(items, item)
|
items = append(items, item)
|
||||||
}
|
}
|
||||||
if err := rows.Err(); err != nil {
|
if err := rows.Err(); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user