feat(account): #49 account page
This commit was merged in pull request #59.
This commit is contained in:
@@ -8,12 +8,13 @@ import (
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
// While it may be duplicated to check for groupIds in the database access, it serves as a security layer
|
||||
type Account interface {
|
||||
Insert(account *types.Account) error
|
||||
Update(account *types.Account) error
|
||||
Insert(groupId uuid.UUID, account *types.Account) error
|
||||
Update(groupId uuid.UUID, account *types.Account) error
|
||||
GetAll(groupId uuid.UUID) ([]*types.Account, error)
|
||||
Get(groupId uuid.UUID, id uuid.UUID) (*types.Account, error)
|
||||
Delete(id uuid.UUID) error
|
||||
Delete(groupId uuid.UUID, id uuid.UUID) error
|
||||
}
|
||||
|
||||
type AccountSqlite struct {
|
||||
@@ -24,11 +25,11 @@ func NewAccountSqlite(db *sqlx.DB) *AccountSqlite {
|
||||
return &AccountSqlite{db: db}
|
||||
}
|
||||
|
||||
func (db AccountSqlite) Insert(account *types.Account) error {
|
||||
func (db AccountSqlite) Insert(groupId uuid.UUID, account *types.Account) error {
|
||||
|
||||
_, err := db.db.Exec(`
|
||||
INSERT INTO account (id, group_id, name, current_balance, oink_balance, created_at, created_by)
|
||||
VALUES (?,?,?,?,?,?,?)`, account.Id, account.GroupId, 0, 0, account.CreatedAt, account.CreatedBy)
|
||||
VALUES (?,?,?,?,?,?,?)`, account.Id, groupId, account.Name, 0, 0, account.CreatedAt, account.CreatedBy)
|
||||
if err != nil {
|
||||
log.Error("Error inserting account: %v", err)
|
||||
return types.ErrInternal
|
||||
@@ -37,22 +38,34 @@ func (db AccountSqlite) Insert(account *types.Account) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db AccountSqlite) Update(account *types.Account) error {
|
||||
func (db AccountSqlite) Update(groupId uuid.UUID, account *types.Account) error {
|
||||
|
||||
_, err := db.db.Exec(`
|
||||
log.Info("Updating account: %v", account)
|
||||
r, err := db.db.Exec(`
|
||||
UPDATE account
|
||||
SET
|
||||
name = ?,
|
||||
current_balance = ?,
|
||||
last_transaction = ?,
|
||||
oink_balance = ?,
|
||||
updated_at = ?,
|
||||
updated_by = ?,
|
||||
updated_by = ?
|
||||
WHERE id = ?
|
||||
AND group_id = ?`, account.Name, account.CurrentBalance, account.LastTransaction, account.OinkBalance, account.UpdatedAt, account.UpdatedBy, account.Id, account.GroupId)
|
||||
AND group_id = ?`, account.Name, account.CurrentBalance, account.LastTransaction, account.OinkBalance, account.UpdatedAt, account.UpdatedBy, account.Id, groupId)
|
||||
if err != nil {
|
||||
log.Error("Error updating account: %v", err)
|
||||
return types.ErrInternal
|
||||
}
|
||||
rows, err := r.RowsAffected()
|
||||
if err != nil {
|
||||
log.Error("Error deleting account, getting rows affected: %v", err)
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
if rows == 0 {
|
||||
log.Error("Error deleting account, rows affected: %v", rows)
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -62,7 +75,7 @@ func (db AccountSqlite) GetAll(groupId uuid.UUID) ([]*types.Account, error) {
|
||||
accounts := make([]*types.Account, 0)
|
||||
err := db.db.Select(&accounts, `
|
||||
SELECT
|
||||
id, name,
|
||||
id, group_id, name,
|
||||
current_balance, last_transaction, oink_balance,
|
||||
created_at, created_by, updated_at, updated_by
|
||||
FROM account
|
||||
@@ -81,7 +94,7 @@ func (db AccountSqlite) Get(groupId uuid.UUID, id uuid.UUID) (*types.Account, er
|
||||
account := &types.Account{}
|
||||
err := db.db.Get(account, `
|
||||
SELECT
|
||||
id, name,
|
||||
id, group_id, name,
|
||||
current_balance, last_transaction, oink_balance,
|
||||
created_at, created_by, updated_at, updated_by
|
||||
FROM account
|
||||
@@ -95,9 +108,9 @@ func (db AccountSqlite) Get(groupId uuid.UUID, id uuid.UUID) (*types.Account, er
|
||||
return account, nil
|
||||
}
|
||||
|
||||
func (db AccountSqlite) Delete(id uuid.UUID) error {
|
||||
func (db AccountSqlite) Delete(groupId uuid.UUID, id uuid.UUID) error {
|
||||
|
||||
res, err := db.db.Exec("DELETE FROM account WHERE id = ?", id)
|
||||
res, err := db.db.Exec("DELETE FROM account WHERE id = ? and group_id = ?", id, groupId)
|
||||
if err != nil {
|
||||
log.Error("Error deleting account: %v", err)
|
||||
return types.ErrInternal
|
||||
@@ -110,6 +123,7 @@ func (db AccountSqlite) Delete(id uuid.UUID) error {
|
||||
}
|
||||
|
||||
if rows == 0 {
|
||||
log.Error("Error deleting account, rows affected: %v", rows)
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user