adds update action method

This commit is contained in:
Deepak Mallubhotla 2021-01-09 23:39:03 -06:00
parent dfb5ead740
commit bc68115ce1
7 changed files with 89 additions and 0 deletions

View File

@ -31,3 +31,8 @@ func (m *Model) Action(id int) (*Action, error) {
func (m *Model) AddAction(action *Action) (int, error) { func (m *Model) AddAction(action *Action) (int, error) {
return m.InsertAction(action) return m.InsertAction(action)
} }
// SaveAction saves and updates an action.
func (m *Model) SaveAction(action *Action) error {
return m.UpdateAction(action)
}

View File

@ -16,6 +16,10 @@ func (e *errorStore) InsertAction(action *models.Action) (int, error) {
return 0, e.error return 0, e.error
} }
func (e *errorStore) UpdateAction(action *models.Action) error {
return e.error
}
func (e *errorStore) SelectPlans() ([]*models.Plan, error) { func (e *errorStore) SelectPlans() ([]*models.Plan, error) {
return nil, e.error return nil, e.error
} }

View File

@ -10,6 +10,7 @@ type Store interface {
SelectActions() ([]*Action, error) SelectActions() ([]*Action, error)
SelectActionByID(id int) (*Action, error) SelectActionByID(id int) (*Action, error)
InsertAction(action *Action) (int, error) InsertAction(action *Action) (int, error)
UpdateAction(action *Action) error
SelectPlans() ([]*Plan, error) SelectPlans() ([]*Plan, error)
SelectPlanByID(id int) (*Plan, error) SelectPlanByID(id int) (*Plan, error)
InsertPlan(plan *Plan) (int, error) InsertPlan(plan *Plan) (int, error)

View File

@ -23,6 +23,10 @@ func (ms *multiStore) InsertAction(action *models.Action) (int, error) {
return int(action.ActionID), nil return int(action.ActionID), nil
} }
func (ms *multiStore) UpdateAction(action *models.Action) error {
return nil
}
func (ms *multiStore) SelectPlans() ([]*models.Plan, error) { func (ms *multiStore) SelectPlans() ([]*models.Plan, error) {
return ms.plans, nil return ms.plans, nil
} }
@ -65,6 +69,8 @@ func TestModelActions(t *testing.T) {
assert.Nil(err) assert.Nil(err)
assert.EqualValues(3, actionID) assert.EqualValues(3, actionID)
err = m.SaveAction(a1)
assert.Nil(err)
} }
func TestModelPlanMethods(t *testing.T) { func TestModelPlanMethods(t *testing.T) {

View File

@ -34,6 +34,10 @@ func (ms *multiStore) InsertAction(action *models.Action) (int, error) {
return int(action.ActionID), nil return int(action.ActionID), nil
} }
func (ms *multiStore) UpdateAction(action *models.Action) error {
return nil
}
func (ms *multiStore) SelectPlans() ([]*models.Plan, error) { func (ms *multiStore) SelectPlans() ([]*models.Plan, error) {
return ms.plans, nil return ms.plans, nil
} }
@ -87,6 +91,10 @@ func (e *errorStore) InsertAction(action *models.Action) (int, error) {
return 0, e.error return 0, e.error
} }
func (e *errorStore) UpdateAction(action *models.Action) error {
return e.error
}
func (e *errorStore) SelectPlans() ([]*models.Plan, error) { func (e *errorStore) SelectPlans() ([]*models.Plan, error) {
return nil, e.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) { func (e *onlyCreateStore) SelectActionByID(id int) (*models.Action, error) {
return nil, e.error return nil, e.error
} }
func (e *onlyCreateStore) InsertAction(action *models.Action) (int, error) { func (e *onlyCreateStore) InsertAction(action *models.Action) (int, error) {
return int(action.ActionID), nil return int(action.ActionID), nil
} }
func (e *onlyCreateStore) UpdateAction(action *models.Action) error {
return nil
}
func (e *onlyCreateStore) SelectPlans() ([]*models.Plan, error) { func (e *onlyCreateStore) SelectPlans() ([]*models.Plan, error) {
return nil, e.error return nil, e.error
} }

View File

@ -75,6 +75,28 @@ func (store *postgresStore) InsertAction(action *models.Action) (int, error) {
return id, nil 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) { func (store *postgresStore) SelectPlans() ([]*models.Plan, error) {
plans := make([]*models.Plan, 0) plans := make([]*models.Plan, 0)
err := store.db.Select(&plans, "SELECT plan_id, plan_date FROM plans") err := store.db.Select(&plans, "SELECT plan_id, plan_date FROM plans")

View File

@ -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)
}
}