155 lines
4.1 KiB
Go
155 lines
4.1 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"gitea.deepak.science/deepak/gogmagog/models"
|
|
)
|
|
|
|
type inMemoryStore struct {
|
|
actions []*models.Action
|
|
plans []*models.Plan
|
|
users []*models.User
|
|
currentPlans []*models.CurrentPlan
|
|
}
|
|
|
|
// 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(userID int) ([]*models.Action, error) {
|
|
ret := make([]*models.Action, 0)
|
|
for _, action := range store.actions {
|
|
if int(action.UserID) == userID {
|
|
ret = append(ret, action)
|
|
}
|
|
}
|
|
return ret, nil
|
|
}
|
|
|
|
func (store *inMemoryStore) SelectActionsByPlanID(plan *models.Plan, userID int) ([]*models.Action, error) {
|
|
ret := make([]*models.Action, 0)
|
|
for _, action := range store.actions {
|
|
if (int(plan.PlanID) == int(action.PlanID)) && (int(action.UserID) == userID) {
|
|
ret = append(ret, action)
|
|
}
|
|
}
|
|
return ret, nil
|
|
}
|
|
|
|
func (store *inMemoryStore) SelectActionByID(id int, userID int) (*models.Action, error) {
|
|
for _, action := range store.actions {
|
|
if id == int(action.ActionID) && (int(action.UserID) == userID) {
|
|
return action, nil
|
|
}
|
|
}
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
|
|
func (store *inMemoryStore) InsertAction(action *models.Action, userID int) (int, error) {
|
|
id := len(store.actions) + 1
|
|
action.ActionID = int64(id)
|
|
action.UserID = int64(userID)
|
|
store.actions = append(store.actions, action)
|
|
return id, nil
|
|
}
|
|
|
|
func (store *inMemoryStore) UpdateAction(action *models.Action, userID int) error {
|
|
currentAction, err := store.SelectActionByID(int(action.ActionID), userID)
|
|
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(userID int) ([]*models.Plan, error) {
|
|
ret := make([]*models.Plan, 0)
|
|
for _, plan := range store.plans {
|
|
if int(plan.UserID) == userID {
|
|
ret = append(ret, plan)
|
|
}
|
|
}
|
|
return ret, nil
|
|
}
|
|
|
|
func (store *inMemoryStore) SelectPlanByID(id int, userID int) (*models.Plan, error) {
|
|
for _, plan := range store.plans {
|
|
if id == int(plan.PlanID) && (userID == int(plan.UserID)) {
|
|
return plan, nil
|
|
}
|
|
}
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
|
|
func (store *inMemoryStore) InsertPlan(plan *models.Plan, userID int) (int, error) {
|
|
id := len(store.plans) + 1
|
|
plan.PlanID = int64(id)
|
|
plan.UserID = int64(userID)
|
|
store.plans = append(store.plans, plan)
|
|
return id, nil
|
|
}
|
|
|
|
func (store *inMemoryStore) SelectCurrentPlan(userID int) (*models.CurrentPlan, error) {
|
|
for _, currentPlan := range store.currentPlans {
|
|
if userID == int(currentPlan.UserID) {
|
|
return currentPlan, nil
|
|
}
|
|
}
|
|
return nil, sql.ErrNoRows
|
|
}
|
|
|
|
func (store *inMemoryStore) InsertCurrentPlan(currentPlan *models.CurrentPlan, userID int) error {
|
|
_, err := store.SelectCurrentPlan(userID)
|
|
if err == nil {
|
|
return fmt.Errorf("Can't insert primary plan")
|
|
}
|
|
// actually impossible, but at this point it must be a not found error.
|
|
// if err != sql.ErrNoRows {
|
|
// return err
|
|
// }
|
|
|
|
store.currentPlans = append(store.currentPlans, &models.CurrentPlan{PlanID: int64(currentPlan.PlanID), UserID: int64(userID)})
|
|
return nil
|
|
}
|
|
|
|
func (store *inMemoryStore) UpdateCurrentPlan(currentPlan *models.CurrentPlan, userID int) error {
|
|
current, err := store.SelectCurrentPlan(userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
current.PlanID = currentPlan.PlanID
|
|
return 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
|
|
}
|