From 9c56ea56e2ebe60d4103dc0c4ab75b1e5c6c8328 Mon Sep 17 00:00:00 2001 From: Deepak Date: Tue, 29 Dec 2020 18:20:28 -0600 Subject: [PATCH] Adds insertplan to model --- config/config.go | 4 ++-- models/models.go | 1 + models/models_test.go | 19 +++++++++++++++++++ models/plan.go | 5 +++++ store/postgres.go | 8 ++++++-- 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/config/config.go b/config/config.go index d89f5d6..599550e 100644 --- a/config/config.go +++ b/config/config.go @@ -10,7 +10,7 @@ import ( type AppConfig struct { Environment string Port string - Timezone string + Timezone string } // DBConfig is the config for the DB connection. @@ -34,7 +34,7 @@ func createDefaultConf() *Conf { App: AppConfig{ Environment: "local", Port: "8080", - Timezone: "America/New_York", + Timezone: "America/New_York", }, Db: DBConfig{ Type: "postgres", diff --git a/models/models.go b/models/models.go index e092729..0bd537a 100644 --- a/models/models.go +++ b/models/models.go @@ -6,6 +6,7 @@ type Store interface { SelectActionByID(id int) (*Action, error) SelectPlans() ([]*Plan, error) SelectPlanByID(id int) (*Plan, error) + InsertPlan(plan *Plan) (int, error) } // Model represents a current model item. diff --git a/models/models_test.go b/models/models_test.go index ba8e0e7..da9d36e 100644 --- a/models/models_test.go +++ b/models/models_test.go @@ -27,6 +27,10 @@ func (ms *multiStore) SelectPlanByID(id int) (*models.Plan, error) { return ms.plans[0], nil } +func (ms *multiStore) InsertPlan(plan *models.Plan) (int, error) { + return int(plan.PlanID), nil +} + func TestModelActions(t *testing.T) { assert := assert.New(t) a1 := &models.Action{ActionID: 3} @@ -53,3 +57,18 @@ func TestModelActions(t *testing.T) { assert.Nil(err) 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) +} diff --git a/models/plan.go b/models/plan.go index 708663a..b5efef7 100644 --- a/models/plan.go +++ b/models/plan.go @@ -19,3 +19,8 @@ func (m *Model) Plans() ([]*Plan, error) { func (m *Model) Plan(id int) (*Plan, error) { 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) +} diff --git a/store/postgres.go b/store/postgres.go index 3a32885..06f1241 100644 --- a/store/postgres.go +++ b/store/postgres.go @@ -1,10 +1,10 @@ package store import ( - "github.com/jmoiron/sqlx" - + "fmt" "gitea.deepak.science/deepak/gogmagog/models" "gitea.deepak.science/deepak/gogmagog/util" + "github.com/jmoiron/sqlx" ) type postgresStore struct { @@ -53,3 +53,7 @@ func (store *postgresStore) SelectPlanByID(id int) (*models.Plan, error) { } return &plan, nil } + +func (store *postgresStore) InsertPlan(plan *models.Plan) (int, error) { + return 0, fmt.Errorf("Unimplemented method") +}