All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good
107 lines
2.6 KiB
Go
107 lines
2.6 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"gitea.deepak.science/deepak/gogmagog/models"
|
|
)
|
|
|
|
type inMemoryStore struct {
|
|
actions []*models.Action
|
|
plans []*models.Plan
|
|
users []*models.User
|
|
}
|
|
|
|
// GetInMemoryStore provides a purely in memory store, for testing purposes only, with no persistence.
|
|
func GetInMemoryStore() (models.Store, error) {
|
|
return &inMemoryStore{
|
|
actions: make([]*models.Action, 0),
|
|
plans: make([]*models.Plan, 0),
|
|
users: make([]*models.User, 0),
|
|
}, nil
|
|
}
|
|
|
|
func (store *inMemoryStore) SelectActions() ([]*models.Action, error) {
|
|
return store.actions, nil
|
|
}
|
|
|
|
func (store *inMemoryStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) {
|
|
ret := make([]*models.Action, 0)
|
|
for _, action := range store.actions {
|
|
if int(plan.PlanID) == int(action.PlanID) {
|
|
ret = append(ret, action)
|
|
}
|
|
}
|
|
return ret, nil
|
|
}
|
|
|
|
func (store *inMemoryStore) SelectActionByID(id int) (*models.Action, error) {
|
|
for _, action := range store.actions {
|
|
if id == int(action.ActionID) {
|
|
return action, nil
|
|
}
|
|
}
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
|
|
func (store *inMemoryStore) InsertAction(action *models.Action) (int, error) {
|
|
id := len(store.actions) + 1
|
|
action.ActionID = int64(id)
|
|
store.actions = append(store.actions, action)
|
|
return id, nil
|
|
}
|
|
|
|
func (store *inMemoryStore) UpdateAction(action *models.Action) error {
|
|
currentAction, err := store.SelectActionByID(int(action.ActionID))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
currentAction.ActionDescription = action.ActionDescription
|
|
currentAction.EstimatedChunks = action.EstimatedChunks
|
|
currentAction.CompletedChunks = action.CompletedChunks
|
|
currentAction.PlanID = action.PlanID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (store *inMemoryStore) SelectPlans() ([]*models.Plan, error) {
|
|
return store.plans, nil
|
|
}
|
|
|
|
func (store *inMemoryStore) SelectPlanByID(id int) (*models.Plan, error) {
|
|
for _, plan := range store.plans {
|
|
if id == int(plan.PlanID) {
|
|
return plan, nil
|
|
}
|
|
}
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
|
|
func (store *inMemoryStore) InsertPlan(plan *models.Plan) (int, error) {
|
|
id := len(store.plans) + 1
|
|
plan.PlanID = int64(id)
|
|
store.plans = append(store.plans, plan)
|
|
return id, nil
|
|
}
|
|
|
|
func (store *inMemoryStore) ConnectionLive() error {
|
|
return nil
|
|
}
|
|
|
|
func (store *inMemoryStore) SelectUserByUsername(username string) (*models.User, error) {
|
|
for _, user := range store.users {
|
|
if username == user.Username {
|
|
return user, nil
|
|
}
|
|
}
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
|
|
// inMemoryStore.InsertUser will not enforce unique usernames, which is ok.
|
|
func (store *inMemoryStore) InsertUser(user *models.User) (int, error) {
|
|
id := len(store.users) + 1
|
|
user.UserID = int64(id)
|
|
store.users = append(store.users, user)
|
|
return id, nil
|
|
}
|