Finished the PgRepo pattern thingy
This commit is contained in:
+21
-11
@@ -3,32 +3,42 @@ package expenses
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"expenses/config"
|
"expenses/config"
|
||||||
"github.com/jackc/pgx/v5"
|
"expenses/expenses/repo"
|
||||||
lua "github.com/yuin/gopher-lua"
|
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
lua "github.com/yuin/gopher-lua"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ExpensesApp struct {
|
type ExpensesApp struct {
|
||||||
DB *pgx.Conn
|
Lua *lua.LState
|
||||||
Lua *lua.LState
|
ExpRepo repo.ExpenseRepository
|
||||||
|
CategoryRepo repo.CategoryRepository
|
||||||
|
UserRepo repo.UserRepository
|
||||||
|
StoreRepo repo.StoreRepository
|
||||||
|
TypeRepo repo.TypeRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
App *ExpensesApp
|
App *ExpensesApp
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewExpenseApp(db *pgx.Conn, luaVM *lua.LState) *ExpensesApp {
|
func NewExpenseApp(db *pgxpool.Pool, luaVM *lua.LState) *ExpensesApp {
|
||||||
return &ExpensesApp{
|
return &ExpensesApp{
|
||||||
DB: db,
|
ExpRepo: repo.NewPgExpRepo(db),
|
||||||
Lua: luaVM,
|
CategoryRepo: repo.NewPgCatRepo(db),
|
||||||
|
UserRepo: repo.NewPgUserRepo(db),
|
||||||
|
StoreRepo: repo.NewPgStoreRepo(db),
|
||||||
|
TypeRepo: repo.NewPgTypeRepo(db),
|
||||||
|
Lua: luaVM,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) Close() {
|
func (a *ExpensesApp) Close() {
|
||||||
ctx := context.Background()
|
// TODO
|
||||||
|
// Close the repos?
|
||||||
a.DB.Close(ctx)
|
|
||||||
a.Lua.Close()
|
a.Lua.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +66,7 @@ func StartApp() error {
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
pgStr := getPgConnString(cfg)
|
pgStr := getPgConnString(cfg)
|
||||||
conn, err := pgx.Connect(ctx, pgStr)
|
conn, err := pgxpool.New(ctx, pgStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to open migration DB: %v", err)
|
log.Fatalf("Failed to open migration DB: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,18 +8,13 @@ import (
|
|||||||
|
|
||||||
func (a *ExpensesApp) GetAllCategories() ([]mod.Category, error) {
|
func (a *ExpensesApp) GetAllCategories() ([]mod.Category, error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
|
||||||
if err != nil {
|
|
||||||
return []mod.Category{}, err
|
|
||||||
}
|
|
||||||
defer tx.Rollback(ctx)
|
|
||||||
|
|
||||||
expenses, err := mod.GetAllCategories(a.DB, tx)
|
categories, err := a.CategoryRepo.GetAll(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []mod.Category{}, err
|
return []mod.Category{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return expenses, tx.Commit(ctx)
|
return categories, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) GetCategory(id int32) (mod.Category, error) {
|
func (a *ExpensesApp) GetCategory(id int32) (mod.Category, error) {
|
||||||
|
|||||||
@@ -144,8 +144,8 @@ func CalculateDebts(e *mod.Expense) (mod.Debts, error) {
|
|||||||
return debts, nil
|
return debts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) settleDebt(payment mod.ExpensePayment,
|
func (a *ExpensesApp) settleDebt(payment mod.Payment,
|
||||||
credit mod.ExpensePayment, eId int32) error {
|
credit mod.Payment, eId int32) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -169,7 +169,7 @@ func (a *ExpensesApp) settleDebt(payment mod.ExpensePayment,
|
|||||||
|
|
||||||
func (a *ExpensesApp) ProcessDebt(expID int32, debt mod.Debt) error {
|
func (a *ExpensesApp) ProcessDebt(expID int32, debt mod.Debt) error {
|
||||||
// We have to balance the debtor and creditor payments
|
// We have to balance the debtor and creditor payments
|
||||||
payment := mod.ExpensePayment{
|
payment := mod.Payment{
|
||||||
User: mod.User{
|
User: mod.User{
|
||||||
UserID: debt.Debtor.UserID,
|
UserID: debt.Debtor.UserID,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ var (
|
|||||||
{ExpShareID: 1, User: user2, Share: dec.NewFromFloat(0.4)},
|
{ExpShareID: 1, User: user2, Share: dec.NewFromFloat(0.4)},
|
||||||
{ExpShareID: 2, User: user3, Share: dec.NewFromFloat(0.2)},
|
{ExpShareID: 2, User: user3, Share: dec.NewFromFloat(0.2)},
|
||||||
},
|
},
|
||||||
Payments: []mod.ExpensePayment{
|
Payments: []mod.Payment{
|
||||||
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromInt(50)},
|
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromInt(50)},
|
||||||
{ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromInt(50)},
|
{ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromInt(50)},
|
||||||
{ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromInt(0)},
|
{ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromInt(0)},
|
||||||
@@ -56,7 +56,7 @@ var (
|
|||||||
{ExpShareID: 2, User: user3, Share: dec.NewFromFloat(0.3), Calculated: dec.NewFromFloat(48.0)},
|
{ExpShareID: 2, User: user3, Share: dec.NewFromFloat(0.3), Calculated: dec.NewFromFloat(48.0)},
|
||||||
{ExpShareID: 3, User: user4, Share: dec.NewFromFloat(0.1), Calculated: dec.NewFromFloat(16)},
|
{ExpShareID: 3, User: user4, Share: dec.NewFromFloat(0.1), Calculated: dec.NewFromFloat(16)},
|
||||||
},
|
},
|
||||||
Payments: []mod.ExpensePayment{
|
Payments: []mod.Payment{
|
||||||
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromInt(40)},
|
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromInt(40)},
|
||||||
{ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromInt(40)},
|
{ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromInt(40)},
|
||||||
{ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromInt(5)},
|
{ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromInt(5)},
|
||||||
@@ -131,7 +131,7 @@ var (
|
|||||||
{ExpShareID: 2, User: user3, Share: dec.NewFromFloat(0.3)},
|
{ExpShareID: 2, User: user3, Share: dec.NewFromFloat(0.3)},
|
||||||
{ExpShareID: 3, User: user4, Share: dec.NewFromFloat(0.1)},
|
{ExpShareID: 3, User: user4, Share: dec.NewFromFloat(0.1)},
|
||||||
},
|
},
|
||||||
Payments: []mod.ExpensePayment{
|
Payments: []mod.Payment{
|
||||||
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromInt(40)},
|
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromInt(40)},
|
||||||
{ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromInt(8)},
|
{ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromInt(8)},
|
||||||
{ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromInt(48)},
|
{ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromInt(48)},
|
||||||
@@ -151,7 +151,7 @@ var (
|
|||||||
{ExpShareID: 1, User: user2, Share: dec.NewFromFloat(0.33)},
|
{ExpShareID: 1, User: user2, Share: dec.NewFromFloat(0.33)},
|
||||||
{ExpShareID: 2, User: user3, Share: dec.NewFromFloat(0.33)},
|
{ExpShareID: 2, User: user3, Share: dec.NewFromFloat(0.33)},
|
||||||
},
|
},
|
||||||
Payments: []mod.ExpensePayment{
|
Payments: []mod.Payment{
|
||||||
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromFloat(0.18)},
|
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromFloat(0.18)},
|
||||||
{ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromFloat(0.16)},
|
{ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromFloat(0.16)},
|
||||||
{ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromFloat(0.16)},
|
{ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromFloat(0.16)},
|
||||||
@@ -172,7 +172,7 @@ var (
|
|||||||
{ExpShareID: 2, User: user3,
|
{ExpShareID: 2, User: user3,
|
||||||
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(36.66)},
|
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(36.66)},
|
||||||
},
|
},
|
||||||
Payments: []mod.ExpensePayment{
|
Payments: []mod.Payment{
|
||||||
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromFloat(37.79)},
|
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromFloat(37.79)},
|
||||||
{ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromFloat(36.66)},
|
{ExpPaymID: 1, User: user1, PayedAmount: dec.NewFromFloat(36.66)},
|
||||||
{ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromFloat(36.66)},
|
{ExpPaymID: 2, User: user2, PayedAmount: dec.NewFromFloat(36.66)},
|
||||||
@@ -190,7 +190,7 @@ var (
|
|||||||
{ExpShareID: 2, User: user3,
|
{ExpShareID: 2, User: user3,
|
||||||
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(0.16)},
|
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(0.16)},
|
||||||
},
|
},
|
||||||
Payments: []mod.ExpensePayment{
|
Payments: []mod.Payment{
|
||||||
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromFloat(0.50)},
|
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromFloat(0.50)},
|
||||||
{ExpPaymID: 1, User: user2, PayedAmount: dec.NewFromFloat(0.0)},
|
{ExpPaymID: 1, User: user2, PayedAmount: dec.NewFromFloat(0.0)},
|
||||||
{ExpPaymID: 2, User: user3, PayedAmount: dec.NewFromFloat(0.0)},
|
{ExpPaymID: 2, User: user3, PayedAmount: dec.NewFromFloat(0.0)},
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ import (
|
|||||||
// conversion that the user calls before calling this
|
// conversion that the user calls before calling this
|
||||||
// Can even create a struct to then hold the data
|
// Can even create a struct to then hold the data
|
||||||
func ParseFormPayments(userIDs []string, paymentsIDs []string,
|
func ParseFormPayments(userIDs []string, paymentsIDs []string,
|
||||||
values []string) ([]mod.ExpensePayment, error) {
|
values []string) ([]mod.Payment, error) {
|
||||||
payments := []mod.ExpensePayment{}
|
payments := []mod.Payment{}
|
||||||
for k := range userIDs {
|
for k := range userIDs {
|
||||||
userID, err := ParseID(userIDs[k])
|
userID, err := ParseID(userIDs[k])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -26,7 +26,7 @@ func ParseFormPayments(userIDs []string, paymentsIDs []string,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
newPayment := mod.ExpensePayment{
|
newPayment := mod.Payment{
|
||||||
ExpPaymID: -1,
|
ExpPaymID: -1,
|
||||||
User: mod.User{
|
User: mod.User{
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
@@ -49,11 +49,11 @@ func ParseFormPayments(userIDs []string, paymentsIDs []string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) GetExpensePaymentByUserID(eId int32, uId int32,
|
func (a *ExpensesApp) GetExpensePaymentByUserID(eId int32, uId int32,
|
||||||
) (mod.ExpensePayment, error) {
|
) (mod.Payment, error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mod.ExpensePayment{}, err
|
return mod.Payment{}, err
|
||||||
}
|
}
|
||||||
defer tx.Rollback(ctx)
|
defer tx.Rollback(ctx)
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@ func (a *ExpensesApp) GetExpensePaymentByUserID(eId int32, uId int32,
|
|||||||
|
|
||||||
// insertPayment allows us to insert a payment manually
|
// insertPayment allows us to insert a payment manually
|
||||||
// for now its private, need to figure out if it needs to be public
|
// for now its private, need to figure out if it needs to be public
|
||||||
func (a *ExpensesApp) insertPayment(payment mod.ExpensePayment, eIdx int32) error {
|
func (a *ExpensesApp) insertPayment(payment mod.Payment, eIdx int32) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -88,7 +88,7 @@ func (a *ExpensesApp) DeletePayment(id int32) error {
|
|||||||
}
|
}
|
||||||
defer tx.Rollback(ctx)
|
defer tx.Rollback(ctx)
|
||||||
|
|
||||||
payment := mod.ExpensePayment{
|
payment := mod.Payment{
|
||||||
ExpPaymID: id,
|
ExpPaymID: id,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +100,7 @@ func (a *ExpensesApp) DeletePayment(id int32) error {
|
|||||||
return tx.Commit(ctx)
|
return tx.Commit(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) UpdatePayment(payment mod.ExpensePayment) error {
|
func (a *ExpensesApp) UpdatePayment(payment mod.Payment) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -117,7 +117,7 @@ func (a *ExpensesApp) UpdatePayment(payment mod.ExpensePayment) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) AddPayment(expID int32, userID int32, sum dec.Decimal) error {
|
func (a *ExpensesApp) AddPayment(expID int32, userID int32, sum dec.Decimal) error {
|
||||||
payment := mod.ExpensePayment{
|
payment := mod.Payment{
|
||||||
User: mod.User{
|
User: mod.User{
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -13,18 +13,12 @@ import (
|
|||||||
func (a *ExpensesApp) GetAllExpenses() ([]mod.Expense, error) {
|
func (a *ExpensesApp) GetAllExpenses() ([]mod.Expense, error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
tx, err := a.DB.Begin(ctx)
|
expenses, err := a.ExpRepo.GetAll(ctx)
|
||||||
if err != nil {
|
|
||||||
return []mod.Expense{}, err
|
|
||||||
}
|
|
||||||
defer tx.Rollback(ctx)
|
|
||||||
|
|
||||||
expenses, err := mod.GetAllExpenses(a.DB, tx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []mod.Expense{}, err
|
return []mod.Expense{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return expenses, tx.Commit(ctx)
|
return expenses, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) GetExpensesRanged(startDate string, endDate string) ([]mod.Expense, error) {
|
func (a *ExpensesApp) GetExpensesRanged(startDate string, endDate string) ([]mod.Expense, error) {
|
||||||
@@ -56,18 +50,12 @@ func (a *ExpensesApp) GetExpensesRanged(startDate string, endDate string) ([]mod
|
|||||||
func (a *ExpensesApp) GetExpense(id int32) (mod.Expense, error) {
|
func (a *ExpensesApp) GetExpense(id int32) (mod.Expense, error) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
tx, err := a.DB.Begin(ctx)
|
expense, err := a.ExpRepo.Get(ctx, id)
|
||||||
if err != nil {
|
|
||||||
return mod.Expense{}, err
|
|
||||||
}
|
|
||||||
defer tx.Rollback(ctx)
|
|
||||||
|
|
||||||
expense, err := mod.GetExpense(a.DB, tx, id)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mod.Expense{}, err
|
return mod.Expense{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return expense, tx.Commit(ctx)
|
return expense, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) LoadExpenseShares(e *mod.Expense) error {
|
func (a *ExpensesApp) LoadExpenseShares(e *mod.Expense) error {
|
||||||
@@ -113,22 +101,8 @@ func (a *ExpensesApp) LoadExpenseDebts(e *mod.Expense) error {
|
|||||||
|
|
||||||
func (a *ExpensesApp) DeleteExpense(id int32) error {
|
func (a *ExpensesApp) DeleteExpense(id int32) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
err := a.ExpRepo.Delete(ctx, id)
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer tx.Rollback(ctx)
|
|
||||||
|
|
||||||
expense := mod.Expense{
|
|
||||||
ExpID: id,
|
|
||||||
}
|
|
||||||
|
|
||||||
err = expense.Delete(a.DB, tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return tx.Commit(ctx)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapShares(e *mod.Expense) map[mod.User]decimal.Decimal {
|
func mapShares(e *mod.Expense) map[mod.User]decimal.Decimal {
|
||||||
@@ -190,16 +164,9 @@ func (a *ExpensesApp) analyzeExpense(e *mod.Expense) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) NewExpense(exp mod.Expense) error {
|
func (a *ExpensesApp) NewExpense(exp mod.Expense) error {
|
||||||
ctx := context.Background()
|
|
||||||
tx, err := a.DB.BeginTx(ctx, pgx.TxOptions{})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer tx.Rollback(ctx)
|
|
||||||
|
|
||||||
a.analyzeExpense(&exp)
|
a.analyzeExpense(&exp)
|
||||||
|
|
||||||
err = exp.Insert(a.DB, tx)
|
err := a.Storage.Insert()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,16 +22,7 @@ func (a *ExpensesApp) prepareExpense(exp *mod.Expense) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *ExpensesApp) luaGetAllExpenses(L *lua.LState) int {
|
func (a *ExpensesApp) luaGetAllExpenses(L *lua.LState) int {
|
||||||
// TODO
|
expenses, err := a.Storage.GetAllExpenses()
|
||||||
// It makes no sense this repeats this db thing
|
|
||||||
ctx := context.Background()
|
|
||||||
tx, err := a.DB.Begin(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return returnWithError(L, err.Error())
|
|
||||||
}
|
|
||||||
defer tx.Rollback(ctx)
|
|
||||||
|
|
||||||
expenses, err := mod.GetAllExpenses(a.DB, tx)
|
|
||||||
|
|
||||||
tbl := L.NewTable()
|
tbl := L.NewTable()
|
||||||
for _, e := range expenses {
|
for _, e := range expenses {
|
||||||
|
|||||||
@@ -1,17 +1,10 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
repo "expenses/expenses/db/repository"
|
|
||||||
experr "expenses/expenses/errors"
|
|
||||||
"fmt"
|
|
||||||
"github.com/jackc/pgx/v5"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Category struct {
|
type Category struct {
|
||||||
CategoryID int32 `json:"CategoryID"`
|
CategoryID int32 `json:"CategoryID"`
|
||||||
CategoryName string `json:"CategoryName"`
|
CategoryName string `json:"CategoryName"`
|
||||||
}
|
}
|
||||||
|
type Categories []Category
|
||||||
|
|
||||||
func NewCategory() Category {
|
func NewCategory() Category {
|
||||||
return Category{
|
return Category{
|
||||||
@@ -19,78 +12,3 @@ func NewCategory() Category {
|
|||||||
CategoryName: "",
|
CategoryName: "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllCategories(db repo.DBTX, tx pgx.Tx) ([]Category, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
categories, err := queries.GetCategories(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return []Category{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return MapRepoCategories(categories), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetCategory(db repo.DBTX, tx pgx.Tx, catID int32) (Category, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
category, err := queries.GetCategory(ctx, catID)
|
|
||||||
|
|
||||||
return MapRepoCategory(category), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cat *Category) Insert(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.InsertCategory(ctx, cat.CategoryName)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return fmt.Errorf("no rows were created")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cat *Category) Delete(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.DeleteCategory(ctx, cat.CategoryID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return experr.ErrNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cat *Category) Update(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.UpdateCategory(ctx, repo.UpdateCategoryParams{
|
|
||||||
CategoryName: cat.CategoryName,
|
|
||||||
CategoryID: cat.CategoryID,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return experr.ErrNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import repo "expenses/expenses/db/repository"
|
|
||||||
|
|
||||||
func MapRepoCategory(rc repo.Category) Category {
|
|
||||||
return Category{
|
|
||||||
CategoryID: rc.CategoryID,
|
|
||||||
CategoryName: rc.CategoryName,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func MapRepoCategories(rc []repo.Category) []Category {
|
|
||||||
categories := make([]Category, len(rc))
|
|
||||||
for k, cat := range rc {
|
|
||||||
categories[k] = MapRepoCategory(cat)
|
|
||||||
}
|
|
||||||
return categories
|
|
||||||
}
|
|
||||||
+17
-249
@@ -1,33 +1,28 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
repo "expenses/expenses/db/repository"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Expense struct {
|
type Expense struct {
|
||||||
ExpID int32 `json:"ExpID"`
|
ExpID int32 `json:"ExpID"`
|
||||||
Description string `json:"Description"`
|
Description string `json:"Description"`
|
||||||
Value decimal.Decimal `json:"Value"`
|
Value decimal.Decimal `json:"Value"`
|
||||||
Store Store `json:"Store"`
|
Store Store `json:"Store"`
|
||||||
Type Type `json:"Type"`
|
Type Type `json:"Type"`
|
||||||
Category Category `json:"Category"`
|
Category Category `json:"Category"`
|
||||||
Owner User `json:"Owner"`
|
Owner User `json:"Owner"`
|
||||||
Date time.Time `json:"Date"`
|
Date time.Time `json:"Date"`
|
||||||
Payments []ExpensePayment `json:"Payments"`
|
Payments []Payment `json:"Payments"`
|
||||||
Shares []Share `json:"Shares"`
|
Shares []Share `json:"Shares"`
|
||||||
Debts Debts `json:"Debts"`
|
Debts Debts `json:"Debts"`
|
||||||
PaidOff bool `json:"PaidOff"`
|
PaidOff bool `json:"PaidOff"`
|
||||||
SharesEven bool `json:"SharesEven"`
|
SharesEven bool `json:"SharesEven"`
|
||||||
CreationDate time.Time `json:"CreationDate"`
|
CreationDate time.Time `json:"CreationDate"`
|
||||||
}
|
}
|
||||||
|
type Expenses []Expense
|
||||||
|
|
||||||
func NewExpense() Expense {
|
func NewExpense() Expense {
|
||||||
return Expense{
|
return Expense{
|
||||||
@@ -38,7 +33,7 @@ func NewExpense() Expense {
|
|||||||
Category: NewCategory(),
|
Category: NewCategory(),
|
||||||
Owner: NewUser(),
|
Owner: NewUser(),
|
||||||
Date: time.Now(),
|
Date: time.Now(),
|
||||||
Payments: []ExpensePayment{},
|
Payments: []Payment{},
|
||||||
Shares: []Share{},
|
Shares: []Share{},
|
||||||
PaidOff: false,
|
PaidOff: false,
|
||||||
SharesEven: false,
|
SharesEven: false,
|
||||||
@@ -49,233 +44,6 @@ func NewExpense() Expense {
|
|||||||
// ExpenseFromJSON takes []byte and returns an *Expense
|
// ExpenseFromJSON takes []byte and returns an *Expense
|
||||||
func ExpenseFromJSON(data []byte) (*Expense, error) {
|
func ExpenseFromJSON(data []byte) (*Expense, error) {
|
||||||
var expense Expense
|
var expense Expense
|
||||||
|
|
||||||
err := json.Unmarshal(data, &expense)
|
err := json.Unmarshal(data, &expense)
|
||||||
|
|
||||||
return &expense, err
|
return &expense, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllExpenses(db repo.DBTX, tx pgx.Tx) ([]Expense, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
var start pgtype.Timestamp
|
|
||||||
start.Valid = false
|
|
||||||
var end pgtype.Timestamp
|
|
||||||
end.Valid = false
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
expenses, err := queries.GetExpenses(ctx, repo.GetExpensesParams{
|
|
||||||
Startdate: start,
|
|
||||||
Enddate: end,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return []Expense{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return mapRepoGetExpensesRows(expenses), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetExpense(db repo.DBTX, tx pgx.Tx, expID int32) (Expense, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
expense, err := queries.GetExpense(ctx, expID)
|
|
||||||
if err != nil {
|
|
||||||
return Expense{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return mapRepoGetExpenseRow(expense), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (exp *Expense) Insert(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
value, err := decimalToNumeric(exp.Value)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
paidOff, err := boolToPgBool(exp.PaidOff)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
sharesEven, err := boolToPgBool(exp.SharesEven)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
expDate, err := timeToTimestamp(exp.Date)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
creationDate, err := timeToTimestamp(exp.CreationDate)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
lastInsertedID, err := queries.InsertExpense(ctx, repo.InsertExpenseParams{
|
|
||||||
Description: exp.Description,
|
|
||||||
Value: value,
|
|
||||||
StoreID: exp.Store.StoreID,
|
|
||||||
CategoryID: exp.Category.CategoryID,
|
|
||||||
TypeID: exp.Type.TypeID,
|
|
||||||
OwnerUserID: exp.Owner.UserID,
|
|
||||||
ExpDate: expDate,
|
|
||||||
PaidOff: paidOff,
|
|
||||||
SharesEven: sharesEven,
|
|
||||||
CreationDate: creationDate,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
exp.ExpID = lastInsertedID
|
|
||||||
|
|
||||||
err = exp.InsertShares(db, tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = exp.InsertPayments(db, tx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (exp *Expense) InsertShares(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
for _, share := range exp.Shares {
|
|
||||||
err := share.Insert(db, tx, exp.ExpID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (exp *Expense) InsertPayments(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
for _, paym := range exp.Payments {
|
|
||||||
err := paym.Insert(db, tx, exp.ExpID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Expense) Update(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
value, err := decimalToNumeric(e.Value)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
paidOff, err := boolToPgBool(e.PaidOff)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
sharesEven, err := boolToPgBool(e.SharesEven)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
expDate, err := timeToTimestamp(e.Date)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.UpdateExpense(ctx, repo.UpdateExpenseParams{
|
|
||||||
ExpID: e.ExpID,
|
|
||||||
Description: e.Description,
|
|
||||||
Value: value,
|
|
||||||
StoreID: e.Store.StoreID,
|
|
||||||
CategoryID: e.Category.CategoryID,
|
|
||||||
TypeID: e.Type.TypeID,
|
|
||||||
OwnerUserID: e.Owner.UserID,
|
|
||||||
PaidOff: paidOff,
|
|
||||||
SharesEven: sharesEven,
|
|
||||||
ExpDate: expDate,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, share := range e.Shares {
|
|
||||||
if share.ExpShareID == -1 {
|
|
||||||
err := share.Insert(db, tx, e.ExpID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
share.Update(db, tx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, paym := range e.Payments {
|
|
||||||
if paym.ExpPaymID == -1 {
|
|
||||||
err := paym.Insert(db, tx, e.ExpID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
paym.Update(db, tx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return fmt.Errorf("no rows were updated")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Expense) Delete(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.DeleteExpense(ctx, e.ExpID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return fmt.Errorf("no rows were created")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SHITTY QUERIES THAT I NEED TO PUT IN SOMEWHERE MORE OGRANHJASLD
|
|
||||||
func GetExpensesRange(db repo.DBTX, tx pgx.Tx, start time.Time, end time.Time) ([]Expense, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
startPg, err := timeToTimestamp(start)
|
|
||||||
if err != nil {
|
|
||||||
return []Expense{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
endPg, err := timeToTimestamp(end)
|
|
||||||
if err != nil {
|
|
||||||
return []Expense{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
expenses, err := queries.GetExpenses(ctx, repo.GetExpensesParams{
|
|
||||||
Startdate: startPg,
|
|
||||||
Enddate: endPg,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return []Expense{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return mapRepoGetExpensesRows(expenses), nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,122 +1,20 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
repo "expenses/expenses/db/repository"
|
|
||||||
"fmt"
|
|
||||||
"github.com/jackc/pgx/v5"
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ExpensePayment struct {
|
type Payment struct {
|
||||||
ExpPaymID int32
|
ExpPaymID int32
|
||||||
User User
|
User User
|
||||||
PayedAmount decimal.Decimal
|
PayedAmount decimal.Decimal
|
||||||
}
|
}
|
||||||
|
type Payments []Payment
|
||||||
|
|
||||||
// PaymentFromJSON takes []byte and returns an *ExpensePayment
|
// PaymentFromJSON takes []byte and returns an *ExpensePayment
|
||||||
func PaymentFromJSON(data []byte) (*ExpensePayment, error) {
|
func PaymentFromJSON(data []byte) (*Payment, error) {
|
||||||
var payment ExpensePayment
|
var payment Payment
|
||||||
err := json.Unmarshal(data, &payment)
|
err := json.Unmarshal(data, &payment)
|
||||||
return &payment, err
|
return &payment, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expense) GetPayments(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
payments, err := queries.GetPayments(ctx, e.ExpID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
e.Payments = mapRepoGetPaymentsRows(payments)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetExpensePaymentByUserID(db repo.DBTX, tx pgx.Tx, eId int32, uId int32,
|
|
||||||
) (ExpensePayment, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
payment, err := queries.GetExpensePaymentByUser(ctx, repo.GetExpensePaymentByUserParams{
|
|
||||||
ExpID: eId,
|
|
||||||
UserID: uId,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return ExpensePayment{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return mapRepoGetPaymentRow(payment), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *ExpensePayment) Insert(db repo.DBTX, tx pgx.Tx, expID int32) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
payed, err := decimalToNumeric(p.PayedAmount)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.InsertPayment(ctx, repo.InsertPaymentParams{
|
|
||||||
ExpID: expID,
|
|
||||||
UserID: p.User.UserID,
|
|
||||||
Payed: payed,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return fmt.Errorf("no rows were created")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *ExpensePayment) Update(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
payed, err := decimalToNumeric(p.PayedAmount)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.UpdatePayment(ctx, repo.UpdatePaymentParams{
|
|
||||||
ExpPaymID: p.ExpPaymID,
|
|
||||||
Payed: payed,
|
|
||||||
UserID: p.User.UserID,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return fmt.Errorf("no rows were created")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *ExpensePayment) Delete(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.DeletePayment(ctx, p.ExpPaymID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return fmt.Errorf("no rows were created")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,13 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"expenses/expenses/db/repository"
|
|
||||||
repo "expenses/expenses/db/repository"
|
|
||||||
"fmt"
|
|
||||||
"github.com/jackc/pgx/v5"
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
|
||||||
dec "github.com/shopspring/decimal"
|
dec "github.com/shopspring/decimal"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -43,95 +37,3 @@ func (sh Shares) Equal(other Shares) bool {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expense) GetShares(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repository.New(db).WithTx(tx)
|
|
||||||
shares, err := queries.GetShares(ctx, e.ExpID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
e.Shares = mapRepoGetSharesRows(shares)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sh *Share) Insert(db repo.DBTX, tx pgx.Tx, expID int32) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
share, err := decimalToNumeric(sh.Share)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
calculated, err := decimalToNumeric(sh.Calculated)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.InsertShare(ctx, repo.InsertShareParams{
|
|
||||||
ExpID: expID,
|
|
||||||
Share: share,
|
|
||||||
UserID: sh.User.UserID,
|
|
||||||
Calculated: calculated,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return fmt.Errorf("no rows were created")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sh *Share) Update(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
share, err := decimalToNumeric(sh.Share)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
calculated, err := decimalToNumeric(sh.Calculated)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.UpdateShare(ctx, repo.UpdateShareParams{
|
|
||||||
ExpShareID: sh.ExpShareID,
|
|
||||||
Share: share,
|
|
||||||
UserID: sh.User.UserID,
|
|
||||||
Calculated: calculated,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return fmt.Errorf("no rows were created")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sh *Share) Delete(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.DeleteShare(ctx, sh.ExpShareID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return fmt.Errorf("no rows were created")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,18 +1,10 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
repo "expenses/expenses/db/repository"
|
|
||||||
experr "expenses/expenses/errors"
|
|
||||||
"fmt"
|
|
||||||
"github.com/jackc/pgx/v5"
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Store struct {
|
type Store struct {
|
||||||
StoreID int32 `json:"StoreID"`
|
StoreID int32 `json:"StoreID"`
|
||||||
StoreName string `json:"StoreName"`
|
StoreName string `json:"StoreName"`
|
||||||
}
|
}
|
||||||
|
type Stores []Store
|
||||||
|
|
||||||
func NewStore() Store {
|
func NewStore() Store {
|
||||||
return Store{
|
return Store{
|
||||||
@@ -20,76 +12,3 @@ func NewStore() Store {
|
|||||||
StoreName: "",
|
StoreName: "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllStores(db repo.DBTX, tx pgx.Tx) ([]Store, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
storeList, err := queries.GetStores(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return []Store{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return mapRepoStores(storeList), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetStore(db repo.DBTX, tx pgx.Tx, storeID int32) (Store, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
store, err := queries.GetStore(ctx, storeID)
|
|
||||||
if err != nil {
|
|
||||||
return Store{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return mapRepoStore(store), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Store) Insert(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.InsertStore(ctx, s.StoreName)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return fmt.Errorf("no rows were created")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Store) Update(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
_, err := queries.UpdateStore(ctx, repo.UpdateStoreParams{
|
|
||||||
StoreID: s.StoreID,
|
|
||||||
StoreName: s.StoreName,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Store) Delete(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.DeleteStore(ctx, s.StoreID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return experr.ErrNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import repo "expenses/expenses/db/repository"
|
|
||||||
|
|
||||||
func mapRepoStore(rs repo.Store) Store {
|
|
||||||
return Store{
|
|
||||||
StoreID: rs.StoreID,
|
|
||||||
StoreName: rs.StoreName,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func mapRepoStores(rs []repo.Store) []Store {
|
|
||||||
stores := make([]Store, len(rs))
|
|
||||||
for k, store := range rs {
|
|
||||||
stores[k] = mapRepoStore(store)
|
|
||||||
}
|
|
||||||
return stores
|
|
||||||
}
|
|
||||||
@@ -1,18 +1,10 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
repo "expenses/expenses/db/repository"
|
|
||||||
experr "expenses/expenses/errors"
|
|
||||||
"fmt"
|
|
||||||
"github.com/jackc/pgx/v5"
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Type struct {
|
type Type struct {
|
||||||
TypeID int32 `json:"TypeID"`
|
TypeID int32 `json:"TypeID"`
|
||||||
TypeName string `json:"TypeName"`
|
TypeName string `json:"TypeName"`
|
||||||
}
|
}
|
||||||
|
type Types []Type
|
||||||
|
|
||||||
func NewType() Type {
|
func NewType() Type {
|
||||||
return Type{
|
return Type{
|
||||||
@@ -20,84 +12,3 @@ func NewType() Type {
|
|||||||
TypeName: "",
|
TypeName: "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllTypes(db repo.DBTX, tx pgx.Tx) ([]Type, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
typeList, err := queries.GetTypes(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return []Type{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return mapRepoTypes(typeList), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetType(db repo.DBTX, tx pgx.Tx, typID int32) (Type, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
typ, err := queries.GetType(ctx, typID)
|
|
||||||
if err != nil {
|
|
||||||
return Type{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return mapRepoType(typ), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (typ *Type) Insert(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.InsertType(ctx, typ.TypeName)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO
|
|
||||||
// Move all theses things outside of this, I'll handle it wherever I'm doing
|
|
||||||
// logics
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return fmt.Errorf("no rows were created")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (typ *Type) Delete(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.DeleteType(ctx, typ.TypeID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return experr.ErrNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (typ *Type) Update(db repo.DBTX, tx pgx.Tx) error {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
res, err := queries.UpdateType(ctx, repo.UpdateTypeParams{
|
|
||||||
TypeID: typ.TypeID,
|
|
||||||
TypeName: typ.TypeName,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
rowsAffected := res.RowsAffected()
|
|
||||||
if rowsAffected == 0 {
|
|
||||||
return experr.ErrNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import repo "expenses/expenses/db/repository"
|
|
||||||
|
|
||||||
func mapRepoType(rt repo.ExpenseType) Type {
|
|
||||||
return Type{
|
|
||||||
TypeID: rt.TypeID,
|
|
||||||
TypeName: rt.TypeName,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func mapRepoTypes(rt []repo.ExpenseType) []Type {
|
|
||||||
types := make([]Type, len(rt))
|
|
||||||
for k, typ := range rt {
|
|
||||||
types[k] = mapRepoType(typ)
|
|
||||||
}
|
|
||||||
return types
|
|
||||||
}
|
|
||||||
@@ -1,16 +1,10 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
repo "expenses/expenses/db/repository"
|
|
||||||
"github.com/jackc/pgx/v5"
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
|
||||||
)
|
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
UserID int32 `json:"UserID"`
|
UserID int32 `json:"UserID"`
|
||||||
UserName string `json:"UserName"`
|
UserName string `json:"UserName"`
|
||||||
}
|
}
|
||||||
|
type Users []User
|
||||||
|
|
||||||
func NewUser() User {
|
func NewUser() User {
|
||||||
return User{
|
return User{
|
||||||
@@ -18,15 +12,3 @@ func NewUser() User {
|
|||||||
UserName: "",
|
UserName: "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllUsers(db repo.DBTX, tx pgx.Tx) ([]User, error) {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
queries := repo.New(db).WithTx(tx)
|
|
||||||
userList, err := queries.GetUsers(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return []User{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return mapRepoUsers(userList), nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
import repo "expenses/expenses/db/repository"
|
|
||||||
|
|
||||||
func mapRepoUser(ru repo.User) User {
|
|
||||||
return User{
|
|
||||||
UserID: ru.UserID,
|
|
||||||
UserName: ru.UserName,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func mapRepoUsers(ru []repo.User) []User {
|
|
||||||
users := make([]User, len(ru))
|
|
||||||
for k, u := range ru {
|
|
||||||
users[k] = mapRepoUser(u)
|
|
||||||
}
|
|
||||||
return users
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CategoryRepository interface {
|
||||||
|
Get(ctx context.Context, id int32) (mod.Category, error)
|
||||||
|
GetAll(ctx context.Context) (mod.Categories, error)
|
||||||
|
Update(ctx context.Context, cat mod.Category) error
|
||||||
|
Insert(ctx context.Context, cat mod.Category) error
|
||||||
|
Delete(ctx context.Context, id int32) error
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ExpenseRepository interface {
|
||||||
|
// Direct expense methods
|
||||||
|
Get(ctx context.Context, id int32) (mod.Expense, error)
|
||||||
|
GetAll(ctx context.Context) (mod.Expenses, error)
|
||||||
|
GetExpensesRange(ctx context.Context, start time.Time,
|
||||||
|
end time.Time) (mod.Expenses, error)
|
||||||
|
Update(ctx context.Context, exp mod.Expense) error
|
||||||
|
Insert(ctx context.Context, exp mod.Expense) error
|
||||||
|
Delete(ctx context.Context, id int32) error
|
||||||
|
|
||||||
|
// Share methods
|
||||||
|
GetShares(ctx context.Context, eId int32) (mod.Shares, error)
|
||||||
|
UpdateShare(ctx context.Context, sh mod.Share) error
|
||||||
|
InsertShare(ctx context.Context, eId int32, sh mod.Share) error
|
||||||
|
InsertShares(ctx context.Context, eId int32, sh mod.Shares) error
|
||||||
|
DeleteShare(ctx context.Context, sh mod.Share) error
|
||||||
|
|
||||||
|
// Payment methods
|
||||||
|
GetPayments(ctx context.Context, eId int32) (mod.Payments, error)
|
||||||
|
GetExpensePaymentByUserID(ctx context.Context, eId int32,
|
||||||
|
uId int32) (mod.Payment, error)
|
||||||
|
UpdatePayment(ctx context.Context, pm mod.Payment) error
|
||||||
|
InsertPayment(ctx context.Context, eId int32, pm mod.Payment) error
|
||||||
|
InsertPayments(ctx context.Context, eId int32, pm mod.Payments) error
|
||||||
|
DeletePayment(ctx context.Context, pm mod.Payment) error
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
experr "expenses/expenses/errors"
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||||
|
"fmt"
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PgCatRepo struct {
|
||||||
|
DB *pgxpool.Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPgCatRepo(db *pgxpool.Pool) CategoryRepository {
|
||||||
|
return PgCatRepo{
|
||||||
|
DB: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgCatRepo) Get(ctx context.Context, id int32) (mod.Category, error) {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
category, err := queries.GetCategory(ctx, id)
|
||||||
|
return MapRepoCategory(category), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgCatRepo) GetAll(ctx context.Context) (mod.Categories, error) {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
categories, err := queries.GetCategories(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return mod.Categories{}, nil
|
||||||
|
}
|
||||||
|
return MapRepoCategories(categories), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgCatRepo) Update(ctx context.Context, cat mod.Category) error {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
res, err := queries.UpdateCategory(ctx, pgsqlc.UpdateCategoryParams{
|
||||||
|
CategoryName: cat.CategoryName,
|
||||||
|
CategoryID: cat.CategoryID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return experr.ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgCatRepo) Insert(ctx context.Context, cat mod.Category) error {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
res, err := queries.InsertCategory(ctx, cat.CategoryName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("no rows were created")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgCatRepo) Delete(ctx context.Context, id int32) error {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
res, err := queries.DeleteCategory(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return experr.ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
pgsqlc "expenses/expenses/repo/pgdb/pgsqlc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func MapRepoCategory(rc pgsqlc.Category) mod.Category {
|
||||||
|
return mod.Category{
|
||||||
|
CategoryID: rc.CategoryID,
|
||||||
|
CategoryName: rc.CategoryName,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func MapRepoCategories(rc []pgsqlc.Category) mod.Categories {
|
||||||
|
categories := make(mod.Categories, len(rc))
|
||||||
|
for k, cat := range rc {
|
||||||
|
categories[k] = MapRepoCategory(cat)
|
||||||
|
}
|
||||||
|
return categories
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package models
|
package repo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (p PgExpRepo) insertPayment(
|
||||||
|
ctx context.Context, q *pgsqlc.Queries, eId int32, pm mod.Payment) error {
|
||||||
|
payed, err := decimalToNumeric(pm.PayedAmount)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := q.InsertPayment(ctx, pgsqlc.InsertPaymentParams{
|
||||||
|
ExpID: eId,
|
||||||
|
UserID: pm.User.UserID,
|
||||||
|
Payed: payed,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("no rows were created")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) InsertPayment(
|
||||||
|
ctx context.Context, eId int32, pm mod.Payment) error {
|
||||||
|
return p.insertPayment(ctx, pgsqlc.New(p.DB), eId, pm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PgExpRepo) insertPayments(
|
||||||
|
ctx context.Context, q *pgsqlc.Queries, eId int32, pm []mod.Payment) error {
|
||||||
|
for k := range pm {
|
||||||
|
err := p.insertPayment(ctx, q, eId, pm[k])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) InsertPayments(
|
||||||
|
ctx context.Context, eId int32, pm mod.Payments) error {
|
||||||
|
return p.insertPayments(ctx, pgsqlc.New(p.DB), eId, pm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PgExpRepo) updatePayment(
|
||||||
|
ctx context.Context, q *pgsqlc.Queries, pm mod.Payment) error {
|
||||||
|
|
||||||
|
payed, err := decimalToNumeric(pm.PayedAmount)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := q.UpdatePayment(ctx, pgsqlc.UpdatePaymentParams{
|
||||||
|
ExpPaymID: pm.ExpPaymID,
|
||||||
|
Payed: payed,
|
||||||
|
UserID: pm.User.UserID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("no rows were created")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) UpdatePayment(
|
||||||
|
ctx context.Context, pm mod.Payment) error {
|
||||||
|
return p.updatePayment(ctx, pgsqlc.New(p.DB), pm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PgExpRepo) deletePayment(
|
||||||
|
ctx context.Context, q *pgsqlc.Queries, pm mod.Payment) error {
|
||||||
|
|
||||||
|
res, err := q.DeletePayment(ctx, pm.ExpPaymID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("no rows were created")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) DeletePayment(ctx context.Context, pm mod.Payment) error {
|
||||||
|
return p.deletePayment(ctx, pgsqlc.New(p.DB), pm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PgExpRepo) getPayments(
|
||||||
|
ctx context.Context, q *pgsqlc.Queries, eId int32) (mod.Payments, error) {
|
||||||
|
|
||||||
|
payments, err := q.GetPayments(ctx, eId)
|
||||||
|
if err != nil {
|
||||||
|
return mod.Payments{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mapRepoGetPaymentsRows(payments), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) GetPayments(
|
||||||
|
ctx context.Context, eId int32) (mod.Payments, error) {
|
||||||
|
return p.getPayments(ctx, pgsqlc.New(p.DB), eId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) GetExpensePaymentByUserID(ctx context.Context, eId int32, uId int32,
|
||||||
|
) (mod.Payment, error) {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
payment, err := queries.GetExpensePaymentByUser(ctx, pgsqlc.GetExpensePaymentByUserParams{
|
||||||
|
ExpID: eId,
|
||||||
|
UserID: uId,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return mod.Payment{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mapRepoGetPaymentRow(payment), nil
|
||||||
|
}
|
||||||
+11
-10
@@ -1,37 +1,38 @@
|
|||||||
package models
|
package repo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
repo "expenses/expenses/db/repository"
|
mod "expenses/expenses/models"
|
||||||
|
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||||
)
|
)
|
||||||
|
|
||||||
func mapRepoGetPaymentsRow(s repo.GetPaymentsRow) ExpensePayment {
|
func mapRepoGetPaymentsRow(s pgsqlc.GetPaymentsRow) mod.Payment {
|
||||||
payed := pgNumericToDecimal(s.ExpensesPayment.Payed)
|
payed := pgNumericToDecimal(s.ExpensesPayment.Payed)
|
||||||
|
|
||||||
return ExpensePayment{
|
return mod.Payment{
|
||||||
ExpPaymID: s.ExpensesPayment.ExpPaymID,
|
ExpPaymID: s.ExpensesPayment.ExpPaymID,
|
||||||
PayedAmount: payed,
|
PayedAmount: payed,
|
||||||
User: User{
|
User: mod.User{
|
||||||
UserID: s.User.UserID,
|
UserID: s.User.UserID,
|
||||||
UserName: s.User.UserName,
|
UserName: s.User.UserName,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapRepoGetPaymentsRows(ep []repo.GetPaymentsRow) []ExpensePayment {
|
func mapRepoGetPaymentsRows(ep []pgsqlc.GetPaymentsRow) []mod.Payment {
|
||||||
payments := make([]ExpensePayment, len(ep))
|
payments := make([]mod.Payment, len(ep))
|
||||||
for k, exp := range ep {
|
for k, exp := range ep {
|
||||||
payments[k] = mapRepoGetPaymentsRow(exp)
|
payments[k] = mapRepoGetPaymentsRow(exp)
|
||||||
}
|
}
|
||||||
return payments
|
return payments
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapRepoGetPaymentRow(s repo.GetExpensePaymentByUserRow) ExpensePayment {
|
func mapRepoGetPaymentRow(s pgsqlc.GetExpensePaymentByUserRow) mod.Payment {
|
||||||
payed := pgNumericToDecimal(s.ExpensesPayment.Payed)
|
payed := pgNumericToDecimal(s.ExpensesPayment.Payed)
|
||||||
|
|
||||||
return ExpensePayment{
|
return mod.Payment{
|
||||||
ExpPaymID: s.ExpensesPayment.ExpPaymID,
|
ExpPaymID: s.ExpensesPayment.ExpPaymID,
|
||||||
PayedAmount: payed,
|
PayedAmount: payed,
|
||||||
User: User{
|
User: mod.User{
|
||||||
UserID: s.User.UserID,
|
UserID: s.User.UserID,
|
||||||
UserName: s.User.UserName,
|
UserName: s.User.UserName,
|
||||||
},
|
},
|
||||||
@@ -0,0 +1,249 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PgExpRepo struct {
|
||||||
|
DB *pgxpool.Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPgExpRepo(db *pgxpool.Pool) ExpenseRepository {
|
||||||
|
return PgExpRepo{
|
||||||
|
DB: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *PgExpRepo) withTx(ctx context.Context, fn func(q *pgsqlc.Queries) error) error {
|
||||||
|
tx, err := r.DB.BeginTx(ctx, pgx.TxOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer tx.Rollback(ctx)
|
||||||
|
|
||||||
|
q := pgsqlc.New(tx) // bind sqlc to this tx
|
||||||
|
|
||||||
|
if err := fn(q); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tx.Commit(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PgExpRepo) getAll(ctx context.Context, q *pgsqlc.Queries) (mod.Expenses, error) {
|
||||||
|
// We want to select all here, no timestamps
|
||||||
|
var start pgtype.Timestamp
|
||||||
|
start.Valid = false
|
||||||
|
var end pgtype.Timestamp
|
||||||
|
end.Valid = false
|
||||||
|
|
||||||
|
expenses, err := q.GetExpenses(ctx, pgsqlc.GetExpensesParams{
|
||||||
|
Startdate: start,
|
||||||
|
Enddate: end,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return []mod.Expense{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mapRepoGetExpensesRows(expenses), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) GetAll(ctx context.Context) (mod.Expenses, error) {
|
||||||
|
return p.getAll(ctx, pgsqlc.New(p.DB))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PgExpRepo) get(ctx context.Context, q *pgsqlc.Queries, id int32) (mod.Expense, error) {
|
||||||
|
expense, err := q.GetExpense(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return mod.Expense{}, err
|
||||||
|
}
|
||||||
|
return mapRepoGetExpenseRow(expense), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) Get(ctx context.Context, id int32) (mod.Expense, error) {
|
||||||
|
return p.get(ctx, pgsqlc.New(p.DB), id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) Update(
|
||||||
|
ctx context.Context,
|
||||||
|
exp mod.Expense) error {
|
||||||
|
|
||||||
|
return p.withTx(ctx, func(q *pgsqlc.Queries) error {
|
||||||
|
value, err := decimalToNumeric(exp.Value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
paidOff, err := boolToPgBool(exp.PaidOff)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sharesEven, err := boolToPgBool(exp.SharesEven)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
expDate, err := timeToTimestamp(exp.Date)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := q.UpdateExpense(ctx, pgsqlc.UpdateExpenseParams{
|
||||||
|
ExpID: exp.ExpID,
|
||||||
|
Description: exp.Description,
|
||||||
|
Value: value,
|
||||||
|
StoreID: exp.Store.StoreID,
|
||||||
|
CategoryID: exp.Category.CategoryID,
|
||||||
|
TypeID: exp.Type.TypeID,
|
||||||
|
OwnerUserID: exp.Owner.UserID,
|
||||||
|
PaidOff: paidOff,
|
||||||
|
SharesEven: sharesEven,
|
||||||
|
ExpDate: expDate,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, share := range exp.Shares {
|
||||||
|
if share.ExpShareID == -1 {
|
||||||
|
err := p.insertShare(ctx, q, exp.ExpID, share)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err := p.updateShare(ctx, q, share)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, paym := range exp.Payments {
|
||||||
|
if paym.ExpPaymID == -1 {
|
||||||
|
err := p.insertPayment(ctx, q, exp.ExpID, paym)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err := p.updatePayment(ctx, q, paym)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("no rows were updated")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) Insert(
|
||||||
|
ctx context.Context,
|
||||||
|
exp mod.Expense) error {
|
||||||
|
|
||||||
|
return p.withTx(ctx, func(q *pgsqlc.Queries) error {
|
||||||
|
value, err := decimalToNumeric(exp.Value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
paidOff, err := boolToPgBool(exp.PaidOff)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sharesEven, err := boolToPgBool(exp.SharesEven)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
expDate, err := timeToTimestamp(exp.Date)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
creationDate, err := timeToTimestamp(exp.CreationDate)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
lastInsertedID, err := q.InsertExpense(ctx, pgsqlc.InsertExpenseParams{
|
||||||
|
Description: exp.Description,
|
||||||
|
Value: value,
|
||||||
|
StoreID: exp.Store.StoreID,
|
||||||
|
CategoryID: exp.Category.CategoryID,
|
||||||
|
TypeID: exp.Type.TypeID,
|
||||||
|
OwnerUserID: exp.Owner.UserID,
|
||||||
|
ExpDate: expDate,
|
||||||
|
PaidOff: paidOff,
|
||||||
|
SharesEven: sharesEven,
|
||||||
|
CreationDate: creationDate,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
exp.ExpID = lastInsertedID
|
||||||
|
|
||||||
|
err = p.insertShares(ctx, q, exp.ExpID, exp.Shares)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = p.insertPayments(ctx, q, exp.ExpID, exp.Payments)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) Delete(ctx context.Context, id int32) error {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
res, err := queries.DeleteExpense(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("no rows were delete")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) GetExpensesRange(
|
||||||
|
ctx context.Context, start time.Time, end time.Time) (mod.Expenses, error) {
|
||||||
|
|
||||||
|
startPg, err := timeToTimestamp(start)
|
||||||
|
if err != nil {
|
||||||
|
return mod.Expenses{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
endPg, err := timeToTimestamp(end)
|
||||||
|
if err != nil {
|
||||||
|
return mod.Expenses{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
expenses, err := queries.GetExpenses(ctx, pgsqlc.GetExpensesParams{
|
||||||
|
Startdate: startPg,
|
||||||
|
Enddate: endPg,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return mod.Expenses{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mapRepoGetExpensesRows(expenses), nil
|
||||||
|
}
|
||||||
@@ -1,83 +1,84 @@
|
|||||||
package models
|
package repo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
repo "expenses/expenses/db/repository"
|
mod "expenses/expenses/models"
|
||||||
|
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||||
)
|
)
|
||||||
|
|
||||||
func mapRepoGetExpenseRow(e repo.GetExpenseRow) Expense {
|
func mapRepoGetExpenseRow(e pgsqlc.GetExpenseRow) mod.Expense {
|
||||||
value := pgNumericToDecimal(e.Expense.Value)
|
value := pgNumericToDecimal(e.Expense.Value)
|
||||||
paidOff := pgBoolToBool(e.Expense.PaidOff)
|
paidOff := pgBoolToBool(e.Expense.PaidOff)
|
||||||
sharesEven := pgBoolToBool(e.Expense.SharesEven)
|
sharesEven := pgBoolToBool(e.Expense.SharesEven)
|
||||||
expDate := pgTimestampToTime(e.Expense.ExpDate)
|
expDate := pgTimestampToTime(e.Expense.ExpDate)
|
||||||
creationDate := pgTimestampToTime(e.Expense.CreationDate)
|
creationDate := pgTimestampToTime(e.Expense.CreationDate)
|
||||||
|
|
||||||
return Expense{
|
return mod.Expense{
|
||||||
ExpID: e.Expense.ExpID,
|
ExpID: e.Expense.ExpID,
|
||||||
Description: e.Expense.Description,
|
Description: e.Expense.Description,
|
||||||
Value: value,
|
Value: value,
|
||||||
Store: Store{
|
Store: mod.Store{
|
||||||
StoreID: e.Store.StoreID,
|
StoreID: e.Store.StoreID,
|
||||||
StoreName: e.Store.StoreName,
|
StoreName: e.Store.StoreName,
|
||||||
},
|
},
|
||||||
Type: Type{
|
Type: mod.Type{
|
||||||
TypeID: e.ExpenseType.TypeID,
|
TypeID: e.ExpenseType.TypeID,
|
||||||
TypeName: e.ExpenseType.TypeName,
|
TypeName: e.ExpenseType.TypeName,
|
||||||
},
|
},
|
||||||
Category: Category{
|
Category: mod.Category{
|
||||||
CategoryID: e.Category.CategoryID,
|
CategoryID: e.Category.CategoryID,
|
||||||
CategoryName: e.Category.CategoryName,
|
CategoryName: e.Category.CategoryName,
|
||||||
},
|
},
|
||||||
Owner: User{
|
Owner: mod.User{
|
||||||
UserID: e.User.UserID,
|
UserID: e.User.UserID,
|
||||||
UserName: e.User.UserName,
|
UserName: e.User.UserName,
|
||||||
},
|
},
|
||||||
Date: expDate,
|
Date: expDate,
|
||||||
Shares: []Share{},
|
Shares: []mod.Share{},
|
||||||
Payments: []ExpensePayment{},
|
Payments: []mod.Payment{},
|
||||||
PaidOff: paidOff,
|
PaidOff: paidOff,
|
||||||
SharesEven: sharesEven,
|
SharesEven: sharesEven,
|
||||||
CreationDate: creationDate,
|
CreationDate: creationDate,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapRepoGetExpenseRowMulti(e repo.GetExpensesRow) Expense {
|
func mapRepoGetExpenseRowMulti(e pgsqlc.GetExpensesRow) mod.Expense {
|
||||||
value := pgNumericToDecimal(e.Expense.Value)
|
value := pgNumericToDecimal(e.Expense.Value)
|
||||||
paidOff := pgBoolToBool(e.Expense.PaidOff)
|
paidOff := pgBoolToBool(e.Expense.PaidOff)
|
||||||
sharesEven := pgBoolToBool(e.Expense.SharesEven)
|
sharesEven := pgBoolToBool(e.Expense.SharesEven)
|
||||||
expDate := pgTimestampToTime(e.Expense.ExpDate)
|
expDate := pgTimestampToTime(e.Expense.ExpDate)
|
||||||
creationDate := pgTimestampToTime(e.Expense.CreationDate)
|
creationDate := pgTimestampToTime(e.Expense.CreationDate)
|
||||||
|
|
||||||
return Expense{
|
return mod.Expense{
|
||||||
ExpID: e.Expense.ExpID,
|
ExpID: e.Expense.ExpID,
|
||||||
Description: e.Expense.Description,
|
Description: e.Expense.Description,
|
||||||
Value: value,
|
Value: value,
|
||||||
Store: Store{
|
Store: mod.Store{
|
||||||
StoreID: e.Store.StoreID,
|
StoreID: e.Store.StoreID,
|
||||||
StoreName: e.Store.StoreName,
|
StoreName: e.Store.StoreName,
|
||||||
},
|
},
|
||||||
Type: Type{
|
Type: mod.Type{
|
||||||
TypeID: e.ExpenseType.TypeID,
|
TypeID: e.ExpenseType.TypeID,
|
||||||
TypeName: e.ExpenseType.TypeName,
|
TypeName: e.ExpenseType.TypeName,
|
||||||
},
|
},
|
||||||
Category: Category{
|
Category: mod.Category{
|
||||||
CategoryID: e.Category.CategoryID,
|
CategoryID: e.Category.CategoryID,
|
||||||
CategoryName: e.Category.CategoryName,
|
CategoryName: e.Category.CategoryName,
|
||||||
},
|
},
|
||||||
Owner: User{
|
Owner: mod.User{
|
||||||
UserID: e.User.UserID,
|
UserID: e.User.UserID,
|
||||||
UserName: e.User.UserName,
|
UserName: e.User.UserName,
|
||||||
},
|
},
|
||||||
Date: expDate,
|
Date: expDate,
|
||||||
Shares: []Share{},
|
Shares: []mod.Share{},
|
||||||
Payments: []ExpensePayment{},
|
Payments: []mod.Payment{},
|
||||||
PaidOff: paidOff,
|
PaidOff: paidOff,
|
||||||
SharesEven: sharesEven,
|
SharesEven: sharesEven,
|
||||||
CreationDate: creationDate,
|
CreationDate: creationDate,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapRepoGetExpensesRows(er []repo.GetExpensesRow) []Expense {
|
func mapRepoGetExpensesRows(er []pgsqlc.GetExpensesRow) []mod.Expense {
|
||||||
expenses := make([]Expense, len(er))
|
expenses := make([]mod.Expense, len(er))
|
||||||
for k, exp := range er {
|
for k, exp := range er {
|
||||||
expenses[k] = mapRepoGetExpenseRowMulti(exp)
|
expenses[k] = mapRepoGetExpenseRowMulti(exp)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (p *PgExpRepo) insertShare(
|
||||||
|
ctx context.Context,
|
||||||
|
q *pgsqlc.Queries,
|
||||||
|
eId int32,
|
||||||
|
sh mod.Share) error {
|
||||||
|
|
||||||
|
share, err := decimalToNumeric(sh.Share)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
calculated, err := decimalToNumeric(sh.Calculated)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := q.InsertShare(ctx, pgsqlc.InsertShareParams{
|
||||||
|
ExpID: eId,
|
||||||
|
Share: share,
|
||||||
|
UserID: sh.User.UserID,
|
||||||
|
Calculated: calculated,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("no rows were created")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) InsertShare(
|
||||||
|
ctx context.Context,
|
||||||
|
eId int32,
|
||||||
|
sh mod.Share) error {
|
||||||
|
|
||||||
|
return p.insertShare(ctx, pgsqlc.New(p.DB), eId, sh)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) InsertShares(
|
||||||
|
ctx context.Context,
|
||||||
|
eId int32,
|
||||||
|
sh mod.Shares) error {
|
||||||
|
|
||||||
|
return p.insertShares(ctx, pgsqlc.New(p.DB), eId, sh)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PgExpRepo) insertShares(
|
||||||
|
ctx context.Context,
|
||||||
|
q *pgsqlc.Queries,
|
||||||
|
eId int32,
|
||||||
|
sh []mod.Share) error {
|
||||||
|
|
||||||
|
for k := range sh {
|
||||||
|
err := p.insertShare(ctx, q, eId, sh[k])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) updateShare(
|
||||||
|
ctx context.Context, q *pgsqlc.Queries, sh mod.Share) error {
|
||||||
|
|
||||||
|
share, err := decimalToNumeric(sh.Share)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
calculated, err := decimalToNumeric(sh.Calculated)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := q.UpdateShare(ctx, pgsqlc.UpdateShareParams{
|
||||||
|
ExpShareID: sh.ExpShareID,
|
||||||
|
Share: share,
|
||||||
|
UserID: sh.User.UserID,
|
||||||
|
Calculated: calculated,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("no rows were updated")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) UpdateShare(ctx context.Context, sh mod.Share) error {
|
||||||
|
return p.updateShare(ctx, pgsqlc.New(p.DB), sh)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PgExpRepo) deleteShare(
|
||||||
|
ctx context.Context, q *pgsqlc.Queries, sh mod.Share) error {
|
||||||
|
|
||||||
|
res, err := q.DeleteShare(ctx, sh.ExpShareID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("no rows were deleted")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) DeleteShare(ctx context.Context, sh mod.Share) error {
|
||||||
|
return p.deleteShare(ctx, pgsqlc.New(p.DB), sh)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PgExpRepo) getShares(
|
||||||
|
ctx context.Context, q *pgsqlc.Queries, eId int32) (mod.Shares, error) {
|
||||||
|
|
||||||
|
shares, err := q.GetShares(ctx, eId)
|
||||||
|
if err != nil {
|
||||||
|
return mod.Shares{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mapRepoGetSharesRows(shares), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgExpRepo) GetShares(ctx context.Context, eId int32) (mod.Shares, error) {
|
||||||
|
return p.getShares(ctx, pgsqlc.New(p.DB), eId)
|
||||||
|
}
|
||||||
+8
-7
@@ -1,17 +1,18 @@
|
|||||||
package models
|
package repo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
repo "expenses/expenses/db/repository"
|
mod "expenses/expenses/models"
|
||||||
|
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||||
)
|
)
|
||||||
|
|
||||||
func mapRepoGetSharesRow(s repo.GetSharesRow) Share {
|
func mapRepoGetSharesRow(s pgsqlc.GetSharesRow) mod.Share {
|
||||||
share := pgNumericToDecimal(s.ExpensesShare.Share)
|
share := pgNumericToDecimal(s.ExpensesShare.Share)
|
||||||
calculated := pgNumericToDecimal(s.ExpensesShare.Calculated)
|
calculated := pgNumericToDecimal(s.ExpensesShare.Calculated)
|
||||||
|
|
||||||
return Share{
|
return mod.Share{
|
||||||
ExpShareID: s.ExpensesShare.ExpShareID,
|
ExpShareID: s.ExpensesShare.ExpShareID,
|
||||||
Share: share,
|
Share: share,
|
||||||
User: User{
|
User: mod.User{
|
||||||
UserID: s.User.UserID,
|
UserID: s.User.UserID,
|
||||||
UserName: s.User.UserName,
|
UserName: s.User.UserName,
|
||||||
},
|
},
|
||||||
@@ -19,8 +20,8 @@ func mapRepoGetSharesRow(s repo.GetSharesRow) Share {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapRepoGetSharesRows(es []repo.GetSharesRow) []Share {
|
func mapRepoGetSharesRows(es []pgsqlc.GetSharesRow) mod.Shares {
|
||||||
shares := make([]Share, len(es))
|
shares := make(mod.Shares, len(es))
|
||||||
for k, exp := range es {
|
for k, exp := range es {
|
||||||
shares[k] = mapRepoGetSharesRow(exp)
|
shares[k] = mapRepoGetSharesRow(exp)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
experr "expenses/expenses/errors"
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||||
|
"fmt"
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PgStoreRepo struct {
|
||||||
|
DB *pgxpool.Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPgStoreRepo(db *pgxpool.Pool) StoreRepository {
|
||||||
|
return PgStoreRepo{
|
||||||
|
DB: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgStoreRepo) Get(ctx context.Context, id int32) (mod.Store, error) {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
store, err := queries.GetStore(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return mod.Store{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mapRepoStore(store), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgStoreRepo) GetAll(ctx context.Context) (mod.Stores, error) {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
storeList, err := queries.GetStores(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return mod.Stores{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mapRepoStores(storeList), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgStoreRepo) Update(ctx context.Context, s mod.Store) error {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
_, err := queries.UpdateStore(ctx, pgsqlc.UpdateStoreParams{
|
||||||
|
StoreID: s.StoreID,
|
||||||
|
StoreName: s.StoreName,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgStoreRepo) Insert(ctx context.Context, s mod.Store) error {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
res, err := queries.InsertStore(ctx, s.StoreName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("no rows were created")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgStoreRepo) Delete(ctx context.Context, id int32) error {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
res, err := queries.DeleteStore(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return experr.ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func mapRepoStore(rs pgsqlc.Store) mod.Store {
|
||||||
|
return mod.Store{
|
||||||
|
StoreID: rs.StoreID,
|
||||||
|
StoreName: rs.StoreName,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mapRepoStores(rs []pgsqlc.Store) mod.Stores {
|
||||||
|
stores := make(mod.Stores, len(rs))
|
||||||
|
for k, store := range rs {
|
||||||
|
stores[k] = mapRepoStore(store)
|
||||||
|
}
|
||||||
|
return stores
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
experr "expenses/expenses/errors"
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||||
|
"fmt"
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PgTypeRepo struct {
|
||||||
|
DB *pgxpool.Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPgTypeRepo(db *pgxpool.Pool) TypeRepository {
|
||||||
|
return PgTypeRepo{
|
||||||
|
DB: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgTypeRepo) Get(ctx context.Context, id int32) (mod.Type, error) {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
typ, err := queries.GetType(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return mod.Type{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mapRepoType(typ), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgTypeRepo) GetAll(ctx context.Context) (mod.Types, error) {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
typeList, err := queries.GetTypes(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return mod.Types{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mapRepoTypes(typeList), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgTypeRepo) Update(ctx context.Context, typ mod.Type) error {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
res, err := queries.UpdateType(ctx, pgsqlc.UpdateTypeParams{
|
||||||
|
TypeID: typ.TypeID,
|
||||||
|
TypeName: typ.TypeName,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return experr.ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgTypeRepo) Insert(ctx context.Context, typ mod.Type) error {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
res, err := queries.InsertType(ctx, typ.TypeName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
// Move all theses things outside of this, I'll handle it wherever I'm doing
|
||||||
|
// logics
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return fmt.Errorf("no rows were created")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgTypeRepo) Delete(ctx context.Context, id int32) error {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
res, err := queries.DeleteType(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rowsAffected := res.RowsAffected()
|
||||||
|
if rowsAffected == 0 {
|
||||||
|
return experr.ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func mapRepoType(rt pgsqlc.ExpenseType) mod.Type {
|
||||||
|
return mod.Type{
|
||||||
|
TypeID: rt.TypeID,
|
||||||
|
TypeName: rt.TypeName,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mapRepoTypes(rt []pgsqlc.ExpenseType) mod.Types {
|
||||||
|
types := make(mod.Types, len(rt))
|
||||||
|
for k, typ := range rt {
|
||||||
|
types[k] = mapRepoType(typ)
|
||||||
|
}
|
||||||
|
return types
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PgUserRepo struct {
|
||||||
|
DB *pgxpool.Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPgUserRepo(db *pgxpool.Pool) UserRepository {
|
||||||
|
return PgUserRepo{
|
||||||
|
DB: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PgUserRepo) GetAll(ctx context.Context) (mod.Users, error) {
|
||||||
|
queries := pgsqlc.New(p.DB)
|
||||||
|
userList, err := queries.GetUsers(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return mod.Users{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return mapRepoUsers(userList), nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func mapRepoUser(ru pgsqlc.User) mod.User {
|
||||||
|
return mod.User{
|
||||||
|
UserID: ru.UserID,
|
||||||
|
UserName: ru.UserName,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mapRepoUsers(ru []pgsqlc.User) mod.Users {
|
||||||
|
users := make(mod.Users, len(ru))
|
||||||
|
for k, u := range ru {
|
||||||
|
users[k] = mapRepoUser(u)
|
||||||
|
}
|
||||||
|
return users
|
||||||
|
}
|
||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
// sqlc v1.29.0
|
// sqlc v1.29.0
|
||||||
// source: categories.sql
|
// source: categories.sql
|
||||||
|
|
||||||
package repository
|
package pgsqlc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.29.0
|
// sqlc v1.29.0
|
||||||
|
|
||||||
package repository
|
package pgsqlc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
// sqlc v1.29.0
|
// sqlc v1.29.0
|
||||||
// source: expenses.sql
|
// source: expenses.sql
|
||||||
|
|
||||||
package repository
|
package pgsqlc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.29.0
|
// sqlc v1.29.0
|
||||||
|
|
||||||
package repository
|
package pgsqlc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
// sqlc v1.29.0
|
// sqlc v1.29.0
|
||||||
// source: payments.sql
|
// source: payments.sql
|
||||||
|
|
||||||
package repository
|
package pgsqlc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
// sqlc v1.29.0
|
// sqlc v1.29.0
|
||||||
// source: shares.sql
|
// source: shares.sql
|
||||||
|
|
||||||
package repository
|
package pgsqlc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
// sqlc v1.29.0
|
// sqlc v1.29.0
|
||||||
// source: stores.sql
|
// source: stores.sql
|
||||||
|
|
||||||
package repository
|
package pgsqlc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
// sqlc v1.29.0
|
// sqlc v1.29.0
|
||||||
// source: types.sql
|
// source: types.sql
|
||||||
|
|
||||||
package repository
|
package pgsqlc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
// sqlc v1.29.0
|
// sqlc v1.29.0
|
||||||
// source: users.sql
|
// source: users.sql
|
||||||
|
|
||||||
package repository
|
package pgsqlc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -6,8 +6,8 @@ sql:
|
|||||||
gen:
|
gen:
|
||||||
go:
|
go:
|
||||||
sql_package: "pgx/v5"
|
sql_package: "pgx/v5"
|
||||||
package: "repository"
|
package: "pgsqlc"
|
||||||
out: "repository"
|
out: "pgsqlc"
|
||||||
rename:
|
rename:
|
||||||
expenseTypes: "ExpenseTypes"
|
expenseTypes: "ExpenseTypes"
|
||||||
expenseShares: "ExpenseShares"
|
expenseShares: "ExpenseShares"
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type StoreRepository interface {
|
||||||
|
Get(ctx context.Context, id int32) (mod.Store, error)
|
||||||
|
GetAll(ctx context.Context) (mod.Stores, error)
|
||||||
|
Update(ctx context.Context, cat mod.Store) error
|
||||||
|
Insert(ctx context.Context, cat mod.Store) error
|
||||||
|
Delete(ctx context.Context, id int32) error
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TypeRepository interface {
|
||||||
|
Get(ctx context.Context, id int32) (mod.Type, error)
|
||||||
|
GetAll(ctx context.Context) (mod.Types, error)
|
||||||
|
Update(ctx context.Context, typ mod.Type) error
|
||||||
|
Insert(ctx context.Context, typ mod.Type) error
|
||||||
|
Delete(ctx context.Context, id int32) error
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
mod "expenses/expenses/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UserRepository interface {
|
||||||
|
GetAll(ctx context.Context) (mod.Users, error)
|
||||||
|
}
|
||||||
@@ -30,6 +30,7 @@ require (
|
|||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||||
|
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||||
github.com/leodido/go-urn v1.4.0 // indirect
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
@@ -45,6 +46,7 @@ require (
|
|||||||
golang.org/x/arch v0.8.0 // indirect
|
golang.org/x/arch v0.8.0 // indirect
|
||||||
golang.org/x/crypto v0.36.0 // indirect
|
golang.org/x/crypto v0.36.0 // indirect
|
||||||
golang.org/x/net v0.38.0 // indirect
|
golang.org/x/net v0.38.0 // indirect
|
||||||
|
golang.org/x/sync v0.12.0 // indirect
|
||||||
golang.org/x/sys v0.31.0 // indirect
|
golang.org/x/sys v0.31.0 // indirect
|
||||||
golang.org/x/text v0.23.0 // indirect
|
golang.org/x/text v0.23.0 // indirect
|
||||||
google.golang.org/protobuf v1.34.2 // indirect
|
google.golang.org/protobuf v1.34.2 // indirect
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ func processFormShares(c *gin.Context) ([]mod.Share, error) {
|
|||||||
return expenses.ParseFormShares(userIDS, shares, shareIDs)
|
return expenses.ParseFormShares(userIDS, shares, shareIDs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func processFormPayments(c *gin.Context) ([]mod.ExpensePayment, error) {
|
func processFormPayments(c *gin.Context) ([]mod.Payment, error) {
|
||||||
userIDs := c.PostFormArray("payments-user-ids[]")
|
userIDs := c.PostFormArray("payments-user-ids[]")
|
||||||
paymentIDs := c.PostFormArray("payments-ids[]")
|
paymentIDs := c.PostFormArray("payments-ids[]")
|
||||||
values := c.PostFormArray("payments-payment[]")
|
values := c.PostFormArray("payments-payment[]")
|
||||||
|
|||||||
Reference in New Issue
Block a user