diff --git a/api/routes.go b/api/routes.go index 62de89f3..2241bb3d 100644 --- a/api/routes.go +++ b/api/routes.go @@ -84,6 +84,10 @@ func AddRoutes(r *mux.Router) { r.Handle("/publicreport/{id}", handlerJSON(publicreport.ByID)).Methods("GET").Name("publicreport.ByIDGet") r.Handle("/publicreport/{id}", handlerJSONPut(publicreport.Update)).Methods("PUT") r.Handle("/publicreport/{id}/image", handlerFormPost(publicreport.ImageCreate)).Methods("POST") + r.Handle("/publicreport/compliance/{id}", handlerJSON(publicreport.ByIDCompliance)).Methods("GET").Name("publicreport.compliance.ByIDGet") + r.Handle("/publicreport/nuisance/{id}", handlerJSON(publicreport.ByIDNuisance)).Methods("GET").Name("publicreport.nuisance.ByIDGet") + r.Handle("/publicreport/water/{id}", handlerJSON(publicreport.ByIDWater)).Methods("GET").Name("publicreport.water.ByIDGet") + publicreport_notification := resource.PublicreportNotification(router) r.Handle("/publicreport-notification", handlerJSONPost(publicreport_notification.Create)).Methods("POST") diff --git a/platform/publicreport.go b/platform/publicreport.go index 79f64bb4..9b55cb3a 100644 --- a/platform/publicreport.go +++ b/platform/publicreport.go @@ -25,7 +25,7 @@ import ( "github.com/rs/zerolog/log" ) -func PublicreportByID(ctx context.Context, report_id string) (*types.Report, error) { +func PublicreportByID(ctx context.Context, report_id string) (*types.PublicReport, error) { return publicreport.Report(ctx, report_id) } func PublicreportInvalid(ctx context.Context, user User, report_id string) error { @@ -87,7 +87,7 @@ func PublicReportMessageCreate(ctx context.Context, user User, report_id, messag return nil, errors.New("no contact methods available") } } -func PublicReportUpdate(ctx context.Context, report_id string, report_setter models.PublicreportReportSetter, address *types.Address, location *types.Location) (*types.Report, error) { +func PublicReportUpdate(ctx context.Context, report_id string, report_setter models.PublicreportReportSetter, address *types.Address, location *types.Location) (*types.PublicReport, error) { txn, err := db.PGInstance.BobDB.BeginTx(ctx, nil) if err != nil { return nil, fmt.Errorf("create txn: %w", err) diff --git a/platform/publicreport/report.go b/platform/publicreport/report.go index ace9fc18..0a9740ff 100644 --- a/platform/publicreport/report.go +++ b/platform/publicreport/report.go @@ -8,16 +8,14 @@ import ( "github.com/Gleipnir-Technology/bob/dialect/psql" "github.com/Gleipnir-Technology/bob/dialect/psql/dialect" "github.com/Gleipnir-Technology/bob/dialect/psql/sm" - //"github.com/Gleipnir-Technology/nidus-sync/config" "github.com/Gleipnir-Technology/nidus-sync/db" - //"github.com/Gleipnir-Technology/nidus-sync/db/models" "github.com/Gleipnir-Technology/nidus-sync/platform/types" //"github.com/google/uuid" "github.com/rs/zerolog/log" "github.com/stephenafamo/scan" ) -func ReportsForOrganization(ctx context.Context, org_id int32) ([]*types.Report, error) { +func ReportsForOrganization(ctx context.Context, org_id int32) ([]*types.PublicReport, error) { query := reportQuery() query.Apply( sm.Where(psql.Quote("publicreport", "report", "organization_id").EQ(psql.Arg(org_id))), @@ -25,8 +23,8 @@ func ReportsForOrganization(ctx context.Context, org_id int32) ([]*types.Report, ) return reportQueryToRows(ctx, query) } -func reportQueryToRows(ctx context.Context, query bob.BaseQuery[*dialect.SelectQuery]) ([]*types.Report, error) { - rows, err := bob.All(ctx, db.PGInstance.BobDB, query, scan.StructMapper[types.Report]()) +func reportQueryToRows(ctx context.Context, query bob.BaseQuery[*dialect.SelectQuery]) ([]*types.PublicReport, error) { + rows, err := bob.All(ctx, db.PGInstance.BobDB, query, scan.StructMapper[types.PublicReport]()) if err != nil { return nil, fmt.Errorf("get reports: %w", err) @@ -51,16 +49,8 @@ func reportQueryToRows(ctx context.Context, query bob.BaseQuery[*dialect.SelectQ if err != nil { return nil, fmt.Errorf("log entries for reports: %w", err) } - nuisances_by_report_id, err := nuisancesByReportID(ctx, report_ids) - if err != nil { - return nil, fmt.Errorf("nuisances: %w", err) - } - waters_by_report_id, err := watersByReportID(ctx, report_ids) - if err != nil { - return nil, fmt.Errorf("waters: %w", err) - } - results := make([]*types.Report, len(rows)) + results := make([]*types.PublicReport, len(rows)) for i, row := range rows { if row.Address.ID != nil { address, ok := addresses_by_id[*row.Address.ID] @@ -77,8 +67,6 @@ func reportQueryToRows(ctx context.Context, query bob.BaseQuery[*dialect.SelectQ row.Images = []types.Image{} } row.Log = logs_by_report_id[row.ID] - row.Nuisance = nuisances_by_report_id[row.ID] - row.Water = waters_by_report_id[row.ID] if row.Location.Latitude == 0.0 || row.Location.Longitude == 0.0 { row.Location = nil } @@ -86,7 +74,7 @@ func reportQueryToRows(ctx context.Context, query bob.BaseQuery[*dialect.SelectQ } return results, nil } -func Report(ctx context.Context, public_id string) (*types.Report, error) { +func Report(ctx context.Context, public_id string) (*types.PublicReport, error) { query := reportQuery() query.Apply( sm.Where(psql.Quote("publicreport", "report", "public_id").EQ(psql.Arg(public_id))), @@ -100,7 +88,7 @@ func Report(ctx context.Context, public_id string) (*types.Report, error) { } return reports[0], nil } -func Reports(ctx context.Context, org_id int32, ids []int32) ([]*types.Report, error) { +func Reports(ctx context.Context, org_id int32, ids []int32) ([]*types.PublicReport, error) { query := reportQuery() query.Apply( sm.Where(psql.Quote("publicreport", "report", "organization_id").EQ(psql.Arg(org_id))), diff --git a/platform/signal.go b/platform/signal.go index b82a58e0..5a0d1aa0 100644 --- a/platform/signal.go +++ b/platform/signal.go @@ -26,17 +26,17 @@ import ( ) type Signal struct { - Address *types.Address `db:"address" json:"address"` - Addressed *time.Time `db:"addressed" json:"addressed"` - Addressor *int32 `db:"addressor" json:"addressor"` - Created time.Time `db:"created" json:"created"` - Creator int32 `db:"creator" json:"creator"` - ID int32 `db:"id" json:"id"` - Location types.Location `db:"location" json:"location"` - Pool *Pool `db:"pool" json:"pool"` - Report *types.Report `db:"report" json:"report"` - Species *string `db:"species" json:"species"` - Type string `db:"type" json:"type"` + Address *types.Address `db:"address" json:"address"` + Addressed *time.Time `db:"addressed" json:"addressed"` + Addressor *int32 `db:"addressor" json:"addressor"` + Created time.Time `db:"created" json:"created"` + Creator int32 `db:"creator" json:"creator"` + ID int32 `db:"id" json:"id"` + Location types.Location `db:"location" json:"location"` + Pool *Pool `db:"pool" json:"pool"` + Report *types.PublicReport `db:"report" json:"report"` + Species *string `db:"species" json:"species"` + Type string `db:"type" json:"type"` } // Create a lead from the given signal and site @@ -212,7 +212,7 @@ func SignalList(ctx context.Context, user User, limit int) ([]*Signal, error) { pool_map[pool.ID] = pool log.Debug().Int32("pool", pool.ID).Msg("Added to map") } - report_map := make(map[int32]*types.Report, len(report_ids)) + report_map := make(map[int32]*types.PublicReport, len(report_ids)) for _, report := range reports { report_map[report.ID] = report } diff --git a/platform/types/public_report.go b/platform/types/public_report.go deleted file mode 100644 index 74955cb9..00000000 --- a/platform/types/public_report.go +++ /dev/null @@ -1,4 +0,0 @@ -package types - -type PublicReport interface { -} diff --git a/platform/types/report.go b/platform/types/publicreport.go similarity index 84% rename from platform/types/report.go rename to platform/types/publicreport.go index a065dce9..60dcbd46 100644 --- a/platform/types/report.go +++ b/platform/types/publicreport.go @@ -4,14 +4,13 @@ import ( "time" ) -type Report struct { +type PublicReport struct { Address Address `db:"address" json:"address"` Created time.Time `db:"created" json:"created"` ID int32 `db:"id" json:"-"` Images []Image `db:"images" json:"images"` Location *Location `db:"location" json:"location"` Log []LogEntry `db:"-" json:"log"` - Nuisance *Nuisance `db:"nuisance" json:"nuisance"` DistrictID *int32 `db:"organization_id" json:"-"` District *string `db:"-" json:"district"` PublicID string `db:"public_id" json:"public_id"` @@ -19,5 +18,4 @@ type Report struct { Status string `db:"status" json:"status"` Type string `db:"report_type" json:"type"` URI string `db:"-" json:"uri"` - Water *Water `db:"water" json:"water"` } diff --git a/resource/publicreport.go b/resource/publicreport.go index 8a3a7472..9a5c4507 100644 --- a/resource/publicreport.go +++ b/resource/publicreport.go @@ -2,6 +2,7 @@ package resource import ( "context" + "fmt" "net/http" "github.com/aarondl/opt/omit" @@ -25,7 +26,7 @@ func Publicreport(r *router) *publicreportR { } } -func (res *publicreportR) ByID(ctx context.Context, r *http.Request, query QueryParams) (*types.Report, *nhttp.ErrorWithStatus) { +func (res *publicreportR) ByID(ctx context.Context, r *http.Request, query QueryParams) (*types.PublicReport, *nhttp.ErrorWithStatus) { vars := mux.Vars(r) public_id := vars["id"] if public_id == "" { @@ -42,12 +43,8 @@ func (res *publicreportR) ByID(ctx context.Context, r *http.Request, query Query return nil, nhttp.NewError("district uri: %w", err) } } - uri, err := res.router.IDStrToURI("publicreport.ByIDGet", report.PublicID) - if err != nil { - return nil, nhttp.NewError("uri: %w", err) - } + populateReportURI(report, res.router) report.District = &district_uri - report.URI = uri return report, nil } @@ -86,14 +83,7 @@ type publicreportForm struct { Reporter *types.Contact `schema:"reporter"` } -func (res *publicreportR) Update(ctx context.Context, r *http.Request, prf publicreportForm) (*types.Report, *nhttp.ErrorWithStatus) { - /* - uploads, err := html.ExtractImageUploads(r) - log.Info().Int("len", len(uploads)).Msg("extracted compliance uploads") - if err != nil { - return nil, nhttp.NewError("Failed to extract image uploads: %w", err) - } - */ +func (res *publicreportR) Update(ctx context.Context, r *http.Request, prf publicreportForm) (*types.PublicReport, *nhttp.ErrorWithStatus) { vars := mux.Vars(r) public_id := vars["id"] if public_id == "" { @@ -124,3 +114,23 @@ func (res *publicreportR) Update(ctx context.Context, r *http.Request, prf publi } return report, nil } + +func populateReportURI(report *types.PublicReport, r *router) error { + var route_name string + switch report.Type { + case "compliance": + route_name = "publicreport.ByIDGet" + case "nuisance": + route_name = "publicreport.nuisance.ByIDGet" + case "water": + route_name = "publicreport.water.ByIDGet" + default: + return fmt.Errorf("Unrecognized report type '%s'", report.Type) + } + uri, err := r.IDStrToURI(route_name, report.PublicID) + if err != nil { + return nhttp.NewError("uri: %w", err) + } + report.URI = uri + return nil +}