Adds insertplan to model

This commit is contained in:
Deepak Mallubhotla 2020-12-29 18:20:28 -06:00
parent b6a6c9375f
commit 9c56ea56e2
Signed by: deepak
GPG Key ID: 64BF53A3369104E7
5 changed files with 33 additions and 4 deletions

View File

@ -6,6 +6,7 @@ type Store interface {
SelectActionByID(id int) (*Action, error) SelectActionByID(id int) (*Action, error)
SelectPlans() ([]*Plan, error) SelectPlans() ([]*Plan, error)
SelectPlanByID(id int) (*Plan, error) SelectPlanByID(id int) (*Plan, error)
InsertPlan(plan *Plan) (int, error)
} }
// Model represents a current model item. // Model represents a current model item.

View File

@ -27,6 +27,10 @@ func (ms *multiStore) SelectPlanByID(id int) (*models.Plan, error) {
return ms.plans[0], nil return ms.plans[0], nil
} }
func (ms *multiStore) InsertPlan(plan *models.Plan) (int, error) {
return int(plan.PlanID), nil
}
func TestModelActions(t *testing.T) { func TestModelActions(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
a1 := &models.Action{ActionID: 3} a1 := &models.Action{ActionID: 3}
@ -53,3 +57,18 @@ func TestModelActions(t *testing.T) {
assert.Nil(err) assert.Nil(err)
assert.EqualValues(6, firstPlan.PlanID) assert.EqualValues(6, firstPlan.PlanID)
} }
func TestModelInsertPlan(t *testing.T) {
assert := assert.New(t)
p := &models.Plan{PlanID: 7}
ss := &multiStore{
[]*models.Action{},
[]*models.Plan{p},
}
m := models.New(ss)
planId, err := m.AddPlan(p)
assert.Nil(err)
assert.EqualValues(7, planId)
}

View File

@ -19,3 +19,8 @@ func (m *Model) Plans() ([]*Plan, error) {
func (m *Model) Plan(id int) (*Plan, error) { func (m *Model) Plan(id int) (*Plan, error) {
return m.SelectPlanByID(id) return m.SelectPlanByID(id)
} }
// AddPlan inserts a given plan into the store, returning the generated PlanID. The provided PlanID is ignored.
func (m *Model) AddPlan(plan *Plan) (int, error) {
return m.InsertPlan(plan)
}

View File

@ -1,10 +1,10 @@
package store package store
import ( import (
"github.com/jmoiron/sqlx" "fmt"
"gitea.deepak.science/deepak/gogmagog/models" "gitea.deepak.science/deepak/gogmagog/models"
"gitea.deepak.science/deepak/gogmagog/util" "gitea.deepak.science/deepak/gogmagog/util"
"github.com/jmoiron/sqlx"
) )
type postgresStore struct { type postgresStore struct {
@ -53,3 +53,7 @@ func (store *postgresStore) SelectPlanByID(id int) (*models.Plan, error) {
} }
return &plan, nil return &plan, nil
} }
func (store *postgresStore) InsertPlan(plan *models.Plan) (int, error) {
return 0, fmt.Errorf("Unimplemented method")
}