2026-02-04 16:07:36 +00:00
|
|
|
package rmo
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
2026-02-05 01:35:00 +00:00
|
|
|
"strings"
|
2026-02-04 16:07:36 +00:00
|
|
|
|
|
|
|
|
//"github.com/Gleipnir-Technology/nidus-sync/config"
|
2026-03-18 15:36:20 +00:00
|
|
|
"github.com/Gleipnir-Technology/bob"
|
|
|
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
|
|
|
|
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
|
2026-02-04 16:07:36 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
2026-04-01 16:57:33 +00:00
|
|
|
//"github.com/gorilla/mux"
|
2026-03-18 15:36:20 +00:00
|
|
|
"github.com/stephenafamo/scan"
|
2026-02-04 16:07:36 +00:00
|
|
|
//"github.com/rs/zerolog/log"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ReportSuggestion struct {
|
2026-03-18 15:36:20 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
|
//Type string `json:"type"`
|
2026-02-04 16:07:36 +00:00
|
|
|
//Location string
|
|
|
|
|
}
|
|
|
|
|
type ReportSuggestionResponse struct {
|
|
|
|
|
Reports []ReportSuggestion `json:"reports"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getReportSuggestion(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
partial_report_id := r.FormValue("r")
|
|
|
|
|
if partial_report_id == "" {
|
|
|
|
|
respondError(w, "You need at least a bit of an 'r'", nil, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-02-06 16:56:26 +00:00
|
|
|
p := partialSearchParam(partial_report_id)
|
2026-02-04 16:07:36 +00:00
|
|
|
ctx := r.Context()
|
2026-03-18 15:36:20 +00:00
|
|
|
/*
|
|
|
|
|
rows, err := sql.PublicreportPublicIDSuggestion(p).All(ctx, db.PGInstance.BobDB)
|
|
|
|
|
if err != nil {
|
|
|
|
|
respondError(w, "Failed to query DB: %w", err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
type _Row struct {
|
|
|
|
|
Location string `db:"location"`
|
|
|
|
|
PublicID string `db:"public_id"`
|
2026-02-04 16:07:36 +00:00
|
|
|
}
|
2026-03-18 15:36:20 +00:00
|
|
|
rows, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select(
|
|
|
|
|
sm.Columns("public_id", "location"),
|
|
|
|
|
sm.From("publicreport.report"),
|
|
|
|
|
sm.Where(
|
|
|
|
|
psql.Quote("public_id").Like(psql.Arg(p)),
|
|
|
|
|
),
|
|
|
|
|
), scan.StructMapper[_Row]())
|
|
|
|
|
|
2026-02-04 16:07:36 +00:00
|
|
|
var result ReportSuggestionResponse
|
|
|
|
|
for _, row := range rows {
|
|
|
|
|
/*
|
|
|
|
|
value, err := row.Location.Value()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warn().Err(err).Msg("Failed to get value")
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
value_str, ok := value.(string)
|
|
|
|
|
if !ok {
|
|
|
|
|
log.Warn().Msg("Failed to get location as string")
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
log.Debug().Str("location", value_str).Msg("Looking at row")
|
|
|
|
|
*/
|
|
|
|
|
result.Reports = append(result.Reports, ReportSuggestion{
|
2026-03-18 15:36:20 +00:00
|
|
|
//Type: row.TableName,
|
|
|
|
|
ID: row.PublicID,
|
2026-02-04 16:07:36 +00:00
|
|
|
//Location: "",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
jsonBody, err := json.Marshal(result)
|
|
|
|
|
if err != nil {
|
|
|
|
|
respondError(w, "Failed to marshal JSON: %w", err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
w.Write(jsonBody)
|
|
|
|
|
}
|
2026-02-05 21:43:29 +00:00
|
|
|
|
2026-02-06 16:56:26 +00:00
|
|
|
func partialSearchParam(p string) string {
|
|
|
|
|
result := strings.ReplaceAll(p, "-", "")
|
|
|
|
|
result = strings.ToUpper(result)
|
|
|
|
|
return result + "%"
|
|
|
|
|
}
|