diff --git a/models/models.go b/models/models.go index ec73370..3dbe75c 100644 --- a/models/models.go +++ b/models/models.go @@ -21,7 +21,7 @@ type Feature struct { // ID is an ID of the feature. // When the feature is submitted by a client for creation (i.e. in Update.Create) it is considered a 'local' ID which must be unique across all submitted features. ID FeatureID `json:"id"` - OwnerID *UserID `json:"owner_id"` + OwnerID UserID `json:"owner_id"` Name string `json:"name"` Properties map[string]interface{} `json:"properties"` Geometry geojson.Geometry `json:"geometry"` diff --git a/server/initdb.sql b/server/initdb.sql index 3391a8b..da1ba30 100644 --- a/server/initdb.sql +++ b/server/initdb.sql @@ -8,7 +8,7 @@ INSERT INTO users(id, name) VALUES(0, 'system'); DROP TABLE IF EXISTS features; CREATE TABLE IF NOT EXISTS features ( id integer PRIMARY KEY AUTOINCREMENT, - owner_id integer REFERENCES users(id) ON DELETE CASCADE, + owner_id integer NOT NULL REFERENCES users(id) ON DELETE CASCADE, name text NOT NULL, properties text NOT NULL, geom text NOT NULL diff --git a/server/server.go b/server/server.go index f4890f3..302c779 100644 --- a/server/server.go +++ b/server/server.go @@ -283,10 +283,7 @@ func (s *Server) getFeatures(conn *sqlite.Conn) ([]models.Feature, error) { id := stmt.ColumnInt64(0) - var ownerID *int64 - if stmt.ColumnType(1) != sqlite.SQLITE_NULL { - ownerID = ptrInt64(stmt.ColumnInt64(1)) - } + ownerID := stmt.ColumnInt64(1) name := stmt.ColumnText(2) @@ -313,7 +310,7 @@ func (s *Server) getFeatures(conn *sqlite.Conn) ([]models.Feature, error) { feature := models.Feature{ ID: models.FeatureID(id), - OwnerID: (*models.UserID)(ownerID), + OwnerID: models.UserID(ownerID), Name: name, Properties: properties, Geometry: geom, @@ -350,11 +347,7 @@ func (s *Server) addFeatures(conn *sqlite.Conn, features []models.Feature) (map[ return nil, fmt.Errorf("failed to clear bindings of prepared statement: %w", err) } - if feature.OwnerID == nil { - stmt.BindNull(1) - } else { - stmt.BindInt64(1, int64(*feature.OwnerID)) - } + stmt.BindInt64(1, int64(feature.OwnerID)) stmt.BindText(2, feature.Name) @@ -473,11 +466,7 @@ func (s *Server) updateFeatures(conn *sqlite.Conn, features []models.Feature) er return fmt.Errorf("failed to clear bindings of prepared statement: %w", err) } - if feature.OwnerID == nil { - stmt.BindNull(1) - } else { - stmt.BindInt64(1, int64(*feature.OwnerID)) - } + stmt.BindInt64(1, int64(feature.OwnerID)) stmt.BindText(2, feature.Name)