diff --git a/api/routes.go b/api/routes.go index d275dcec..90f77f87 100644 --- a/api/routes.go +++ b/api/routes.go @@ -53,6 +53,8 @@ func AddRoutes(r *mux.Router) { r.Handle("/session", authenticatedHandlerJSON(session.Get)).Methods("GET").Name("session.get") signal := resource.Signal(r) r.Handle("/signal", authenticatedHandlerJSON(signal.List)).Methods("GET") + site := resource.Site(router) + r.Handle("/site", authenticatedHandlerJSONSlice(site.List)).Methods("GET") sync := resource.Sync(r) r.Handle("/sync", authenticatedHandlerJSONSlice(sync.List)).Methods("GET") r.Handle("/sudo/email", authenticatedHandlerJSONPost(postSudoEmail)).Methods("POST") diff --git a/platform/site.go b/platform/site.go index c21e1931..927d1c5a 100644 --- a/platform/site.go +++ b/platform/site.go @@ -64,6 +64,42 @@ func SiteCreate(ctx context.Context, txn bob.Tx, user User, address_id int32) (* Version: omit.From(int32(1)), }).One(ctx, txn) } +func SiteList(ctx context.Context, user User, limit int) ([]*types.Site, error) { + rows, err := bob.All(ctx, db.PGInstance.BobDB, psql.Select( + sm.Columns( + "address.country AS \"address.country\"", + "address.locality AS \"address.locality\"", + "COALESCE(address.location_latitude, 0) AS \"address.location.latitude\"", + "COALESCE(address.location_longitude, 0) AS \"address.location.longitude\"", + "address.number_ AS \"address.number\"", + "address.postal_code AS \"address.postal_code\"", + "address.region AS \"address.region\"", + "address.street AS \"address.street\"", + "address.unit AS \"address.unit\"", + "site.created AS \"created\"", + "site.id AS \"id\"", + "site.notes AS \"notes\"", + "site.owner_name AS \"owner.name\"", + "site.owner_phone_e164 AS \"owner.phone\"", + "site.parcel_id AS \"parcel_id\"", + ), + sm.From("site"), + sm.InnerJoin("address").OnEQ( + psql.Quote("site", "address_id"), + psql.Quote("address", "id"), + ), + sm.Where(psql.Quote("site", "organization_id").EQ(psql.Arg(user.Organization.ID))), + sm.Limit(limit), + ), scan.StructMapper[types.Site]()) + if err != nil { + return nil, fmt.Errorf("query sites: %w", err) + } + results := make([]*types.Site, len(rows)) + for i, row := range rows { + results[i] = &row + } + return results, nil +} func siteFromAddress(ctx context.Context, txn bob.Tx, user User, address_id int32) (*models.Site, error) { site, err := models.Sites.Query( models.SelectWhere.Sites.AddressID.EQ(address_id), diff --git a/resource/site.go b/resource/site.go new file mode 100644 index 00000000..7c38704f --- /dev/null +++ b/resource/site.go @@ -0,0 +1,34 @@ +package resource + +import ( + "context" + "net/http" + + nhttp "github.com/Gleipnir-Technology/nidus-sync/http" + "github.com/Gleipnir-Technology/nidus-sync/platform" + "github.com/Gleipnir-Technology/nidus-sync/platform/types" + //"github.com/aarondl/opt/null" + //"github.com/gorilla/mux" +) + +type siteR struct { + router *router +} + +func Site(r *router) *siteR { + return &siteR{ + router: r, + } +} + +func (res *siteR) List(ctx context.Context, r *http.Request, user platform.User, query QueryParams) ([]*types.Site, *nhttp.ErrorWithStatus) { + limit := 20 + if query.Limit != nil { + limit = *query.Limit + } + sites, err := platform.SiteList(ctx, user, limit) + if err != nil { + return nil, nhttp.NewError("list signals: %w", err) + } + return sites, nil +}