diff --git a/models/action.go b/models/action.go index 8b71cb1..5f17bb1 100644 --- a/models/action.go +++ b/models/action.go @@ -31,3 +31,8 @@ func (m *Model) Action(id int) (*Action, error) { func (m *Model) AddAction(action *Action) (int, error) { return m.InsertAction(action) } + +// SaveAction saves and updates an action. +func (m *Model) SaveAction(action *Action) error { + return m.UpdateAction(action) +} diff --git a/models/err_model_test.go b/models/err_model_test.go index b800fd1..27c357a 100644 --- a/models/err_model_test.go +++ b/models/err_model_test.go @@ -16,6 +16,10 @@ func (e *errorStore) InsertAction(action *models.Action) (int, error) { return 0, e.error } +func (e *errorStore) UpdateAction(action *models.Action) error { + return e.error +} + func (e *errorStore) SelectPlans() ([]*models.Plan, error) { return nil, e.error } diff --git a/models/models.go b/models/models.go index 86e99c0..d25a1ac 100644 --- a/models/models.go +++ b/models/models.go @@ -10,6 +10,7 @@ type Store interface { SelectActions() ([]*Action, error) SelectActionByID(id int) (*Action, error) InsertAction(action *Action) (int, error) + UpdateAction(action *Action) error SelectPlans() ([]*Plan, error) SelectPlanByID(id int) (*Plan, error) InsertPlan(plan *Plan) (int, error) diff --git a/models/models_test.go b/models/models_test.go index ed832e6..7cb62e8 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -23,6 +23,10 @@ func (ms *multiStore) InsertAction(action *models.Action) (int, error) { return int(action.ActionID), nil } +func (ms *multiStore) UpdateAction(action *models.Action) error { + return nil +} + func (ms *multiStore) SelectPlans() ([]*models.Plan, error) { return ms.plans, nil } @@ -65,6 +69,8 @@ func TestModelActions(t *testing.T) { assert.Nil(err) assert.EqualValues(3, actionID) + err = m.SaveAction(a1) + assert.Nil(err) } func TestModelPlanMethods(t *testing.T) { diff --git a/routes/route_model_test.go b/routes/route_model_test.go index a177dfb..d0d98f8 100644 --- a/routes/route_model_test.go +++ b/routes/route_model_test.go @@ -34,6 +34,10 @@ func (ms *multiStore) InsertAction(action *models.Action) (int, error) { return int(action.ActionID), nil } +func (ms *multiStore) UpdateAction(action *models.Action) error { + return nil +} + func (ms *multiStore) SelectPlans() ([]*models.Plan, error) { return ms.plans, nil } @@ -87,6 +91,10 @@ func (e *errorStore) InsertAction(action *models.Action) (int, error) { return 0, e.error } +func (e *errorStore) UpdateAction(action *models.Action) error { + return e.error +} + func (e *errorStore) SelectPlans() ([]*models.Plan, error) { return nil, e.error } @@ -123,10 +131,15 @@ func (e *onlyCreateStore) SelectActions() ([]*models.Action, error) { func (e *onlyCreateStore) SelectActionByID(id int) (*models.Action, error) { return nil, e.error } + func (e *onlyCreateStore) InsertAction(action *models.Action) (int, error) { return int(action.ActionID), nil } +func (e *onlyCreateStore) UpdateAction(action *models.Action) error { + return nil +} + func (e *onlyCreateStore) SelectPlans() ([]*models.Plan, error) { return nil, e.error } diff --git a/store/postgres.go b/store/postgres.go index ae041de..a443f40 100644 --- a/store/postgres.go +++ b/store/postgres.go @@ -75,6 +75,28 @@ func (store *postgresStore) InsertAction(action *models.Action) (int, error) { return id, nil } +func (store *postgresStore) UpdateAction(action *models.Action) error { + query := `UPDATE actions SET + action_description = :action_description, + estimated_chunks = :estimated_chunks, + completed_chunks = :completed_chunks, + completed_on = :completed_on, + plan_id = :plan_id + WHERE action_id = :action_id` + tx := store.db.MustBegin() + _, err := store.db.NamedExec(query, action) + if err != nil { + tx.Rollback() + return err + } + err = tx.Commit() + if err != nil { + return err + } + return nil + +} + func (store *postgresStore) SelectPlans() ([]*models.Plan, error) { plans := make([]*models.Plan, 0) err := store.db.Select(&plans, "SELECT plan_id, plan_date FROM plans") diff --git a/store/postgres_test.go b/store/postgres_test.go index 823beb6..e45d38f 100644 --- a/store/postgres_test.go +++ b/store/postgres_test.go @@ -496,3 +496,41 @@ func TestInsertActionCommitErr(t *testing.T) { } } + +func TestUpdateAction(t *testing.T) { + // setup + assert := assert.New(t) + + str, mock := getDbMock(t) + completedOn, _ := time.Parse("2006-01-02", "2021-01-01") + action := &models.Action{ + CompletedOn: &completedOn, + EstimatedChunks: 3, + CompletedChunks: 6, + PlanID: 5, + ActionDescription: "testing", + ActionID: 2, + } + + mock.ExpectBegin() + mock.ExpectExec(` + UPDATE actions SET + action_description = \$1, + estimated_chunks = \$2, + completed_chunks = \$3, + completed_on = \$4, + plan_id = \$5 + WHERE action_id = \$6`). + WithArgs("testing", 3, 6, completedOn, 5, 2). + WillReturnResult(sqlmock.NewResult(1, 1)) + mock.ExpectCommit() + + // function under test + err := str.UpdateAction(action) + // check results + assert.Nil(err) + if err := mock.ExpectationsWereMet(); err != nil { + t.Errorf("unfulfilled expectations: %s", err) + } + +}