Files
cpone_midleware/internal/http/laporan_mcu.go

73 lines
2.2 KiB
Go

package http
import (
"net/http"
"strings"
"time"
"primaya-api/cpone-middleware/internal/repository"
)
// ListLaporanMCU exposes the data formerly produced as mcu.xls by
// docs/lap_mcu.php. The API keeps the same lower-case date parameters as the
// PHP script and returns the spreadsheet rows as JSON data.
func (h Handler) ListLaporanMCU(w http.ResponseWriter, r *http.Request) {
tanggal, ok := firstQueryValue(r, "tanggal", "Tanggal")
if !ok || strings.TrimSpace(tanggal) == "" {
WriteJSON(w, http.StatusUnprocessableEntity, Response{
Success: false, Message: "Parameter tanggal wajib diisi", Data: nil,
})
return
}
tanggal2, ok := firstQueryValue(r, "tanggal2", "Tanggal2")
if !ok || strings.TrimSpace(tanggal2) == "" {
WriteJSON(w, http.StatusUnprocessableEntity, Response{
Success: false, Message: "Parameter tanggal2 wajib diisi", Data: nil,
})
return
}
start, err := parseReportDate(tanggal)
if err != nil {
WriteJSON(w, http.StatusUnprocessableEntity, Response{
Success: false, Message: "Parameter tanggal harus format Y-m-d", Data: nil,
})
return
}
end, err := parseReportDate(tanggal2)
if err != nil {
WriteJSON(w, http.StatusUnprocessableEntity, Response{
Success: false, Message: "Parameter tanggal2 harus format Y-m-d", Data: nil,
})
return
}
if end.Before(start) {
WriteJSON(w, http.StatusUnprocessableEntity, Response{
Success: false, Message: "Parameter tanggal2 tidak boleh lebih kecil dari tanggal", Data: nil,
})
return
}
rows, err := h.laporanMCURepository(r.Context()).ListLaporanMCU(r.Context(), repository.LaporanMCUParams{
Tanggal: start.Format("2006-01-02"), Tanggal2: end.Format("2006-01-02"),
})
if err != nil {
WriteJSON(w, http.StatusInternalServerError, Response{
Success: false, Message: "internal server error", Data: nil,
})
return
}
WriteJSON(w, http.StatusOK, Response{Success: true, Message: "ok", Data: rows})
}
// GetLaporanMCU is an explicit alias for callers that use the Get naming
// convention for report endpoints.
func (h Handler) GetLaporanMCU(w http.ResponseWriter, r *http.Request) {
h.ListLaporanMCU(w, r)
}
func parseReportDate(value string) (time.Time, error) {
return time.Parse("2006-01-02", strings.TrimSpace(value))
}