diff --git a/api/handlers/collection_groups.go b/api/handlers/collection_groups.go index 2ea7404a..ba531c96 100644 --- a/api/handlers/collection_groups.go +++ b/api/handlers/collection_groups.go @@ -174,3 +174,20 @@ func RemoveTimeseriesFromCollectionGroup(db *sqlx.DB) echo.HandlerFunc { return c.JSON(http.StatusOK, make(map[string]interface{})) } } + +// UpdateTimeseriesInCollectionGroup updates timeseries associative details in a certain collection group +func UpdateTimeseriesInCollectionGroup(db *sqlx.DB) echo.HandlerFunc { + return func(c echo.Context) error { + cgD := models.CollectionGroupDetails{} + if err := c.Bind(&cgD); err != nil { + return echo.NewHTTPError(http.StatusBadRequest, err.Error()) + } + + cgDT, err := models.UpdateTimeseriesInCollectionGroup(db, &cgD) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, err.Error()) + } + + return c.JSON(http.StatusOK, cgDT) + } +} diff --git a/api/main.go b/api/main.go index d51af7de..52840b12 100644 --- a/api/main.go +++ b/api/main.go @@ -237,6 +237,8 @@ func main() { private.POST("/projects/:project_id/collection_groups/:collection_group_id/timeseries/:timeseries_id", handlers.AddTimeseriesToCollectionGroup(db)) // // Collection Group; Remove Timeseries from collection_group private.DELETE("/projects/:project_id/collection_groups/:collection_group_id/timeseries/:timeseries_id", handlers.RemoveTimeseriesFromCollectionGroup(db)) + // // Collection Group; Update Timeseries in collection_group + private.PUT("/collection_groups/:collection_group_id/timeseries", handlers.UpdateTimeseriesInCollectionGroup(db)) // Plotting Configurations public.GET("/projects/:project_id/plot_configurations", handlers.ListPlotConfigurations(db)) diff --git a/api/models/collection_group.go b/api/models/collection_group.go index 8ad1fdfe..2b063668 100644 --- a/api/models/collection_group.go +++ b/api/models/collection_group.go @@ -30,6 +30,7 @@ type cgdTsItem struct { ts.Timeseries LatestTime *time.Time `json:"latest_time" db:"latest_time"` LatestValue *float32 `json:"latest_value" db:"latest_value"` + ListOrder *int `json:"list_order" db:"list_order"` } var listCollectionGroupsSQL = ` @@ -66,7 +67,7 @@ func GetCollectionGroupDetails(db *sqlx.DB, projectID *uuid.UUID, collectionGrou d.Timeseries = make([]cgdTsItem, 0) if err := db.Select( &d.Timeseries, - `SELECT t.*, tm.time as latest_time, tm.value as latest_value + `SELECT t.*, tm.time as latest_time, tm.value as latest_value, list_order FROM collection_group_timeseries cgt INNER JOIN collection_group cg on cg.id = cgt.collection_group_id INNER JOIN v_timeseries t on t.id = cgt.timeseries_id @@ -145,3 +146,34 @@ func RemoveTimeseriesFromCollectionGroup(db *sqlx.DB, collectionGroupID *uuid.UU } return nil } + +// UpdateTimeseriesInCollectionGroup updates timeseries associative details in a certain collection group +func UpdateTimeseriesInCollectionGroup(db *sqlx.DB, cgD *CollectionGroupDetails) (*CollectionGroupDetails, error) { + txn, err := db.Beginx() + if err != nil { + return nil, err + } + defer txn.Rollback() + + stmt_timeseries, err := txn.Preparex( + `UPDATE collection_group_timeseries + SET list_order = $1 + WHERE collection_group_id = $2 AND timeseries_id = $3; + `, + ) + if err != nil { + return nil, err + } + + for _, cgdTs := range cgD.Timeseries { + if _, err := stmt_timeseries.Exec(cgdTs.ListOrder, cgD.ID, cgdTs.ID); err != nil { + return nil, err + } + } + + if err := txn.Commit(); err != nil { + return nil, err + } + + return cgD, nil +} diff --git a/sql/common/V1.3.5__collection_group_timeseries_order.sql b/sql/common/V1.3.5__collection_group_timeseries_order.sql new file mode 100644 index 00000000..156d42c2 --- /dev/null +++ b/sql/common/V1.3.5__collection_group_timeseries_order.sql @@ -0,0 +1,2 @@ +ALTER TABLE collection_group_timeseries +ADD list_order int NULL; \ No newline at end of file diff --git a/sql/local/V999.1.0__seed_data.sql b/sql/local/V999.1.0__seed_data.sql index a361c56e..4e963593 100644 --- a/sql/local/V999.1.0__seed_data.sql +++ b/sql/local/V999.1.0__seed_data.sql @@ -250,9 +250,9 @@ INSERT INTO collection_group (id, project_id, name, slug) VALUES ('30b32cb1-0936-42c4-95d1-63a7832a57db', '5b6f4f37-7755-4cf9-bd02-94f1e9bc5984', 'High Water Inspection', 'high-water-inspection'); -- collection_group_timeseries -INSERT INTO collection_group_timeseries (collection_group_id, timeseries_id) VALUES - ('30b32cb1-0936-42c4-95d1-63a7832a57db', '7ee902a3-56d0-4acf-8956-67ac82c03a96'), - ('30b32cb1-0936-42c4-95d1-63a7832a57db', '9a3864a8-8766-4bfa-bad1-0328b166f6a8'); +INSERT INTO collection_group_timeseries (collection_group_id, timeseries_id, list_order) VALUES + ('30b32cb1-0936-42c4-95d1-63a7832a57db', '7ee902a3-56d0-4acf-8956-67ac82c03a96', 1), + ('30b32cb1-0936-42c4-95d1-63a7832a57db', '9a3864a8-8766-4bfa-bad1-0328b166f6a8', 0); -- plot_configuration INSERT INTO plot_configuration (project_id, id, slug, name) VALUES diff --git a/tests/postman/instrumentation-regression.postman_collection.json b/tests/postman/instrumentation-regression.postman_collection.json index bee3cffb..4dcbb860 100644 --- a/tests/postman/instrumentation-regression.postman_collection.json +++ b/tests/postman/instrumentation-regression.postman_collection.json @@ -5807,8 +5807,9 @@ " \"latest_time\": {\"type\": \"string\", \"format\": \"date-time\"},", " \"latest_value\": {\"type\": \"number\"},", " \"is_computed\": {\"type\": \"boolean\"},", + " \"list_order\": {\"type\": \"number\"},", " },", - " \"required\": [\"id\", \"slug\", \"name\", \"variable\", \"project_id\", \"project\", \"project_slug\", \"instrument_id\", \"instrument\", \"instrument_slug\", \"parameter_id\", \"parameter\", \"unit_id\", \"unit\", \"latest_time\", \"latest_value\", \"is_computed\"],", + " \"required\": [\"id\", \"slug\", \"name\", \"variable\", \"project_id\", \"project\", \"project_slug\", \"instrument_id\", \"instrument\", \"instrument_slug\", \"parameter_id\", \"parameter\", \"unit_id\", \"unit\", \"latest_time\", \"latest_value\", \"is_computed\", \"list_order\"],", " \"additionalProperties\": false", " },", " },",