mirror of
https://github.com/aclindsa/moneygo.git
synced 2025-06-14 13:58:37 -04:00
Move users and securities to store
This commit is contained in:
95
internal/store/db/securities.go
Normal file
95
internal/store/db/securities.go
Normal file
@ -0,0 +1,95 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/aclindsa/moneygo/internal/models"
|
||||
)
|
||||
|
||||
type SecurityInUseError struct {
|
||||
message string
|
||||
}
|
||||
|
||||
func (e SecurityInUseError) Error() string {
|
||||
return e.message
|
||||
}
|
||||
|
||||
func (tx *Tx) GetSecurity(securityid int64, userid int64) (*models.Security, error) {
|
||||
var s models.Security
|
||||
|
||||
err := tx.SelectOne(&s, "SELECT * from securities where UserId=? AND SecurityId=?", userid, securityid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
func (tx *Tx) GetSecurities(userid int64) (*[]*models.Security, error) {
|
||||
var securities []*models.Security
|
||||
|
||||
_, err := tx.Select(&securities, "SELECT * from securities where UserId=?", userid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &securities, nil
|
||||
}
|
||||
|
||||
func (tx *Tx) FindMatchingSecurities(userid int64, security *models.Security) (*[]*models.Security, error) {
|
||||
var securities []*models.Security
|
||||
|
||||
_, err := tx.Select(&securities, "SELECT * from securities where UserId=? AND Type=? AND AlternateId=? AND Preciseness=?", userid, security.Type, security.AlternateId, security.Precision)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &securities, nil
|
||||
}
|
||||
|
||||
func (tx *Tx) InsertSecurity(s *models.Security) error {
|
||||
err := tx.Insert(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tx *Tx) UpdateSecurity(security *models.Security) error {
|
||||
count, err := tx.Update(security)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count != 1 {
|
||||
return fmt.Errorf("Expected to update 1 security, was going to update %d", count)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tx *Tx) DeleteSecurity(s *models.Security) error {
|
||||
// First, ensure no accounts are using this security
|
||||
accounts, err := tx.SelectInt("SELECT count(*) from accounts where UserId=? and SecurityId=?", s.UserId, s.SecurityId)
|
||||
|
||||
if accounts != 0 {
|
||||
return SecurityInUseError{"One or more accounts still use this security"}
|
||||
}
|
||||
|
||||
user, err := tx.GetUser(s.UserId)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if user.DefaultCurrency == s.SecurityId {
|
||||
return SecurityInUseError{"Cannot delete security which is user's default currency"}
|
||||
}
|
||||
|
||||
// Remove all prices involving this security (either of this security, or
|
||||
// using it as a currency)
|
||||
_, err = tx.Exec("DELETE FROM prices WHERE SecurityId=? OR CurrencyId=?", s.SecurityId, s.SecurityId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
count, err := tx.Delete(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count != 1 {
|
||||
return fmt.Errorf("Expected to delete 1 security, was going to delete %d", count)
|
||||
}
|
||||
return nil
|
||||
}
|
@ -31,6 +31,12 @@ func (tx *Tx) SessionExists(secret string) (bool, error) {
|
||||
}
|
||||
|
||||
func (tx *Tx) DeleteSession(session *models.Session) error {
|
||||
_, err := tx.Delete(session)
|
||||
return err
|
||||
count, err := tx.Delete(session)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count != 1 {
|
||||
return fmt.Errorf("Expected to delete 1 user, was going to delete %d", count)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
86
internal/store/db/users.go
Normal file
86
internal/store/db/users.go
Normal file
@ -0,0 +1,86 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/aclindsa/moneygo/internal/models"
|
||||
)
|
||||
|
||||
func (tx *Tx) UsernameExists(username string) (bool, error) {
|
||||
existing, err := tx.SelectInt("SELECT count(*) from users where Username=?", username)
|
||||
return existing != 0, err
|
||||
}
|
||||
|
||||
func (tx *Tx) InsertUser(user *models.User) error {
|
||||
return tx.Insert(user)
|
||||
}
|
||||
|
||||
func (tx *Tx) GetUser(userid int64) (*models.User, error) {
|
||||
var u models.User
|
||||
|
||||
err := tx.SelectOne(&u, "SELECT * from users where UserId=?", userid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func (tx *Tx) GetUserByUsername(username string) (*models.User, error) {
|
||||
var u models.User
|
||||
|
||||
err := tx.SelectOne(&u, "SELECT * from users where Username=?", username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func (tx *Tx) UpdateUser(user *models.User) error {
|
||||
count, err := tx.Update(user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count != 1 {
|
||||
return fmt.Errorf("Expected to update 1 user, was going to update %d", count)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tx *Tx) DeleteUser(user *models.User) error {
|
||||
count, err := tx.Delete(user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count != 1 {
|
||||
return fmt.Errorf("Expected to delete 1 user, was going to delete %d", count)
|
||||
}
|
||||
_, err = tx.Exec("DELETE FROM prices WHERE prices.SecurityId IN (SELECT securities.SecurityId FROM securities WHERE securities.UserId=?)", user.UserId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec("DELETE FROM splits WHERE splits.TransactionId IN (SELECT transactions.TransactionId FROM transactions WHERE transactions.UserId=?)", user.UserId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec("DELETE FROM transactions WHERE transactions.UserId=?", user.UserId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec("DELETE FROM securities WHERE securities.UserId=?", user.UserId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec("DELETE FROM accounts WHERE accounts.UserId=?", user.UserId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec("DELETE FROM reports WHERE reports.UserId=?", user.UserId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec("DELETE FROM sessions WHERE sessions.UserId=?", user.UserId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user