diff --git a/internal/default.go b/internal/default.go index 4ba27a9..1d7de57 100644 --- a/internal/default.go +++ b/internal/default.go @@ -15,6 +15,7 @@ import ( "spend-sparrow/internal/handler/middleware" "spend-sparrow/internal/log" "spend-sparrow/internal/service" + "spend-sparrow/internal/transaction_recurring" "spend-sparrow/internal/treasure_chest" "spend-sparrow/internal/types" "sync" @@ -119,7 +120,7 @@ func createHandlerWithServices(ctx context.Context, d *sqlx.DB, serverSettings * accountService := account.NewServiceImpl(d, randomService, clockService) treasureChestService := treasure_chest.NewService(d, randomService, clockService) transactionService := service.NewTransaction(d, randomService, clockService) - transactionRecurringService := service.NewTransactionRecurring(d, randomService, clockService, transactionService) + transactionRecurringService := transaction_recurring.NewService(d, randomService, clockService, transactionService) dashboardService := dashboard.NewService(d) render := core.NewRender() @@ -129,7 +130,7 @@ func createHandlerWithServices(ctx context.Context, d *sqlx.DB, serverSettings * accountHandler := account.NewHandler(accountService, render) treasureChestHandler := treasure_chest.NewHandler(treasureChestService, transactionRecurringService, render) transactionHandler := handler.NewTransaction(transactionService, accountService, treasureChestService, render) - transactionRecurringHandler := handler.NewTransactionRecurring(transactionRecurringService, render) + transactionRecurringHandler := transaction_recurring.NewHandler(transactionRecurringService, render) go dailyTaskTimer(ctx, transactionRecurringService, authService) @@ -159,7 +160,7 @@ func createHandlerWithServices(ctx context.Context, d *sqlx.DB, serverSettings * return wrapper } -func dailyTaskTimer(ctx context.Context, transactionRecurring service.TransactionRecurring, auth authentication.Service) { +func dailyTaskTimer(ctx context.Context, transactionRecurring transaction_recurring.Service, auth authentication.Service) { runDailyTasks(ctx, transactionRecurring, auth) ticker := time.NewTicker(24 * time.Hour) defer ticker.Stop() @@ -174,7 +175,7 @@ func dailyTaskTimer(ctx context.Context, transactionRecurring service.Transactio } } -func runDailyTasks(ctx context.Context, transactionRecurring service.TransactionRecurring, auth authentication.Service) { +func runDailyTasks(ctx context.Context, transactionRecurring transaction_recurring.Service, auth authentication.Service) { slog.InfoContext(ctx, "Running daily tasks") _ = transactionRecurring.GenerateTransactions(ctx) _ = auth.CleanupSessionsAndTokens(ctx) diff --git a/internal/handler/transaction.go b/internal/handler/transaction.go index b6cfe6e..3c351b6 100644 --- a/internal/handler/transaction.go +++ b/internal/handler/transaction.go @@ -19,6 +19,10 @@ import ( "github.com/google/uuid" ) +const ( + DECIMALS_MULTIPLIER = 100 +) + type Transaction interface { Handle(router *http.ServeMux) } @@ -191,7 +195,7 @@ func (h TransactionImpl) handleUpdateTransaction() http.HandlerFunc { core.HandleError(w, r, fmt.Errorf("could not parse value: %w", core.ErrBadRequest)) return } - value := int64(math.Round(valueF * service.DECIMALS_MULTIPLIER)) + value := int64(math.Round(valueF * DECIMALS_MULTIPLIER)) timestamp, err := time.Parse("2006-01-02", r.FormValue("timestamp")) if err != nil { diff --git a/internal/template/transaction_recurring/default.go b/internal/template/transaction_recurring/default.go deleted file mode 100644 index 5b5b034..0000000 --- a/internal/template/transaction_recurring/default.go +++ /dev/null @@ -1 +0,0 @@ -package transaction_recurring diff --git a/internal/handler/transaction_recurring.go b/internal/transaction_recurring/handler.go similarity index 72% rename from internal/handler/transaction_recurring.go rename to internal/transaction_recurring/handler.go index 615d443..b4d8286 100644 --- a/internal/handler/transaction_recurring.go +++ b/internal/transaction_recurring/handler.go @@ -1,38 +1,35 @@ -package handler +package transaction_recurring import ( "net/http" "spend-sparrow/internal/auth_types" "spend-sparrow/internal/core" - "spend-sparrow/internal/service" - t "spend-sparrow/internal/template/transaction_recurring" - "spend-sparrow/internal/types" "spend-sparrow/internal/utils" ) -type TransactionRecurring interface { +type Handler interface { Handle(router *http.ServeMux) } -type TransactionRecurringImpl struct { - s service.TransactionRecurring +type HandlerImpl struct { + s Service r *core.Render } -func NewTransactionRecurring(s service.TransactionRecurring, r *core.Render) TransactionRecurring { - return TransactionRecurringImpl{ +func NewHandler(s Service, r *core.Render) Handler { + return HandlerImpl{ s: s, r: r, } } -func (h TransactionRecurringImpl) Handle(r *http.ServeMux) { +func (h HandlerImpl) Handle(r *http.ServeMux) { r.Handle("GET /transaction-recurring", h.handleTransactionRecurringItemComp()) r.Handle("POST /transaction-recurring/{id}", h.handleUpdateTransactionRecurring()) r.Handle("DELETE /transaction-recurring/{id}", h.handleDeleteTransactionRecurring()) } -func (h TransactionRecurringImpl) handleTransactionRecurringItemComp() http.HandlerFunc { +func (h HandlerImpl) handleTransactionRecurringItemComp() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { core.UpdateSpan(r) @@ -49,7 +46,7 @@ func (h TransactionRecurringImpl) handleTransactionRecurringItemComp() http.Hand } } -func (h TransactionRecurringImpl) handleUpdateTransactionRecurring() http.HandlerFunc { +func (h HandlerImpl) handleUpdateTransactionRecurring() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { core.UpdateSpan(r) @@ -59,7 +56,7 @@ func (h TransactionRecurringImpl) handleUpdateTransactionRecurring() http.Handle return } - input := types.TransactionRecurringInput{ + input := TransactionRecurringInput{ Id: r.PathValue("id"), IntervalMonths: r.FormValue("interval-months"), NextExecution: r.FormValue("next-execution"), @@ -88,7 +85,7 @@ func (h TransactionRecurringImpl) handleUpdateTransactionRecurring() http.Handle } } -func (h TransactionRecurringImpl) handleDeleteTransactionRecurring() http.HandlerFunc { +func (h HandlerImpl) handleDeleteTransactionRecurring() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { core.UpdateSpan(r) @@ -112,8 +109,8 @@ func (h TransactionRecurringImpl) handleDeleteTransactionRecurring() http.Handle } } -func (h TransactionRecurringImpl) renderItems(w http.ResponseWriter, r *http.Request, user *auth_types.User, id, accountId, treasureChestId string) { - var transactionsRecurring []*types.TransactionRecurring +func (h HandlerImpl) renderItems(w http.ResponseWriter, r *http.Request, user *auth_types.User, id, accountId, treasureChestId string) { + var transactionsRecurring []*TransactionRecurring var err error if accountId == "" && treasureChestId == "" { utils.TriggerToastWithStatus(r.Context(), w, r, "error", "Please select an account or treasure chest", http.StatusBadRequest) @@ -132,6 +129,6 @@ func (h TransactionRecurringImpl) renderItems(w http.ResponseWriter, r *http.Req } } - comp := t.TransactionRecurringItems(transactionsRecurring, id, accountId, treasureChestId) + comp := TransactionRecurringItems(transactionsRecurring, id, accountId, treasureChestId) h.r.Render(r, w, comp) } diff --git a/internal/service/transaction_recurring.go b/internal/transaction_recurring/service.go similarity index 86% rename from internal/service/transaction_recurring.go rename to internal/transaction_recurring/service.go index 1ac0b6b..62e480b 100644 --- a/internal/service/transaction_recurring.go +++ b/internal/transaction_recurring/service.go @@ -1,4 +1,4 @@ -package service +package transaction_recurring import ( "context" @@ -8,6 +8,7 @@ import ( "math" "spend-sparrow/internal/auth_types" "spend-sparrow/internal/core" + "spend-sparrow/internal/service" "spend-sparrow/internal/treasure_chest_types" "spend-sparrow/internal/types" "strconv" @@ -21,26 +22,26 @@ const ( DECIMALS_MULTIPLIER = 100 ) -type TransactionRecurring interface { - Add(ctx context.Context, user *auth_types.User, transactionRecurring types.TransactionRecurringInput) (*types.TransactionRecurring, error) - Update(ctx context.Context, user *auth_types.User, transactionRecurring types.TransactionRecurringInput) (*types.TransactionRecurring, error) - GetAll(ctx context.Context, user *auth_types.User) ([]*types.TransactionRecurring, error) - GetAllByAccount(ctx context.Context, user *auth_types.User, accountId string) ([]*types.TransactionRecurring, error) - GetAllByTreasureChest(ctx context.Context, user *auth_types.User, treasureChestId string) ([]*types.TransactionRecurring, error) +type Service interface { + Add(ctx context.Context, user *auth_types.User, transactionRecurring TransactionRecurringInput) (*TransactionRecurring, error) + Update(ctx context.Context, user *auth_types.User, transactionRecurring TransactionRecurringInput) (*TransactionRecurring, error) + GetAll(ctx context.Context, user *auth_types.User) ([]*TransactionRecurring, error) + GetAllByAccount(ctx context.Context, user *auth_types.User, accountId string) ([]*TransactionRecurring, error) + GetAllByTreasureChest(ctx context.Context, user *auth_types.User, treasureChestId string) ([]*TransactionRecurring, error) Delete(ctx context.Context, user *auth_types.User, id string) error GenerateTransactions(ctx context.Context) error } -type TransactionRecurringImpl struct { +type ServiceImpl struct { db *sqlx.DB clock core.Clock random core.Random - transaction Transaction + transaction service.Transaction } -func NewTransactionRecurring(db *sqlx.DB, random core.Random, clock core.Clock, transaction Transaction) TransactionRecurring { - return TransactionRecurringImpl{ +func NewService(db *sqlx.DB, random core.Random, clock core.Clock, transaction service.Transaction) Service { + return ServiceImpl{ db: db, clock: clock, random: random, @@ -48,10 +49,10 @@ func NewTransactionRecurring(db *sqlx.DB, random core.Random, clock core.Clock, } } -func (s TransactionRecurringImpl) Add(ctx context.Context, +func (s ServiceImpl) Add(ctx context.Context, user *auth_types.User, - transactionRecurringInput types.TransactionRecurringInput, -) (*types.TransactionRecurring, error) { + transactionRecurringInput TransactionRecurringInput, +) (*TransactionRecurring, error) { if user == nil { return nil, core.ErrUnauthorized } @@ -90,10 +91,10 @@ func (s TransactionRecurringImpl) Add(ctx context.Context, return transactionRecurring, nil } -func (s TransactionRecurringImpl) Update(ctx context.Context, +func (s ServiceImpl) Update(ctx context.Context, user *auth_types.User, - input types.TransactionRecurringInput, -) (*types.TransactionRecurring, error) { + input TransactionRecurringInput, +) (*TransactionRecurring, error) { if user == nil { return nil, core.ErrUnauthorized } @@ -112,7 +113,7 @@ func (s TransactionRecurringImpl) Update(ctx context.Context, _ = tx.Rollback() }() - transactionRecurring := &types.TransactionRecurring{} + transactionRecurring := &TransactionRecurring{} err = tx.GetContext(ctx, transactionRecurring, `SELECT * FROM transaction_recurring WHERE user_id = ? AND id = ?`, user.Id, uuid) err = core.TransformAndLogDbError(ctx, "transactionRecurring Update", nil, err) if err != nil { @@ -155,12 +156,12 @@ func (s TransactionRecurringImpl) Update(ctx context.Context, return transactionRecurring, nil } -func (s TransactionRecurringImpl) GetAll(ctx context.Context, user *auth_types.User) ([]*types.TransactionRecurring, error) { +func (s ServiceImpl) GetAll(ctx context.Context, user *auth_types.User) ([]*TransactionRecurring, error) { if user == nil { return nil, core.ErrUnauthorized } - transactionRecurrings := make([]*types.TransactionRecurring, 0) + transactionRecurrings := make([]*TransactionRecurring, 0) err := s.db.SelectContext(ctx, &transactionRecurrings, ` SELECT * FROM transaction_recurring @@ -175,7 +176,7 @@ func (s TransactionRecurringImpl) GetAll(ctx context.Context, user *auth_types.U return transactionRecurrings, nil } -func (s TransactionRecurringImpl) GetAllByAccount(ctx context.Context, user *auth_types.User, accountId string) ([]*types.TransactionRecurring, error) { +func (s ServiceImpl) GetAllByAccount(ctx context.Context, user *auth_types.User, accountId string) ([]*TransactionRecurring, error) { if user == nil { return nil, core.ErrUnauthorized } @@ -205,7 +206,7 @@ func (s TransactionRecurringImpl) GetAllByAccount(ctx context.Context, user *aut return nil, core.ErrInternal } - transactionRecurrings := make([]*types.TransactionRecurring, 0) + transactionRecurrings := make([]*TransactionRecurring, 0) err = tx.SelectContext(ctx, &transactionRecurrings, ` SELECT * FROM transaction_recurring @@ -227,10 +228,10 @@ func (s TransactionRecurringImpl) GetAllByAccount(ctx context.Context, user *aut return transactionRecurrings, nil } -func (s TransactionRecurringImpl) GetAllByTreasureChest(ctx context.Context, +func (s ServiceImpl) GetAllByTreasureChest(ctx context.Context, user *auth_types.User, treasureChestId string, -) ([]*types.TransactionRecurring, error) { +) ([]*TransactionRecurring, error) { if user == nil { return nil, core.ErrUnauthorized } @@ -260,7 +261,7 @@ func (s TransactionRecurringImpl) GetAllByTreasureChest(ctx context.Context, return nil, core.ErrInternal } - transactionRecurrings := make([]*types.TransactionRecurring, 0) + transactionRecurrings := make([]*TransactionRecurring, 0) err = tx.SelectContext(ctx, &transactionRecurrings, ` SELECT * FROM transaction_recurring @@ -282,7 +283,7 @@ func (s TransactionRecurringImpl) GetAllByTreasureChest(ctx context.Context, return transactionRecurrings, nil } -func (s TransactionRecurringImpl) Delete(ctx context.Context, user *auth_types.User, id string) error { +func (s ServiceImpl) Delete(ctx context.Context, user *auth_types.User, id string) error { if user == nil { return core.ErrUnauthorized } @@ -301,7 +302,7 @@ func (s TransactionRecurringImpl) Delete(ctx context.Context, user *auth_types.U _ = tx.Rollback() }() - var transactionRecurring types.TransactionRecurring + var transactionRecurring TransactionRecurring err = tx.GetContext(ctx, &transactionRecurring, `SELECT * FROM transaction_recurring WHERE user_id = ? AND id = ?`, user.Id, uuid) err = core.TransformAndLogDbError(ctx, "transactionRecurring Delete", nil, err) if err != nil { @@ -323,7 +324,7 @@ func (s TransactionRecurringImpl) Delete(ctx context.Context, user *auth_types.U return nil } -func (s TransactionRecurringImpl) GenerateTransactions(ctx context.Context) error { +func (s ServiceImpl) GenerateTransactions(ctx context.Context) error { now := s.clock.Now() tx, err := s.db.BeginTxx(ctx, nil) @@ -335,7 +336,7 @@ func (s TransactionRecurringImpl) GenerateTransactions(ctx context.Context) erro _ = tx.Rollback() }() - recurringTransactions := make([]*types.TransactionRecurring, 0) + recurringTransactions := make([]*TransactionRecurring, 0) err = tx.SelectContext(ctx, &recurringTransactions, ` SELECT * FROM transaction_recurring WHERE next_execution <= ?`, now) @@ -379,13 +380,13 @@ func (s TransactionRecurringImpl) GenerateTransactions(ctx context.Context) erro return nil } -func (s TransactionRecurringImpl) validateAndEnrichTransactionRecurring( +func (s ServiceImpl) validateAndEnrichTransactionRecurring( ctx context.Context, tx *sqlx.Tx, - oldTransactionRecurring *types.TransactionRecurring, + oldTransactionRecurring *TransactionRecurring, userId uuid.UUID, - input types.TransactionRecurringInput, -) (*types.TransactionRecurring, error) { + input TransactionRecurringInput, +) (*TransactionRecurring, error) { var ( id uuid.UUID accountUuid *uuid.UUID @@ -509,7 +510,7 @@ func (s TransactionRecurringImpl) validateAndEnrichTransactionRecurring( nextExecution = &t } - transactionRecurring := types.TransactionRecurring{ + transactionRecurring := TransactionRecurring{ Id: id, UserId: userId, diff --git a/internal/template/transaction_recurring/transaction_recurring.templ b/internal/transaction_recurring/transaction_recurring.templ similarity index 93% rename from internal/template/transaction_recurring/transaction_recurring.templ rename to internal/transaction_recurring/transaction_recurring.templ index 08b5bb1..33894e6 100644 --- a/internal/template/transaction_recurring/transaction_recurring.templ +++ b/internal/transaction_recurring/transaction_recurring.templ @@ -5,7 +5,7 @@ import "time" import "spend-sparrow/internal/template/svg" import "spend-sparrow/internal/types" -templ TransactionRecurringItems(transactionsRecurring []*types.TransactionRecurring, editId, accountId, treasureChestId string) { +templ TransactionRecurringItems(transactionsRecurring []*TransactionRecurring, editId, accountId, treasureChestId string) {
if transactionRecurring.NextExecution != nil { { transactionRecurring.NextExecution.Format("2006/01") } @@ -84,7 +84,7 @@ templ TransactionRecurringItem(transactionRecurring *types.TransactionRecurring, } -templ EditTransactionRecurring(transactionRecurring *types.TransactionRecurring, accountId, treasureChestId string) { +templ EditTransactionRecurring(transactionRecurring *TransactionRecurring, accountId, treasureChestId string) { {{ var ( id string diff --git a/internal/types/transaction_recurring.go b/internal/transaction_recurring/types.go similarity index 96% rename from internal/types/transaction_recurring.go rename to internal/transaction_recurring/types.go index 3d426ee..e0060b3 100644 --- a/internal/types/transaction_recurring.go +++ b/internal/transaction_recurring/types.go @@ -1,4 +1,4 @@ -package types +package transaction_recurring import ( "time" diff --git a/internal/treasure_chest/handler.go b/internal/treasure_chest/handler.go index 58c561c..67ab617 100644 --- a/internal/treasure_chest/handler.go +++ b/internal/treasure_chest/handler.go @@ -3,10 +3,8 @@ package treasure_chest import ( "net/http" "spend-sparrow/internal/core" - "spend-sparrow/internal/service" - tr "spend-sparrow/internal/template/transaction_recurring" + "spend-sparrow/internal/transaction_recurring" "spend-sparrow/internal/treasure_chest_types" - "spend-sparrow/internal/types" "spend-sparrow/internal/utils" "github.com/a-h/templ" @@ -19,11 +17,11 @@ type Handler interface { type HandlerImpl struct { s Service - transactionRecurring service.TransactionRecurring + transactionRecurring transaction_recurring.Service r *core.Render } -func NewHandler(s Service, transactionRecurring service.TransactionRecurring, r *core.Render) Handler { +func NewHandler(s Service, transactionRecurring transaction_recurring.Service, r *core.Render) Handler { return HandlerImpl{ s: s, transactionRecurring: transactionRecurring, @@ -101,7 +99,7 @@ func (h HandlerImpl) handleHandlerItemComp() http.HandlerFunc { core.HandleError(w, r, err) return } - transactionsRec := tr.TransactionRecurringItems(transactionsRecurring, "", "", treasureChest.Id.String()) + transactionsRec := transaction_recurring.TransactionRecurringItems(transactionsRecurring, "", "", treasureChest.Id.String()) var comp templ.Component if r.URL.Query().Get("edit") == "true" { @@ -181,7 +179,7 @@ func (h HandlerImpl) handleDeleteHandler() http.HandlerFunc { func (h HandlerImpl) calculateMonthlySums( treasureChests []*treasure_chest_types.TreasureChest, - transactionsRecurring []*types.TransactionRecurring, + transactionsRecurring []*transaction_recurring.TransactionRecurring, ) map[uuid.UUID]int64 { monthlySums := make(map[uuid.UUID]int64) for _, tc := range treasureChests {