Initial commit
This commit is contained in:
74
cpone-dashboard/menu/result/handler.go
Normal file
74
cpone-dashboard/menu/result/handler.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package result
|
||||
|
||||
import (
|
||||
"cpone-dashboard/menu/auth"
|
||||
"cpone-dashboard/menu/projects"
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var tmpl *template.Template
|
||||
var pdfBaseURL string
|
||||
var basePath string
|
||||
|
||||
func SetTemplates(t *template.Template) { tmpl = t }
|
||||
func SetPDFBaseURL(u string) { pdfBaseURL = u }
|
||||
func SetBasePath(p string) { basePath = p }
|
||||
|
||||
type pageData struct {
|
||||
Username string
|
||||
CurrentProject projects.ProjectItem
|
||||
Search string
|
||||
Filter string
|
||||
Rows []ResultRow
|
||||
FilteredRows []ResultRow
|
||||
Summary ResultSummary
|
||||
PDFBaseURL string
|
||||
}
|
||||
|
||||
func Index(w http.ResponseWriter, r *http.Request) {
|
||||
username := auth.Username(r)
|
||||
mcuID := auth.SelectedProjectID(r)
|
||||
if mcuID == 0 {
|
||||
http.Redirect(w, r, basePath+"/projects", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
project, ok, err := projects.GetUserProject(username, mcuID)
|
||||
if err != nil {
|
||||
http.Error(w, "query error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if !ok {
|
||||
http.Redirect(w, r, basePath+"/projects", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
rows, err := GetResultRows(mcuID)
|
||||
if err != nil {
|
||||
http.Error(w, "query error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
summary := BuildResultSummary(rows)
|
||||
search := r.URL.Query().Get("search")
|
||||
filter := r.URL.Query().Get("filter")
|
||||
filteredRows := FilterResultRows(rows, search, filter)
|
||||
|
||||
t := tmpl
|
||||
if t == nil {
|
||||
http.Error(w, "template not ready", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if err := t.ExecuteTemplate(w, "base", pageData{
|
||||
Username: username,
|
||||
CurrentProject: project,
|
||||
Search: search,
|
||||
Filter: filter,
|
||||
Rows: rows,
|
||||
FilteredRows: filteredRows,
|
||||
Summary: summary,
|
||||
PDFBaseURL: pdfBaseURL,
|
||||
}); err != nil {
|
||||
http.Error(w, "template error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
101
cpone-dashboard/menu/result/query.go
Normal file
101
cpone-dashboard/menu/result/query.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package result
|
||||
|
||||
import (
|
||||
"cpone-dashboard/db"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ResultRow struct {
|
||||
NIP string
|
||||
Name string
|
||||
Posisi string
|
||||
FileUrl string
|
||||
ReportDate string
|
||||
}
|
||||
|
||||
type ResultSummary struct {
|
||||
Total int
|
||||
HasPDF int
|
||||
}
|
||||
|
||||
func GetResultRows(mcuID int) ([]ResultRow, error) {
|
||||
rows, err := db.DB.Query(`
|
||||
SELECT
|
||||
COALESCE(NULLIF(TRIM(mp.Mcu_PatientNIP), ''), '-') AS nip,
|
||||
COALESCE(NULLIF(TRIM(mp.Mcu_PatientName), ''), '-') AS name,
|
||||
COALESCE(
|
||||
NULLIF(TRIM(mp.Mcu_PatientDepartment), ''),
|
||||
NULLIF(TRIM(mp.Mcu_PatientDivision), ''),
|
||||
NULLIF(TRIM(mp.Mcu_PatientPosisi), ''),
|
||||
'-'
|
||||
) AS posisi,
|
||||
COALESCE(p.Published_McuDasboardFileUrl, '') AS file_url,
|
||||
CASE
|
||||
WHEN p.Published_McuDasboardFileUrl IS NOT NULL
|
||||
AND p.Published_McuDasboardFileUrl != ''
|
||||
THEN COALESCE(CAST(p.Published_McuDasboardLastUpdated AS CHAR), '')
|
||||
ELSE ''
|
||||
END AS report_date
|
||||
FROM mcu_patient mp
|
||||
LEFT JOIN published_mcu_dashboard_sync p
|
||||
ON p.Published_McuDasboardT_OrderHeaderID = mp.Mcu_PatientOrderID
|
||||
WHERE mp.Mcu_PatientMcuID = ?
|
||||
AND mp.Mcu_PatientIsActive = 'Y'
|
||||
ORDER BY
|
||||
(p.Published_McuDasboardFileUrl IS NOT NULL AND p.Published_McuDasboardFileUrl != '') DESC,
|
||||
mp.Mcu_PatientName ASC
|
||||
`, mcuID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var result []ResultRow
|
||||
for rows.Next() {
|
||||
var r ResultRow
|
||||
if err := rows.Scan(&r.NIP, &r.Name, &r.Posisi, &r.FileUrl, &r.ReportDate); err != nil {
|
||||
continue
|
||||
}
|
||||
result = append(result, r)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
func BuildResultSummary(rows []ResultRow) ResultSummary {
|
||||
s := ResultSummary{Total: len(rows)}
|
||||
for _, r := range rows {
|
||||
if r.FileUrl != "" {
|
||||
s.HasPDF++
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func FilterResultRows(rows []ResultRow, search, filter string) []ResultRow {
|
||||
search = strings.ToLower(strings.TrimSpace(search))
|
||||
filter = strings.TrimSpace(filter)
|
||||
if search == "" && filter == "" {
|
||||
return rows
|
||||
}
|
||||
out := make([]ResultRow, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
switch filter {
|
||||
case "has_pdf":
|
||||
if r.FileUrl == "" {
|
||||
continue
|
||||
}
|
||||
case "no_pdf":
|
||||
if r.FileUrl != "" {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if search != "" {
|
||||
hay := strings.ToLower(r.Name + " " + r.NIP + " " + r.Posisi)
|
||||
if !strings.Contains(hay, search) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
out = append(out, r)
|
||||
}
|
||||
return out
|
||||
}
|
||||
7
cpone-dashboard/menu/result/route.go
Normal file
7
cpone-dashboard/menu/result/route.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package result
|
||||
|
||||
import "github.com/go-chi/chi/v5"
|
||||
|
||||
func Routes(r chi.Router) {
|
||||
r.Get("/", Index)
|
||||
}
|
||||
Reference in New Issue
Block a user