1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2025-07-03 04:38:38 -04:00

Add per-user default currency

This commit is contained in:
2017-06-21 21:25:38 -04:00
parent 25b04a4f0f
commit 4e73e8b508
12 changed files with 237 additions and 23 deletions

View File

@ -3,7 +3,9 @@ package main
import (
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"gopkg.in/gorp.v1"
"io"
"log"
"net/http"
@ -11,12 +13,13 @@ import (
)
type User struct {
UserId int64
Name string
Username string
Password string `db:"-"`
PasswordHash string `json:"-"`
Email string
UserId int64
DefaultCurrency int64 // SecurityId of default currency, or ISO4217 code for it if creating new user
Name string
Username string
Password string `db:"-"`
PasswordHash string `json:"-"`
Email string
}
const BogusPassword = "password"
@ -54,6 +57,16 @@ func GetUser(userid int64) (*User, error) {
return &u, nil
}
func GetUserTx(transaction *gorp.Transaction, userid int64) (*User, error) {
var u User
err := transaction.SelectOne(&u, "SELECT * from users where UserId=?", userid)
if err != nil {
return nil, err
}
return &u, nil
}
func GetUserByUsername(username string) (*User, error) {
var u User
@ -70,6 +83,12 @@ func InsertUser(u *User) error {
return err
}
security_template := FindCurrencyTemplate(u.DefaultCurrency)
if security_template == nil {
transaction.Rollback()
return errors.New("Invalid ISO4217 Default Currency")
}
existing, err := transaction.SelectInt("SELECT count(*) from users where Username=?", u.Username)
if err != nil {
transaction.Rollback()
@ -86,6 +105,28 @@ func InsertUser(u *User) error {
return err
}
// Copy the security template and give it our new UserId
var security Security
security = *security_template
security.UserId = u.UserId
err = InsertSecurityTx(transaction, &security)
if err != nil {
transaction.Rollback()
return err
}
// Update the user's DefaultCurrency to our new SecurityId
u.DefaultCurrency = security.SecurityId
count, err := transaction.Update(u)
if err != nil {
transaction.Rollback()
return err
} else if count != 1 {
transaction.Rollback()
return errors.New("Would have updated more than one user")
}
err = transaction.Commit()
if err != nil {
transaction.Rollback()
@ -103,6 +144,42 @@ func GetUserFromSession(r *http.Request) (*User, error) {
return GetUser(s.UserId)
}
func UpdateUser(u *User) error {
transaction, err := DB.Begin()
if err != nil {
return err
}
security, err := GetSecurityTx(transaction, u.DefaultCurrency, u.UserId)
if err != nil {
transaction.Rollback()
return err
} else if security.UserId != u.UserId || security.SecurityId != u.DefaultCurrency {
transaction.Rollback()
return errors.New("UserId and DefaultCurrency don't match the fetched security")
} else if security.Type != Currency {
transaction.Rollback()
return errors.New("New DefaultCurrency security is not a currency")
}
count, err := transaction.Update(u)
if err != nil {
transaction.Rollback()
return err
} else if count != 1 {
transaction.Rollback()
return errors.New("Would have updated more than one user")
}
err = transaction.Commit()
if err != nil {
transaction.Rollback()
return err
}
return nil
}
func UserHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
user_json := r.PostFormValue("user")
@ -187,8 +264,8 @@ func UserHandler(w http.ResponseWriter, r *http.Request) {
user.PasswordHash = old_pwhash
}
count, err := DB.Update(user)
if count != 1 || err != nil {
err = UpdateUser(user)
if err != nil {
WriteError(w, 999 /*Internal Error*/)
log.Print(err)
return