Just renamed some stuff
This commit is contained in:
+2
-2
@@ -34,5 +34,5 @@ test:
|
||||
build_db_image:
|
||||
make -C ./pgImage build
|
||||
|
||||
my-test:
|
||||
@echo hello
|
||||
test_db:
|
||||
make -C ./pgImage test
|
||||
|
||||
@@ -2,9 +2,10 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
exp "expenses/expenses"
|
||||
"net/http"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
||||
@@ -2,9 +2,10 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
exp "expenses/expenses"
|
||||
"net/http"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
||||
@@ -2,9 +2,10 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
exp "expenses/expenses"
|
||||
"net/http"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
||||
+2
-1
@@ -2,9 +2,10 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
exp "expenses/expenses"
|
||||
"net/http"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
||||
+2
-1
@@ -2,9 +2,10 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
exp "expenses/expenses"
|
||||
"net/http"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
expauth "expenses/expenses/auth"
|
||||
expauth "github.com/ESilva15/expenses/expenses/auth"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
+5
-3
@@ -1,10 +1,12 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
exp "expenses/expenses"
|
||||
"expenses/expenses/auth"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
|
||||
"github.com/ESilva15/expenses/expenses/auth"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type LoginData struct {
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
<table>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Desc</th>
|
||||
<th>Price</th>
|
||||
<th>Date</th>
|
||||
<th>OWNER</th>
|
||||
<th>DESC</th>
|
||||
<th>PRICE</th>
|
||||
<th>DATE</th>
|
||||
<!-- <th>Store</th> -->
|
||||
<th>Category</th>
|
||||
<!-- <th>Owner</th> -->
|
||||
@@ -12,9 +13,10 @@
|
||||
{{ range .expenses }}
|
||||
<tr>
|
||||
<th><a href="/expenses/{{ .ExpID }}">{{ .ExpID }}</th>
|
||||
<th spellcheck="false" contenteditable>{{ .Description }}</th>
|
||||
<th spellcheck="false" contenteditable>{{ formatPrice .Value }}</th>
|
||||
<th spellcheck="false" contenteditable>{{ formatDate .Date }}</th>
|
||||
<th spellcheck="false">{{ .Owner.UserName }}</th>
|
||||
<th spellcheck="false">{{ .Description }}</th>
|
||||
<th spellcheck="false">{{ formatPrice .Value }}</th>
|
||||
<th spellcheck="false">{{ formatDate .Date }}</th>
|
||||
<th spellcheck="false">{{ .Category.CategoryName }}</th>
|
||||
</tr>
|
||||
{{ end }}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"expenses/expenses"
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"github.com/ESilva15/expenses/expenses"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
||||
+2
-1
@@ -1,9 +1,10 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
exp "expenses/expenses"
|
||||
"log"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
||||
+4
-4
@@ -1,10 +1,10 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"expenses/api"
|
||||
"expenses/config"
|
||||
"expenses/expenses/auth"
|
||||
"expenses/pages"
|
||||
"github.com/ESilva15/expenses/api"
|
||||
"github.com/ESilva15/expenses/config"
|
||||
"github.com/ESilva15/expenses/expenses/auth"
|
||||
"github.com/ESilva15/expenses/pages"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-contrib/sessions/cookie"
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
// Package config defines the configuration of our app
|
||||
package config
|
||||
|
||||
import (
|
||||
"gopkg.in/yaml.v2"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// PostgresConfig defines the data required to connect to Postgres.
|
||||
type PostgresConfig struct {
|
||||
Host string `yaml:"host"`
|
||||
Port string `yaml:"port"`
|
||||
@@ -15,6 +18,7 @@ type PostgresConfig struct {
|
||||
Pass string `yaml:"pass"`
|
||||
}
|
||||
|
||||
// Configuration defines the base configuration of the expenses app
|
||||
type Configuration struct {
|
||||
AssetsDir string `yaml:"assets"`
|
||||
Port string `yaml:"port"`
|
||||
@@ -29,6 +33,7 @@ var (
|
||||
confPath string
|
||||
)
|
||||
|
||||
// GetInstance returns the instance of the configuration of the app.
|
||||
func GetInstance() *Configuration {
|
||||
once.Do(func() {
|
||||
instance = &Configuration{}
|
||||
@@ -38,6 +43,7 @@ func GetInstance() *Configuration {
|
||||
return instance
|
||||
}
|
||||
|
||||
// SetConfig sets the path for the configuration file.
|
||||
func SetConfig(path string) {
|
||||
confPath = path
|
||||
}
|
||||
|
||||
+2
-2
@@ -6,8 +6,8 @@ import (
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"expenses/config"
|
||||
"expenses/expenses/repo"
|
||||
"github.com/ESilva15/expenses/config"
|
||||
"github.com/ESilva15/expenses/expenses/repo"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
exp "expenses/expenses"
|
||||
mod "expenses/expenses/models"
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
)
|
||||
|
||||
// AuthMiddleware is the middleware we will use to verify if a user is
|
||||
|
||||
@@ -2,7 +2,8 @@ package expenses
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
)
|
||||
|
||||
// GetAllCategories returns all categories.
|
||||
|
||||
@@ -2,9 +2,11 @@ package expenses
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
"log"
|
||||
"sort"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
@@ -139,11 +141,14 @@ func resolveDebts(debtors userTabs, creditors userTabs) mod.Debts {
|
||||
return debts
|
||||
}
|
||||
|
||||
// TODO
|
||||
// Are we sure there are no errors in here?
|
||||
// TODO - Are we sure there are no errors in here?
|
||||
func CalculateDebts(e *mod.Expense) (mod.Debts, error) {
|
||||
debtors, creditors := filterExpenseParticipants(e)
|
||||
log.Println("Debtors:", debtors)
|
||||
log.Println("Creditors:", creditors)
|
||||
|
||||
debts := resolveDebts(debtors, creditors)
|
||||
log.Println("Debts:", debts)
|
||||
|
||||
return debts, nil
|
||||
}
|
||||
|
||||
@@ -1,24 +1,18 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type DebtCalculator struct {
|
||||
Shares map[mod.User]decimal.Decimal
|
||||
Payments map[mod.User]decimal.Decimal
|
||||
Expense *mod.Expense
|
||||
}
|
||||
|
||||
func NewDebtCalculator(e *mod.Expense) *DebtCalculator {
|
||||
return &DebtCalculator{
|
||||
Shares: make(map[mod.User]decimal.Decimal),
|
||||
Payments: make(map[mod.User]decimal.Decimal),
|
||||
Expense: e,
|
||||
}
|
||||
}
|
||||
// type DebtCalculator struct {
|
||||
// Shares map[mod.User]decimal.Decimal
|
||||
// Payments map[mod.User]decimal.Decimal
|
||||
// Expense *mod.Expense
|
||||
// }
|
||||
//
|
||||
// func NewDebtCalculator(e *mod.Expense) *DebtCalculator {
|
||||
// return &DebtCalculator{
|
||||
// Shares: make(map[mod.User]decimal.Decimal),
|
||||
// Payments: make(map[mod.User]decimal.Decimal),
|
||||
// Expense: e,
|
||||
// }
|
||||
// }
|
||||
|
||||
// TODO
|
||||
// This code will be replicated in the expenses controller but without
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
"testing"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
@@ -165,12 +164,18 @@ var (
|
||||
},
|
||||
Value: dec.NewFromFloat(111.11),
|
||||
Shares: []mod.Share{
|
||||
{ExpShareID: 0, User: user1,
|
||||
Share: dec.NewFromFloat(0.34), Calculated: dec.NewFromFloat(37.79)},
|
||||
{ExpShareID: 1, User: user2,
|
||||
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(36.66)},
|
||||
{ExpShareID: 2, User: user3,
|
||||
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(36.66)},
|
||||
{
|
||||
ExpShareID: 0, User: user1,
|
||||
Share: dec.NewFromFloat(0.34), Calculated: dec.NewFromFloat(37.79),
|
||||
},
|
||||
{
|
||||
ExpShareID: 1, User: user2,
|
||||
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(36.66),
|
||||
},
|
||||
{
|
||||
ExpShareID: 2, User: user3,
|
||||
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(36.66),
|
||||
},
|
||||
},
|
||||
Payments: []mod.Payment{
|
||||
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromFloat(37.79)},
|
||||
@@ -183,12 +188,18 @@ var (
|
||||
ExpID: 252,
|
||||
Value: dec.NewFromFloat(0.50),
|
||||
Shares: []mod.Share{
|
||||
{ExpShareID: 0, User: user1,
|
||||
Share: dec.NewFromFloat(0.34), Calculated: dec.NewFromFloat(0.18)},
|
||||
{ExpShareID: 1, User: user2,
|
||||
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(0.16)},
|
||||
{ExpShareID: 2, User: user3,
|
||||
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(0.16)},
|
||||
{
|
||||
ExpShareID: 0, User: user1,
|
||||
Share: dec.NewFromFloat(0.34), Calculated: dec.NewFromFloat(0.18),
|
||||
},
|
||||
{
|
||||
ExpShareID: 1, User: user2,
|
||||
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(0.16),
|
||||
},
|
||||
{
|
||||
ExpShareID: 2, User: user3,
|
||||
Share: dec.NewFromFloat(0.33), Calculated: dec.NewFromFloat(0.16),
|
||||
},
|
||||
},
|
||||
Payments: []mod.Payment{
|
||||
{ExpPaymID: 0, User: user1, PayedAmount: dec.NewFromFloat(0.50)},
|
||||
@@ -198,33 +209,33 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
func TestSharesAndPaymentsMapping(t *testing.T) {
|
||||
// expectedPayments := map[mod.User]float32{
|
||||
// user1: 100,
|
||||
// user2: 0,
|
||||
// user3: 60,
|
||||
// }
|
||||
//
|
||||
// expectedShares := map[mod.User]float32{
|
||||
// user1: 0.4,
|
||||
// user2: 0.4,
|
||||
// user3: 0.2,
|
||||
// }
|
||||
//
|
||||
// dc := NewDebtCalculator(&expense1)
|
||||
// dc.mapShares()
|
||||
// dc.mapPayments()
|
||||
//
|
||||
// if !reflect.DeepEqual(expectedPayments, dc.Payments) {
|
||||
// t.Errorf("expected payments total and result is different:\n%+v\n%+v\n",
|
||||
// expectedPayments, dc.Payments)
|
||||
// }
|
||||
//
|
||||
// if !reflect.DeepEqual(expectedShares, dc.Shares) {
|
||||
// t.Errorf("expected user shares and result is different:\n%+v\n%+v\n",
|
||||
// expectedShares, dc.Shares)
|
||||
// }
|
||||
}
|
||||
// func TestSharesAndPaymentsMapping(t *testing.T) {
|
||||
// expectedPayments := map[mod.User]float32{
|
||||
// user1: 100,
|
||||
// user2: 0,
|
||||
// user3: 60,
|
||||
// }
|
||||
//
|
||||
// expectedShares := map[mod.User]float32{
|
||||
// user1: 0.4,
|
||||
// user2: 0.4,
|
||||
// user3: 0.2,
|
||||
// }
|
||||
//
|
||||
// dc := NewDebtCalculator(&expense1)
|
||||
// dc.mapShares()
|
||||
// dc.mapPayments()
|
||||
//
|
||||
// if !reflect.DeepEqual(expectedPayments, dc.Payments) {
|
||||
// t.Errorf("expected payments total and result is different:\n%+v\n%+v\n",
|
||||
// expectedPayments, dc.Payments)
|
||||
// }
|
||||
//
|
||||
// if !reflect.DeepEqual(expectedShares, dc.Shares) {
|
||||
// t.Errorf("expected user shares and result is different:\n%+v\n%+v\n",
|
||||
// expectedShares, dc.Shares)
|
||||
// }
|
||||
// }
|
||||
|
||||
// func TestFilterExpenseParticipants(t *testing.T) {
|
||||
// expectedDebtors := []Debt{
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
|
||||
@@ -2,7 +2,8 @@ package expenses
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
|
||||
@@ -2,7 +2,8 @@ package expenses
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
)
|
||||
|
||||
type UserExpenseSummary struct {
|
||||
@@ -19,21 +19,21 @@ func NewUserExpenseSummary() UserExpenseSummary {
|
||||
}
|
||||
|
||||
// func (e *Expense) GetSummary() ([]UserExpenseSummary, error) {
|
||||
// summary := []UserExpenseSummary{}
|
||||
//
|
||||
// for _, share := range e.Shares {
|
||||
// curSummary := NewUserExpenseSummary()
|
||||
// curSummary.User = share.User
|
||||
// curSummary.Share = share.Share
|
||||
//
|
||||
// for _, payment := range e.Payments {
|
||||
// if payment.User.UserID == curSummary.User.UserID {
|
||||
// curSummary.PayedTotal += payment.PayedAmount
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// summary = append(summary, curSummary)
|
||||
// }
|
||||
//
|
||||
// return summary, nil
|
||||
// summary := []UserExpenseSummary{}
|
||||
//
|
||||
// for _, share := range e.Shares {
|
||||
// curSummary := NewUserExpenseSummary()
|
||||
// curSummary.User = share.User
|
||||
// curSummary.Share = share.Share
|
||||
//
|
||||
// for _, payment := range e.Payments {
|
||||
// if payment.User.UserID == curSummary.User.UserID {
|
||||
// curSummary.PayedTotal += payment.PayedAmount
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// summary = append(summary, curSummary)
|
||||
// }
|
||||
//
|
||||
// return summary, nil
|
||||
// }
|
||||
|
||||
@@ -2,10 +2,11 @@ package expenses
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
|
||||
@@ -3,7 +3,8 @@ package expenses
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
"expenses/luadec"
|
||||
"github.com/ESilva15/expenses/luadec"
|
||||
|
||||
ljson "github.com/layeh/gopher-json"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
|
||||
@@ -2,9 +2,11 @@ package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
)
|
||||
|
||||
// CategoryRepository defines the repository for the category data model.
|
||||
type CategoryRepository interface {
|
||||
Get(ctx context.Context, id int32) (mod.Category, error)
|
||||
GetAll(ctx context.Context) (mod.Categories, error)
|
||||
@@ -12,5 +14,5 @@ type CategoryRepository interface {
|
||||
Insert(ctx context.Context, cat mod.Category) error
|
||||
Delete(ctx context.Context, id int32) error
|
||||
|
||||
Close()
|
||||
Close()
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
// Package repo defines the repositories for the expenses app
|
||||
package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
"time"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
)
|
||||
|
||||
// ExpenseRepository defines the repository for the expense data model.
|
||||
type ExpenseRepository interface {
|
||||
// Direct expense methods
|
||||
Get(ctx context.Context, id int32) (mod.Expense, error)
|
||||
|
||||
@@ -2,6 +2,8 @@ package repo
|
||||
|
||||
import lua "github.com/yuin/gopher-lua"
|
||||
|
||||
// Migrator defines the interface for the migration procedures of different
|
||||
// data storage.
|
||||
type Migrator interface {
|
||||
Goto(id uint) error
|
||||
RunMigrations(lua *lua.LState) error
|
||||
|
||||
@@ -2,42 +2,57 @@ package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
experr "expenses/expenses/errors"
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||
"fmt"
|
||||
|
||||
experr "github.com/ESilva15/expenses/expenses/errors"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
"github.com/ESilva15/expenses/expenses/repo/pgdb/pgsqlc"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// PgCatRepo is the PG repository for categories.
|
||||
type PgCatRepo struct {
|
||||
DB *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewPgCatRepo(db *pgxpool.Pool) CategoryRepository {
|
||||
// NewPgCatRepo returns a new PG repository for categories.
|
||||
func NewPgCatRepo(db *pgxpool.Pool) PgCatRepo {
|
||||
return PgCatRepo{
|
||||
DB: db,
|
||||
}
|
||||
}
|
||||
|
||||
// Close closes a PgCatRepo.
|
||||
func (p PgCatRepo) Close() {
|
||||
p.DB.Close()
|
||||
}
|
||||
|
||||
// Get fetches a category by its id
|
||||
//
|
||||
// Returns an empty mod.Category and error if it fails.
|
||||
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
|
||||
|
||||
return mapRepoCategory(category), err
|
||||
}
|
||||
|
||||
// GetAll fetches all categories.
|
||||
//
|
||||
// Returns an empty mod.Categories if it fails and an error.
|
||||
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 mod.Categories{}, err
|
||||
}
|
||||
return MapRepoCategories(categories), nil
|
||||
|
||||
return mapRepoCategories(categories), nil
|
||||
}
|
||||
|
||||
// Update updates the parameter cat
|
||||
//
|
||||
// Returns an error if it fails.
|
||||
func (p PgCatRepo) Update(ctx context.Context, cat mod.Category) error {
|
||||
queries := pgsqlc.New(p.DB)
|
||||
res, err := queries.UpdateCategory(ctx, pgsqlc.UpdateCategoryParams{
|
||||
@@ -56,6 +71,9 @@ func (p PgCatRepo) Update(ctx context.Context, cat mod.Category) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Insert inserts a new category
|
||||
//
|
||||
// Returns an error if it fails.
|
||||
func (p PgCatRepo) Insert(ctx context.Context, cat mod.Category) error {
|
||||
queries := pgsqlc.New(p.DB)
|
||||
res, err := queries.InsertCategory(ctx, cat.CategoryName)
|
||||
@@ -71,6 +89,9 @@ func (p PgCatRepo) Insert(ctx context.Context, cat mod.Category) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete deletes a category by ID
|
||||
//
|
||||
// Returns an error if it fails.
|
||||
func (p PgCatRepo) Delete(ctx context.Context, id int32) error {
|
||||
queries := pgsqlc.New(p.DB)
|
||||
res, err := queries.DeleteCategory(ctx, id)
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
pgsqlc "expenses/expenses/repo/pgdb/pgsqlc"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
pgsqlc "github.com/ESilva15/expenses/expenses/repo/pgdb/pgsqlc"
|
||||
)
|
||||
|
||||
func MapRepoCategory(rc pgsqlc.Category) mod.Category {
|
||||
func mapRepoCategory(rc pgsqlc.Category) mod.Category {
|
||||
return mod.Category{
|
||||
CategoryID: rc.CategoryID,
|
||||
CategoryName: rc.CategoryName,
|
||||
}
|
||||
}
|
||||
|
||||
func MapRepoCategories(rc []pgsqlc.Category) mod.Categories {
|
||||
func mapRepoCategories(rc []pgsqlc.Category) mod.Categories {
|
||||
categories := make(mod.Categories, len(rc))
|
||||
for k, cat := range rc {
|
||||
categories[k] = MapRepoCategory(cat)
|
||||
categories[k] = mapRepoCategory(cat)
|
||||
}
|
||||
|
||||
return categories
|
||||
}
|
||||
|
||||
@@ -2,15 +2,19 @@ package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
"github.com/ESilva15/expenses/expenses/repo/pgdb/pgsqlc"
|
||||
)
|
||||
|
||||
// SettleDebt will update the PG storage with a debt settlement.
|
||||
//
|
||||
// Returns an error if it fails.
|
||||
func (p PgExpRepo) SettleDebt(
|
||||
ctx context.Context, eId int32, payment mod.Payment, credit mod.Payment) error {
|
||||
|
||||
ctx context.Context, eID int32, payment mod.Payment, credit mod.Payment,
|
||||
) error {
|
||||
return withTx(p.DB, ctx, func(q *pgsqlc.Queries) error {
|
||||
err := p.insertPayment(ctx, q, eId, payment)
|
||||
err := p.insertPayment(ctx, q, eID, payment)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -2,22 +2,24 @@ package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||
"fmt"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
"github.com/ESilva15/expenses/expenses/repo/pgdb/pgsqlc"
|
||||
)
|
||||
|
||||
func (p PgExpRepo) insertPayment(
|
||||
ctx context.Context, q *pgsqlc.Queries, eId int32, pm mod.Payment) error {
|
||||
payed, err := decimalToNumeric(pm.PayedAmount)
|
||||
ctx context.Context, q *pgsqlc.Queries, eID int32, pm mod.Payment,
|
||||
) error {
|
||||
paid, err := decimalToNumeric(pm.PayedAmount)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res, err := q.InsertPayment(ctx, pgsqlc.InsertPaymentParams{
|
||||
ExpID: eId,
|
||||
ExpID: eID,
|
||||
UserID: pm.User.UserID,
|
||||
Payed: payed,
|
||||
Payed: paid,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
"github.com/ESilva15/expenses/expenses/repo/pgdb/pgsqlc"
|
||||
)
|
||||
|
||||
func mapRepoGetPaymentsRow(s pgsqlc.GetPaymentsRow) mod.Payment {
|
||||
|
||||
@@ -2,11 +2,12 @@ package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
"github.com/ESilva15/expenses/expenses/repo/pgdb/pgsqlc"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
@@ -65,8 +66,9 @@ func (p *PgExpRepo) getAll(
|
||||
return mapRepoGetExpensesRows(expenses), err
|
||||
}
|
||||
|
||||
func (p PgExpRepo) GetAll(ctx context.Context, uId int32) (mod.Expenses, error) {
|
||||
return p.getAll(ctx, pgsqlc.New(p.DB), uId)
|
||||
// GetAll fetches all expenses.
|
||||
func (p PgExpRepo) GetAll(ctx context.Context, uID int32) (mod.Expenses, error) {
|
||||
return p.getAll(ctx, pgsqlc.New(p.DB), uID)
|
||||
}
|
||||
|
||||
func (p *PgExpRepo) get(ctx context.Context, q *pgsqlc.Queries, id int32) (mod.Expense, error) {
|
||||
@@ -74,13 +76,16 @@ func (p *PgExpRepo) get(ctx context.Context, q *pgsqlc.Queries, id int32) (mod.E
|
||||
if err != nil {
|
||||
return mod.Expense{}, err
|
||||
}
|
||||
return mapRepoGetExpenseRow(expense), nil
|
||||
|
||||
return mapRepoExpenseRow(ExpenseRowSingle(expense)), nil
|
||||
}
|
||||
|
||||
// Get fetches a single expense by its ID.
|
||||
func (p PgExpRepo) Get(ctx context.Context, id int32) (mod.Expense, error) {
|
||||
return p.get(ctx, pgsqlc.New(p.DB), id)
|
||||
}
|
||||
|
||||
// Update updates a given expense.
|
||||
func (p PgExpRepo) Update(
|
||||
ctx context.Context,
|
||||
exp mod.Expense) error {
|
||||
@@ -146,7 +151,7 @@ func (p PgExpRepo) Update(
|
||||
} else {
|
||||
err := p.updatePayment(ctx, q, paym)
|
||||
if err != nil {
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -223,6 +228,7 @@ func (p PgExpRepo) Insert(
|
||||
})
|
||||
}
|
||||
|
||||
// Delete deletes a given expense by its ID.
|
||||
func (p PgExpRepo) Delete(ctx context.Context, id int32) error {
|
||||
queries := pgsqlc.New(p.DB)
|
||||
res, err := queries.DeleteExpense(ctx, id)
|
||||
@@ -238,11 +244,12 @@ func (p PgExpRepo) Delete(ctx context.Context, id int32) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetExpensesRange fetches all expenses in a given time frame.
|
||||
func (p PgExpRepo) GetExpensesRange(
|
||||
ctx context.Context,
|
||||
start time.Time,
|
||||
end time.Time,
|
||||
uId int32) (mod.Expenses, error) {
|
||||
uID int32) (mod.Expenses, error) {
|
||||
|
||||
startPg, err := timeToTimestamp(start)
|
||||
if err != nil {
|
||||
@@ -258,7 +265,7 @@ func (p PgExpRepo) GetExpensesRange(
|
||||
expenses, err := queries.GetExpenses(ctx, pgsqlc.GetExpensesParams{
|
||||
Startdate: startPg,
|
||||
Enddate: endPg,
|
||||
UserID: uId,
|
||||
UserID: uID,
|
||||
})
|
||||
if err != nil {
|
||||
return mod.Expenses{}, err
|
||||
|
||||
@@ -1,80 +1,69 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
"github.com/ESilva15/expenses/expenses/repo/pgdb/pgsqlc"
|
||||
)
|
||||
|
||||
func mapRepoGetExpenseRow(e pgsqlc.GetExpenseRow) mod.Expense {
|
||||
value := pgNumericToDecimal(e.Expense.Value)
|
||||
paidOff := pgBoolToBool(e.Expense.PaidOff)
|
||||
sharesEven := pgBoolToBool(e.Expense.SharesEven)
|
||||
expDate := pgTimestampToTime(e.Expense.ExpDate)
|
||||
creationDate := pgTimestampToTime(e.Expense.CreationDate)
|
||||
|
||||
return mod.Expense{
|
||||
ExpID: e.Expense.ExpID,
|
||||
Description: e.Expense.Description,
|
||||
Value: value,
|
||||
Store: mod.Store{
|
||||
StoreID: e.Store.StoreID,
|
||||
StoreName: e.Store.StoreName,
|
||||
},
|
||||
Type: mod.Type{
|
||||
TypeID: e.ExpenseType.TypeID,
|
||||
TypeName: e.ExpenseType.TypeName,
|
||||
},
|
||||
Category: mod.Category{
|
||||
CategoryID: e.Category.CategoryID,
|
||||
CategoryName: e.Category.CategoryName,
|
||||
},
|
||||
Owner: mod.User{
|
||||
UserID: e.User.UserID,
|
||||
UserName: e.User.UserName,
|
||||
},
|
||||
Date: expDate,
|
||||
Shares: []mod.Share{},
|
||||
Payments: []mod.Payment{},
|
||||
PaidOff: paidOff,
|
||||
SharesEven: sharesEven,
|
||||
QRString: e.Expense.Qr,
|
||||
CreationDate: creationDate,
|
||||
}
|
||||
type ExpenseRow interface {
|
||||
GetExpense() pgsqlc.Expense
|
||||
GetStore() pgsqlc.Store
|
||||
GetType() pgsqlc.ExpenseType
|
||||
GetCategory() pgsqlc.Category
|
||||
GetUser() pgsqlc.User
|
||||
}
|
||||
|
||||
func mapRepoGetExpenseRowMulti(e pgsqlc.GetExpensesRow) mod.Expense {
|
||||
value := pgNumericToDecimal(e.Expense.Value)
|
||||
paidOff := pgBoolToBool(e.Expense.PaidOff)
|
||||
sharesEven := pgBoolToBool(e.Expense.SharesEven)
|
||||
expDate := pgTimestampToTime(e.Expense.ExpDate)
|
||||
creationDate := pgTimestampToTime(e.Expense.CreationDate)
|
||||
type (
|
||||
ExpenseRowSingle pgsqlc.GetExpenseRow
|
||||
ExpenseRowMulti pgsqlc.GetExpensesRow
|
||||
)
|
||||
|
||||
func (e ExpenseRowSingle) GetExpense() pgsqlc.Expense { return e.Expense }
|
||||
func (e ExpenseRowSingle) GetStore() pgsqlc.Store { return e.Store }
|
||||
func (e ExpenseRowSingle) GetType() pgsqlc.ExpenseType { return e.ExpenseType }
|
||||
func (e ExpenseRowSingle) GetCategory() pgsqlc.Category { return e.Category }
|
||||
func (e ExpenseRowSingle) GetUser() pgsqlc.User { return e.User }
|
||||
|
||||
func (e ExpenseRowMulti) GetExpense() pgsqlc.Expense { return e.Expense }
|
||||
func (e ExpenseRowMulti) GetStore() pgsqlc.Store { return e.Store }
|
||||
func (e ExpenseRowMulti) GetType() pgsqlc.ExpenseType { return e.ExpenseType }
|
||||
func (e ExpenseRowMulti) GetCategory() pgsqlc.Category { return e.Category }
|
||||
func (e ExpenseRowMulti) GetUser() pgsqlc.User { return e.User }
|
||||
|
||||
func mapRepoExpenseRow(e ExpenseRow) mod.Expense {
|
||||
exp := e.GetExpense()
|
||||
value := pgNumericToDecimal(exp.Value)
|
||||
paidOff := pgBoolToBool(exp.PaidOff)
|
||||
sharesEven := pgBoolToBool(exp.SharesEven)
|
||||
expDate := pgTimestampToTime(exp.ExpDate)
|
||||
creationDate := pgTimestampToTime(exp.CreationDate)
|
||||
|
||||
return mod.Expense{
|
||||
ExpID: e.Expense.ExpID,
|
||||
Description: e.Expense.Description,
|
||||
ExpID: exp.ExpID,
|
||||
Description: exp.Description,
|
||||
Value: value,
|
||||
Store: mod.Store{
|
||||
StoreID: e.Store.StoreID,
|
||||
StoreName: e.Store.StoreName,
|
||||
StoreID: e.GetStore().StoreID,
|
||||
StoreName: e.GetStore().StoreName,
|
||||
},
|
||||
Type: mod.Type{
|
||||
TypeID: e.ExpenseType.TypeID,
|
||||
TypeName: e.ExpenseType.TypeName,
|
||||
TypeID: e.GetType().TypeID,
|
||||
TypeName: e.GetType().TypeName,
|
||||
},
|
||||
Category: mod.Category{
|
||||
CategoryID: e.Category.CategoryID,
|
||||
CategoryName: e.Category.CategoryName,
|
||||
CategoryID: e.GetCategory().CategoryID,
|
||||
CategoryName: e.GetCategory().CategoryName,
|
||||
},
|
||||
Owner: mod.User{
|
||||
UserID: e.User.UserID,
|
||||
UserName: e.User.UserName,
|
||||
UserID: e.GetUser().UserID,
|
||||
UserName: e.GetUser().UserName,
|
||||
},
|
||||
Date: expDate,
|
||||
Shares: []mod.Share{},
|
||||
Payments: []mod.Payment{},
|
||||
PaidOff: paidOff,
|
||||
SharesEven: sharesEven,
|
||||
QRString: e.Expense.Qr,
|
||||
QRString: exp.Qr,
|
||||
CreationDate: creationDate,
|
||||
}
|
||||
}
|
||||
@@ -82,7 +71,7 @@ func mapRepoGetExpenseRowMulti(e pgsqlc.GetExpensesRow) mod.Expense {
|
||||
func mapRepoGetExpensesRows(er []pgsqlc.GetExpensesRow) []mod.Expense {
|
||||
expenses := make([]mod.Expense, len(er))
|
||||
for k, exp := range er {
|
||||
expenses[k] = mapRepoGetExpenseRowMulti(exp)
|
||||
expenses[k] = mapRepoExpenseRow(ExpenseRowSingle(exp))
|
||||
}
|
||||
return expenses
|
||||
}
|
||||
|
||||
@@ -2,17 +2,19 @@ package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||
"fmt"
|
||||
|
||||
"github.com/ESilva15/expenses/expenses/repo/pgdb/pgsqlc"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
)
|
||||
|
||||
func (p *PgExpRepo) insertShare(
|
||||
ctx context.Context,
|
||||
q *pgsqlc.Queries,
|
||||
eId int32,
|
||||
sh mod.Share) error {
|
||||
|
||||
sh mod.Share,
|
||||
) error {
|
||||
share, err := decimalToNumeric(sh.Share)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -43,16 +45,16 @@ func (p *PgExpRepo) insertShare(
|
||||
func (p PgExpRepo) InsertShare(
|
||||
ctx context.Context,
|
||||
eId int32,
|
||||
sh mod.Share) error {
|
||||
|
||||
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 {
|
||||
|
||||
sh mod.Shares,
|
||||
) error {
|
||||
return p.insertShares(ctx, pgsqlc.New(p.DB), eId, sh)
|
||||
}
|
||||
|
||||
@@ -60,8 +62,8 @@ func (p *PgExpRepo) insertShares(
|
||||
ctx context.Context,
|
||||
q *pgsqlc.Queries,
|
||||
eId int32,
|
||||
sh []mod.Share) error {
|
||||
|
||||
sh []mod.Share,
|
||||
) error {
|
||||
for k := range sh {
|
||||
err := p.insertShare(ctx, q, eId, sh[k])
|
||||
if err != nil {
|
||||
@@ -73,8 +75,8 @@ func (p *PgExpRepo) insertShares(
|
||||
}
|
||||
|
||||
func (p PgExpRepo) updateShare(
|
||||
ctx context.Context, q *pgsqlc.Queries, sh mod.Share) error {
|
||||
|
||||
ctx context.Context, q *pgsqlc.Queries, sh mod.Share,
|
||||
) error {
|
||||
share, err := decimalToNumeric(sh.Share)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -107,8 +109,8 @@ func (p PgExpRepo) UpdateShare(ctx context.Context, sh mod.Share) error {
|
||||
}
|
||||
|
||||
func (p *PgExpRepo) deleteShare(
|
||||
ctx context.Context, q *pgsqlc.Queries, id int32) error {
|
||||
|
||||
ctx context.Context, q *pgsqlc.Queries, id int32,
|
||||
) error {
|
||||
res, err := q.DeleteShare(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -127,8 +129,8 @@ func (p PgExpRepo) DeleteShare(ctx context.Context, id int32) error {
|
||||
}
|
||||
|
||||
func (p *PgExpRepo) getShares(
|
||||
ctx context.Context, q *pgsqlc.Queries, eId int32) (mod.Shares, error) {
|
||||
|
||||
ctx context.Context, q *pgsqlc.Queries, eId int32,
|
||||
) (mod.Shares, error) {
|
||||
shares, err := q.GetShares(ctx, eId)
|
||||
if err != nil {
|
||||
return mod.Shares{}, err
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
"github.com/ESilva15/expenses/expenses/repo/pgdb/pgsqlc"
|
||||
)
|
||||
|
||||
func mapRepoGetSharesRow(s pgsqlc.GetSharesRow) mod.Share {
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"expenses/config"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/ESilva15/expenses/config"
|
||||
|
||||
mig "github.com/golang-migrate/migrate/v4"
|
||||
_ "github.com/golang-migrate/migrate/v4/database/postgres"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
|
||||
@@ -2,10 +2,11 @@ package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
experr "expenses/expenses/errors"
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||
"fmt"
|
||||
|
||||
experr "github.com/ESilva15/expenses/expenses/errors"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
"github.com/ESilva15/expenses/expenses/repo/pgdb/pgsqlc"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
"github.com/ESilva15/expenses/expenses/repo/pgdb/pgsqlc"
|
||||
)
|
||||
|
||||
func mapRepoStore(rs pgsqlc.Store) mod.Store {
|
||||
|
||||
@@ -2,10 +2,11 @@ package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
experr "expenses/expenses/errors"
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||
"fmt"
|
||||
|
||||
experr "github.com/ESilva15/expenses/expenses/errors"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
"github.com/ESilva15/expenses/expenses/repo/pgdb/pgsqlc"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
"github.com/ESilva15/expenses/expenses/repo/pgdb/pgsqlc"
|
||||
)
|
||||
|
||||
func mapRepoType(rt pgsqlc.ExpenseType) mod.Type {
|
||||
|
||||
@@ -2,25 +2,30 @@ package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
"github.com/ESilva15/expenses/expenses/repo/pgdb/pgsqlc"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// PgUserRepo is a PG repository for users.
|
||||
type PgUserRepo struct {
|
||||
DB *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewPgUserRepo(db *pgxpool.Pool) UserRepository {
|
||||
// NewPgUserRepo returns a new UserRepository of Postgres.
|
||||
func NewPgUserRepo(db *pgxpool.Pool) PgUserRepo {
|
||||
return PgUserRepo{
|
||||
DB: db,
|
||||
}
|
||||
}
|
||||
|
||||
// Close closes a PgUserRepo instance.
|
||||
func (p PgUserRepo) Close() {
|
||||
p.DB.Close()
|
||||
}
|
||||
|
||||
// Get returns an user by its ID.
|
||||
func (p PgUserRepo) Get(ctx context.Context, id int32) (*mod.User, error) {
|
||||
queries := pgsqlc.New(p.DB)
|
||||
user, err := queries.GetUser(ctx, id)
|
||||
@@ -33,7 +38,8 @@ func (p PgUserRepo) Get(ctx context.Context, id int32) (*mod.User, error) {
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func (p PgUserRepo) GetByName(ctx context.Context, name string) (*mod.User, error) {
|
||||
// GetByName returns a user by its name.
|
||||
func (p PgUserRepo) GetByName(ctx context.Context, name string) (*mod.User, error) {
|
||||
queries := pgsqlc.New(p.DB)
|
||||
user, err := queries.GetUserByName(ctx, name)
|
||||
if err != nil {
|
||||
@@ -45,6 +51,7 @@ func (p PgUserRepo) GetByName(ctx context.Context, name string) (*mod.User, erro
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
// GetAll returns all users.
|
||||
func (p PgUserRepo) GetAll(ctx context.Context) (mod.Users, error) {
|
||||
queries := pgsqlc.New(p.DB)
|
||||
userList, err := queries.GetUsers(ctx)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
"expenses/expenses/repo/pgdb/pgsqlc"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
"github.com/ESilva15/expenses/expenses/repo/pgdb/pgsqlc"
|
||||
)
|
||||
|
||||
func mapRepoUser(ru pgsqlc.User) mod.User {
|
||||
|
||||
@@ -2,9 +2,11 @@ package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
)
|
||||
|
||||
// StoreRepository defines the repository for the store data model.
|
||||
type StoreRepository interface {
|
||||
Get(ctx context.Context, id int32) (mod.Store, error)
|
||||
GetByNIF(ctx context.Context, nif string) (mod.Store, error)
|
||||
|
||||
@@ -2,9 +2,11 @@ package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
)
|
||||
|
||||
// TypeRepository defines the repository for the type data model.
|
||||
type TypeRepository interface {
|
||||
Get(ctx context.Context, id int32) (mod.Type, error)
|
||||
GetAll(ctx context.Context) (mod.Types, error)
|
||||
|
||||
@@ -2,9 +2,11 @@ package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
)
|
||||
|
||||
// UserRepository defines the repository for the user data model.
|
||||
type UserRepository interface {
|
||||
Get(ctx context.Context, id int32) (*mod.User, error)
|
||||
GetByName(ctx context.Context, name string) (*mod.User, error)
|
||||
|
||||
@@ -2,7 +2,8 @@ package expenses
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
)
|
||||
|
||||
func (a *ExpApp) GetAllStores(ctx context.Context) ([]mod.Store, error) {
|
||||
|
||||
@@ -2,7 +2,8 @@ package expenses
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
)
|
||||
|
||||
// GetAllTypes returns all types
|
||||
|
||||
@@ -2,7 +2,8 @@ package expenses
|
||||
|
||||
import (
|
||||
"context"
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
module expenses
|
||||
module github.com/ESilva15/expenses
|
||||
|
||||
go 1.24.6
|
||||
|
||||
|
||||
+3
-2
@@ -2,9 +2,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"expenses/cmd"
|
||||
"expenses/expenses"
|
||||
"log"
|
||||
|
||||
"github.com/ESilva15/expenses/cmd"
|
||||
"github.com/ESilva15/expenses/expenses"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
+2
-1
@@ -1,10 +1,11 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"expenses/api"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/ESilva15/expenses/api"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
||||
@@ -4,12 +4,13 @@ package pages
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
exp "expenses/expenses"
|
||||
experr "expenses/expenses/errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
experr "github.com/ESilva15/expenses/expenses/errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
||||
+15
-13
@@ -2,18 +2,22 @@ package pages
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
exp "expenses/expenses"
|
||||
experr "expenses/expenses/errors"
|
||||
mod "expenses/expenses/models"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
experr "github.com/ESilva15/expenses/expenses/errors"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
|
||||
fatqr "github.com/ESilva15/gofatqr"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
// ExpensesPath is the expenses resource path.
|
||||
ExpensesPath = "/expenses"
|
||||
)
|
||||
|
||||
@@ -35,7 +39,7 @@ func expensesPartial(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func ExpensesGlobalPage(c *gin.Context) {
|
||||
func expensesGlobalPage(c *gin.Context) {
|
||||
ctx, err := getLoggedInUserCTX(c)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "Could not get logged in user")
|
||||
@@ -130,7 +134,6 @@ func expensePage(c *gin.Context) {
|
||||
"stores": stores,
|
||||
"types": types,
|
||||
"users": users,
|
||||
// "summary": summary,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -189,8 +192,7 @@ func createExpense(c *gin.Context) {
|
||||
|
||||
newExp, err := expenseFromForm(c, ctx)
|
||||
if err != nil {
|
||||
// TODO
|
||||
// Change this to something the user can see
|
||||
// TODO - Change this to something the user can see
|
||||
c.Header("HX-Redirect", "/500")
|
||||
return
|
||||
}
|
||||
@@ -220,8 +222,7 @@ func updateExpense(c *gin.Context) {
|
||||
|
||||
newExp, err := expenseFromForm(c, ctx)
|
||||
if err != nil {
|
||||
// TODO
|
||||
// Change this to something the user can see
|
||||
// TODO - Change this to something the user can see
|
||||
c.Header("HX-Redirect", "/500")
|
||||
return
|
||||
}
|
||||
@@ -245,7 +246,7 @@ func deleteExpense(c *gin.Context) {
|
||||
}
|
||||
|
||||
err = exp.App.DeleteExpense(expenseID)
|
||||
if err == experr.ErrNotFound {
|
||||
if errors.Is(err, experr.ErrNotFound) {
|
||||
log.Println("error:", err)
|
||||
errMsg := fmt.Sprintf("category %d not found", expenseID)
|
||||
c.String(http.StatusNotFound, errMsg)
|
||||
@@ -289,14 +290,15 @@ func qrRequest(c *gin.Context) {
|
||||
response := map[string]string{
|
||||
"total": fat.GrossTotal.String(),
|
||||
"date": fat.InvoiceDate.Format("02-Jan-2006"),
|
||||
"storeID": fmt.Sprintf("%d", storeID),
|
||||
"storeID": strconv.Itoa(int(storeID)),
|
||||
}
|
||||
|
||||
c.JSON(200, response)
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// RouteExpenses routes the endpoints for the HTML frontend.
|
||||
func RouteExpenses(router *gin.RouterGroup) {
|
||||
router.GET(ExpensesPath, ExpensesGlobalPage)
|
||||
router.GET(ExpensesPath, expensesGlobalPage)
|
||||
router.GET(ExpensesPath+"/:id", expensePage)
|
||||
router.POST(ExpensesPath, expensesPartial)
|
||||
|
||||
|
||||
@@ -2,10 +2,11 @@ package pages
|
||||
|
||||
import (
|
||||
"context"
|
||||
"expenses/expenses"
|
||||
mod "expenses/expenses/models"
|
||||
"time"
|
||||
|
||||
"github.com/ESilva15/expenses/expenses"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
+3
-2
@@ -1,9 +1,10 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
exp "expenses/expenses"
|
||||
"net/http"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -25,7 +26,7 @@ func loginUser(c *gin.Context) {
|
||||
password := c.PostForm("login-pass")
|
||||
|
||||
user, err := exp.App.ValidateCredentials(username, password)
|
||||
if err != nil {
|
||||
if err != nil {
|
||||
// TODO - go to an error page or login failed or something
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
exp "expenses/expenses"
|
||||
mod "expenses/expenses/models"
|
||||
"log"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -68,6 +69,10 @@ func getResults(c *gin.Context) {
|
||||
log.Printf("failed to get debts: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println(queriedExpenses[k].Shares)
|
||||
log.Println(queriedExpenses[k].Payments)
|
||||
log.Println(queriedExpenses[k].Debts)
|
||||
}
|
||||
|
||||
userDebtSummary := make(map[mod.User]decimal.Decimal)
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
exp "expenses/expenses"
|
||||
experr "expenses/expenses/errors"
|
||||
mod "expenses/expenses/models"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
experr "github.com/ESilva15/expenses/expenses/errors"
|
||||
mod "github.com/ESilva15/expenses/expenses/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
dec "github.com/shopspring/decimal"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func deletePayment(c *gin.Context) {
|
||||
|
||||
+3
-2
@@ -1,11 +1,12 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
exp "expenses/expenses"
|
||||
experr "expenses/expenses/errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
experr "github.com/ESilva15/expenses/expenses/errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
||||
+11
-8
@@ -2,17 +2,20 @@ package pages
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
exp "expenses/expenses"
|
||||
experr "expenses/expenses/errors"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
experr "github.com/ESilva15/expenses/expenses/errors"
|
||||
|
||||
fatqr "github.com/ESilva15/gofatqr"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
// StoresPath defines the stores resources path.
|
||||
StoresPath = "/stores"
|
||||
)
|
||||
|
||||
@@ -46,7 +49,7 @@ func storePage(c *gin.Context) {
|
||||
}
|
||||
|
||||
store, err := exp.App.GetStore(storeID)
|
||||
if err == sql.ErrNoRows {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
NotFoundView(c, fmt.Sprintf("didn't find store with ID `%d`", storeID))
|
||||
return
|
||||
}
|
||||
@@ -89,7 +92,7 @@ func createStore(c *gin.Context) {
|
||||
func updateStore(c *gin.Context) {
|
||||
storeID, err := exp.ParseID(c.Param("id"))
|
||||
if err != nil {
|
||||
c.Redirect(404, "/404")
|
||||
c.Redirect(http.StatusNotFound, "/404")
|
||||
}
|
||||
newName := c.PostForm("store-name")
|
||||
newNIF := c.PostForm("store-nif")
|
||||
@@ -106,7 +109,7 @@ func updateStore(c *gin.Context) {
|
||||
func deleteStore(c *gin.Context) {
|
||||
storeID, err := exp.ParseID(c.Param("id"))
|
||||
if err != nil {
|
||||
c.Redirect(404, "/404")
|
||||
c.Redirect(http.StatusNotFound, "/404")
|
||||
}
|
||||
|
||||
err = exp.App.DeleteStore(storeID)
|
||||
@@ -132,8 +135,7 @@ func getNIF(c *gin.Context) {
|
||||
var fat fatqr.FatQR
|
||||
err := fat.Scan(qr, 0)
|
||||
if err != nil {
|
||||
// TODO
|
||||
// handle the error here for the user
|
||||
// TODO - handle the error here for the user
|
||||
return
|
||||
}
|
||||
|
||||
@@ -141,9 +143,10 @@ func getNIF(c *gin.Context) {
|
||||
"nif": fat.TaxRegistrationNumber,
|
||||
}
|
||||
|
||||
c.JSON(200, response)
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// RouteStores routes the endpoints for the stores resources.
|
||||
func RouteStores(router *gin.RouterGroup) {
|
||||
router.GET(StoresPath, storesGlobalPage)
|
||||
router.GET(StoresPath+"/:id", storePage)
|
||||
|
||||
+8
-4
@@ -2,14 +2,17 @@ package pages
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
exp "expenses/expenses"
|
||||
experr "expenses/expenses/errors"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
|
||||
exp "github.com/ESilva15/expenses/expenses"
|
||||
experr "github.com/ESilva15/expenses/expenses/errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
// TypesPath defines the resource path
|
||||
TypesPath = "/types"
|
||||
)
|
||||
|
||||
@@ -42,7 +45,7 @@ func typePage(c *gin.Context) {
|
||||
}
|
||||
|
||||
typ, err := exp.App.GetType(typeID)
|
||||
if err == sql.ErrNoRows {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
NotFoundView(c, fmt.Sprintf("Couldn't find type `%d`", typeID))
|
||||
return
|
||||
}
|
||||
@@ -118,6 +121,7 @@ func updateType(c *gin.Context) {
|
||||
c.Header("HX-Trigger", "{\"formState\":\"Success\"}")
|
||||
}
|
||||
|
||||
// RouteTypes routes the endpoints for the types resources.
|
||||
func RouteTypes(router *gin.RouterGroup) {
|
||||
router.GET(TypesPath, typesGlobalPage)
|
||||
router.GET(TypesPath+"/:id", typePage)
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
include .env
|
||||
include $(dir $(lastword $(MAKEFILE_LIST))).env
|
||||
|
||||
REQUIRED_VARS := registry repo
|
||||
|
||||
|
||||
Reference in New Issue
Block a user