Split apart comms logic into platform-lever funcs
This commit is contained in:
parent
2f7ecdfae8
commit
884634a2d7
9 changed files with 232 additions and 204 deletions
67
platform/publicreport/image.go
Normal file
67
platform/publicreport/image.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package publicreport
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/Gleipnir-Technology/bob"
|
||||
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
||||
"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"
|
||||
)
|
||||
|
||||
/*
|
||||
SELECT
|
||||
i.*,
|
||||
MAX(e.value) FILTER (WHERE e.name = 'Make') as exif_make,
|
||||
MAX(e.value) FILTER (WHERE e.name = 'Model') as exif_model,
|
||||
MAX(e.value) FILTER (WHERE e.name = 'DateTime') as exif_datetime,
|
||||
MAX(e.value) FILTER (WHERE e.name = 'GPSLatitude') as exif_gps_lat
|
||||
FROM publicreport.image i
|
||||
LEFT JOIN publicreport.image_exif e ON i.id = e.image_id
|
||||
WHERE i.id IN (1, 2, 3, 4)
|
||||
GROUP BY i.id;
|
||||
*/
|
||||
// Get all the images that belong to the list of report IDs
|
||||
func loadImagesForReportNuisance(ctx context.Context, org_id int32, report_ids []int32) (results map[int32][]types.Image, err error) {
|
||||
rows, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select(
|
||||
sm.Columns(
|
||||
"i.storage_uuid AS uuid",
|
||||
"ST_X(location) AS location.longitude",
|
||||
"ST_Y(location) AS location.latitude",
|
||||
"MAX(e.value) FILTER (WHERE e.name = 'Make') AS exif_make",
|
||||
"MAX(e.value) FILTER (WHERE e.name = 'Model') AS exif_model",
|
||||
"MAX(e.value) FILTER (WHERE e.name = 'DateTime') AS exif_datetime",
|
||||
"ni.nuisance_id AS nuisance_id",
|
||||
),
|
||||
sm.From("publicreport.image").As("i"),
|
||||
sm.LeftJoin("publicreport.image_exif").As("e").OnEQ(
|
||||
psql.Quote("r", "id"),
|
||||
psql.Quote("e", "image_id"),
|
||||
),
|
||||
sm.InnerJoin("publicreport.nuisance_image").As("ni").OnEQ(
|
||||
psql.Quote("ni", "image_id"),
|
||||
psql.Quote("i", "id"),
|
||||
),
|
||||
sm.Where(psql.Quote("ni", "nuisance_id").In(psql.Arg(report_ids))),
|
||||
), scan.StructMapper[types.Image]())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get images: %w", err)
|
||||
}
|
||||
results = make(map[int32][]types.Image, len(report_ids))
|
||||
for _, row := range rows {
|
||||
r, ok := results[row.NuisanceID]
|
||||
if !ok {
|
||||
r = make([]types.Image, 0)
|
||||
}
|
||||
r = append(r, row)
|
||||
results[row.NuisanceID] = r
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
103
platform/publicreport/nuisance.go
Normal file
103
platform/publicreport/nuisance.go
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
package publicreport
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/Gleipnir-Technology/bob"
|
||||
"github.com/Gleipnir-Technology/bob/dialect/psql"
|
||||
"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/platform/types"
|
||||
//"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
||||
//"github.com/google/uuid"
|
||||
//"github.com/rs/zerolog/log"
|
||||
"github.com/stephenafamo/scan"
|
||||
)
|
||||
|
||||
type Nuisance struct {
|
||||
AdditionalInfo string `db:"additional_info"`
|
||||
Address types.Address `db:"address"`
|
||||
AddressAsGiven string `db:"address_as_given"`
|
||||
Created time.Time `db:"created"`
|
||||
Duration string `db:"duration"`
|
||||
ID int32 `db:"id"`
|
||||
Images []types.Image
|
||||
IsLocationBackyard bool `db:"is_location_backyard"`
|
||||
IsLocationFrontyard bool `db:"is_location_frontyard"`
|
||||
IsLocationGarden bool `db:"is_location_garden"`
|
||||
IsLocationOther bool `db:"is_location_other"`
|
||||
IsLocationPool bool `db:"is_location_pool"`
|
||||
Location types.Location `db:"location"`
|
||||
PublicID string `db:"public_id"`
|
||||
Reporter Reporter `db:"reporter"`
|
||||
SourceContainer bool `db:"source_container"`
|
||||
SourceDescription string `db:"source_description"`
|
||||
SourceGutter bool `db:"source_gutter"`
|
||||
SourceStagnant bool `db:"source_stagnant"`
|
||||
TODDay bool `db:"tod_day"`
|
||||
TODEarly bool `db:"tod_early"`
|
||||
TODEvening bool `db:"tod_evening"`
|
||||
TODNight bool `db:"tod_night"`
|
||||
}
|
||||
type Reporter struct {
|
||||
Email *string `db:"reporter_email"`
|
||||
Name *string `db:"reporter_name"`
|
||||
Phone *string `db:"reporter_phone"`
|
||||
}
|
||||
|
||||
func NuisanceReportForOrganization(ctx context.Context, org_id int32) ([]Nuisance, error) {
|
||||
reports, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select(
|
||||
sm.Columns(
|
||||
"additional_info",
|
||||
"address AS address_as_given",
|
||||
"address_country AS \"address.country\"",
|
||||
"address_number AS \"address.number\"",
|
||||
"address_place AS \"address.place\"",
|
||||
"address_postcode AS \"address.postcode\"",
|
||||
"address_region AS \"address.region\"",
|
||||
"address_street AS \"address.street\"",
|
||||
"created",
|
||||
"duration",
|
||||
"id",
|
||||
"is_location_backyard",
|
||||
"is_location_frontyard",
|
||||
"is_location_garden",
|
||||
"is_location_other",
|
||||
"is_location_pool",
|
||||
"ST_Y(location::geometry::geometry(point, 4326)) AS \"location.latitude\"",
|
||||
"ST_X(location::geometry::geometry(point, 4326)) AS \"location.longitude\"",
|
||||
"public_id",
|
||||
"reporter_email AS \"reporter.email\"",
|
||||
"reporter_name AS \"reporter.name\"",
|
||||
"reporter_phone AS \"reporter.phone\"",
|
||||
"source_container",
|
||||
"source_description",
|
||||
"source_gutter",
|
||||
"source_stagnant",
|
||||
"tod_day",
|
||||
"tod_early",
|
||||
"tod_evening",
|
||||
"tod_night",
|
||||
),
|
||||
sm.From("publicreport.nuisance"),
|
||||
sm.Where(psql.Quote("publicreport", "nuisance", "organization_id").EQ(psql.Arg(org_id))),
|
||||
), scan.StructMapper[Nuisance]())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get reports: %w", err)
|
||||
}
|
||||
report_ids := make([]int32, len(reports))
|
||||
for _, report := range reports {
|
||||
report_ids = append(report_ids, report.ID)
|
||||
}
|
||||
images_by_id, err := loadImagesForReportNuisance(ctx, org_id, report_ids)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("images for report: %w", err)
|
||||
}
|
||||
for _, report := range reports {
|
||||
report.Images = images_by_id[report.ID]
|
||||
}
|
||||
return reports, nil
|
||||
}
|
||||
11
platform/types/address.go
Normal file
11
platform/types/address.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package types
|
||||
|
||||
type Address struct {
|
||||
Country string `db:"country" json:"country"`
|
||||
Locality string `db:"locality" json:"locality"`
|
||||
Number string `db:"number" json:"number"`
|
||||
PostalCode string `db:"postal_code" json:"postal_code"`
|
||||
Region string `db:"region" json:"region"`
|
||||
Street string `db:"street" json:"street"`
|
||||
Unit string `db:"unit" json:"unit"`
|
||||
}
|
||||
14
platform/types/image.go
Normal file
14
platform/types/image.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Image struct {
|
||||
ExifMake string `db:"exif_make"`
|
||||
ExifModel string `db:"exif_model"`
|
||||
ExifDateTime string `db:"exif_datetime"`
|
||||
Location Location `db:"location"`
|
||||
NuisanceID int32 `db:"nuisance_id"`
|
||||
UUID uuid.UUID `db:"uuid"`
|
||||
}
|
||||
6
platform/types/location.go
Normal file
6
platform/types/location.go
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
package types
|
||||
|
||||
type Location struct {
|
||||
Latitude float64 `db:"latitude"`
|
||||
Longitude float64 `db:"longitude"`
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue