feat(expense): create and display expenses

This commit is contained in:
2021-11-26 01:51:13 +01:00
parent 82d86fb33f
commit 53b0b8c9a2
6 changed files with 71 additions and 24 deletions

View File

@@ -1,16 +1,37 @@
package expense
import "nos-comptes/internal/storage/dao/postgresql"
import (
"github.com/lib/pq"
"nos-comptes/internal/storage/dao/postgresql"
)
type Database struct {
*postgresql.DatabasePostgreSQL
}
func (d Database) GetAllExpensesOfAnAccount(id string) (interface{}, interface{}) {
func (db *Database) CreateExpense(expense *Expense) error {
q := `
SELECT a.id, a.user_id, a.name, a.provider, a.created_at, a.updated_at
FROM public.account a
WHERE a.user_id = $1
INSERT INTO public.expense
(account_id, value, type_expense, expense_date, libelle)
VALUES
($1, $2, $3, $4)
RETURNING id, created_at
`
err := db.Session.
QueryRow(q, expense.AccountId, expense.Value, expense.TypeExpense, expense.ExpenseDate, expense.Libelle).
Scan(&expense.ID, &expense.CreatedAt)
if errPq, ok := err.(*pq.Error); ok {
return postgresql.HandlePgError(errPq)
}
return err
}
func (db Database) GetAllExpensesOfAnAccount(id string) ([]*Expense, error) {
q := `
SELECT a.id, a.account_id, a.value, a.type_expense, a.expense_date, a.created_at, a.updated_at, a.libelle
FROM public.expense a
WHERE a.account_id = $1
`
rows, err := db.Session.Query(q, id)
if err != nil {
@@ -18,16 +39,16 @@ func (d Database) GetAllExpensesOfAnAccount(id string) (interface{}, interface{}
}
defer rows.Close()
as := make([]*Account, 0)
es := make([]*Expense, 0)
for rows.Next() {
a := Account{}
err := rows.Scan(&a.ID, &a.UserId, &a.Name, &a.Provider, &a.CreatedAt, &a.UpdatedAt)
e := Expense{}
err := rows.Scan(&e.ID, &e.AccountId, &e.Value, &e.TypeExpense, &e.ExpenseDate, &e.CreatedAt, &e.UpdatedAt, &e.Libelle)
if err != nil {
return nil, err
}
as = append(as, &a)
es = append(es, &e)
}
return as, nil
return es, nil
}
func NewDatabase(db *postgresql.DatabasePostgreSQL) *Database {