diff --git a/internal/dashboard/handler.go b/internal/dashboard/handler.go index b936037..a111b0f 100644 --- a/internal/dashboard/handler.go +++ b/internal/dashboard/handler.go @@ -6,7 +6,7 @@ import ( "net/http" "spend-sparrow/internal/core" "spend-sparrow/internal/dashboard/template" - "spend-sparrow/internal/service" + "spend-sparrow/internal/treasure_chest" "spend-sparrow/internal/utils" "strings" "time" @@ -21,10 +21,10 @@ type Handler interface { type HandlerImpl struct { r *core.Render s *Service - treasureChest service.TreasureChest + treasureChest treasure_chest.Service } -func NewHandler(r *core.Render, s *Service, treasureChest service.TreasureChest) Handler { +func NewHandler(r *core.Render, s *Service, treasureChest treasure_chest.Service) Handler { return HandlerImpl{ r: r, s: s, diff --git a/internal/dashboard/service.go b/internal/dashboard/service.go index 06c131b..29631d6 100644 --- a/internal/dashboard/service.go +++ b/internal/dashboard/service.go @@ -4,7 +4,8 @@ import ( "context" "spend-sparrow/internal/auth_types" "spend-sparrow/internal/core" - "spend-sparrow/internal/service" + "spend-sparrow/internal/treasure_chest" + "spend-sparrow/internal/treasure_chest_types" "spend-sparrow/internal/types" "time" @@ -90,14 +91,14 @@ func (s Service) TreasureChests( return nil, core.ErrUnauthorized } - treasureChests := make([]*types.TreasureChest, 0) + treasureChests := make([]*treasure_chest_types.TreasureChest, 0) err := s.db.SelectContext(ctx, &treasureChests, `SELECT * FROM treasure_chest WHERE user_id = ?`, user.Id) err = core.TransformAndLogDbError(ctx, "dashboard TreasureChests", nil, err) if err != nil { return nil, err } - treasureChests = service.SortTreasureChests(treasureChests) + treasureChests = treasure_chest.SortTreasureChests(treasureChests) result := make([]*DashboardTreasureChest, 0) diff --git a/internal/dashboard/template/dashboard.templ b/internal/dashboard/template/dashboard.templ index 9fe8241..d4c364c 100644 --- a/internal/dashboard/template/dashboard.templ +++ b/internal/dashboard/template/dashboard.templ @@ -1,8 +1,8 @@ package template -import "spend-sparrow/internal/types" +import "spend-sparrow/internal/treasure_chest_types" -templ Dashboard(treasureChests []*types.TreasureChest) { +templ Dashboard(treasureChests []*treasure_chest_types.TreasureChest) {
diff --git a/internal/default.go b/internal/default.go index 6dffd69..4ba27a9 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/treasure_chest" "spend-sparrow/internal/types" "sync" "syscall" @@ -116,7 +117,7 @@ func createHandlerWithServices(ctx context.Context, d *sqlx.DB, serverSettings * authService := authentication.NewService(authDb, randomService, clockService, mailService, serverSettings) accountService := account.NewServiceImpl(d, randomService, clockService) - treasureChestService := service.NewTreasureChest(d, randomService, clockService) + treasureChestService := treasure_chest.NewService(d, randomService, clockService) transactionService := service.NewTransaction(d, randomService, clockService) transactionRecurringService := service.NewTransactionRecurring(d, randomService, clockService, transactionService) dashboardService := dashboard.NewService(d) @@ -126,7 +127,7 @@ func createHandlerWithServices(ctx context.Context, d *sqlx.DB, serverSettings * dashboardHandler := dashboard.NewHandler(render, dashboardService, treasureChestService) authHandler := authentication.NewHandler(authService, render) accountHandler := account.NewHandler(accountService, render) - treasureChestHandler := handler.NewTreasureChest(treasureChestService, transactionRecurringService, render) + treasureChestHandler := treasure_chest.NewHandler(treasureChestService, transactionRecurringService, render) transactionHandler := handler.NewTransaction(transactionService, accountService, treasureChestService, render) transactionRecurringHandler := handler.NewTransactionRecurring(transactionRecurringService, render) diff --git a/internal/handler/transaction.go b/internal/handler/transaction.go index 5e330fa..b6cfe6e 100644 --- a/internal/handler/transaction.go +++ b/internal/handler/transaction.go @@ -8,6 +8,8 @@ import ( "spend-sparrow/internal/core" "spend-sparrow/internal/service" t "spend-sparrow/internal/template/transaction" + "spend-sparrow/internal/treasure_chest" + "spend-sparrow/internal/treasure_chest_types" "spend-sparrow/internal/types" "spend-sparrow/internal/utils" "strconv" @@ -24,11 +26,11 @@ type Transaction interface { type TransactionImpl struct { s service.Transaction account account.Service - treasureChest service.TreasureChest + treasureChest treasure_chest.Service r *core.Render } -func NewTransaction(s service.Transaction, account account.Service, treasureChest service.TreasureChest, r *core.Render) Transaction { +func NewTransaction(s service.Transaction, account account.Service, treasureChest treasure_chest.Service, r *core.Render) Transaction { return TransactionImpl{ s: s, account: account, @@ -280,7 +282,7 @@ func (h TransactionImpl) handleDeleteTransaction() http.HandlerFunc { } } -func (h TransactionImpl) getTransactionData(accounts []*account.Account, treasureChests []*types.TreasureChest) (map[uuid.UUID]string, map[uuid.UUID]string) { +func (h TransactionImpl) getTransactionData(accounts []*account.Account, treasureChests []*treasure_chest_types.TreasureChest) (map[uuid.UUID]string, map[uuid.UUID]string) { accountMap := make(map[uuid.UUID]string, 0) for _, account := range accounts { accountMap[account.Id] = account.Name diff --git a/internal/service/transaction.go b/internal/service/transaction.go index bffe587..ecf7784 100644 --- a/internal/service/transaction.go +++ b/internal/service/transaction.go @@ -7,6 +7,7 @@ import ( "log/slog" "spend-sparrow/internal/auth_types" "spend-sparrow/internal/core" + "spend-sparrow/internal/treasure_chest_types" "spend-sparrow/internal/types" "strconv" "time" @@ -485,7 +486,7 @@ func (s TransactionImpl) validateAndEnrichTransaction(ctx context.Context, tx *s } if input.TreasureChestId != nil { - var treasureChest types.TreasureChest + var treasureChest treasure_chest_types.TreasureChest err = tx.GetContext(ctx, &treasureChest, `SELECT * FROM treasure_chest WHERE id = ? AND user_id = ?`, input.TreasureChestId, userId) err = core.TransformAndLogDbError(ctx, "transaction validate", nil, err) if err != nil { diff --git a/internal/service/transaction_recurring.go b/internal/service/transaction_recurring.go index 72769d2..1ac0b6b 100644 --- a/internal/service/transaction_recurring.go +++ b/internal/service/transaction_recurring.go @@ -8,6 +8,7 @@ import ( "math" "spend-sparrow/internal/auth_types" "spend-sparrow/internal/core" + "spend-sparrow/internal/treasure_chest_types" "spend-sparrow/internal/types" "strconv" "time" @@ -444,7 +445,7 @@ func (s TransactionRecurringImpl) validateAndEnrichTransactionRecurring( return nil, fmt.Errorf("could not parse treasureChestId: %w", core.ErrBadRequest) } treasureChestUuid = &temp - var treasureChest types.TreasureChest + var treasureChest treasure_chest_types.TreasureChest err = tx.GetContext(ctx, &treasureChest, `SELECT * FROM treasure_chest WHERE id = ? AND user_id = ?`, treasureChestUuid, userId) err = core.TransformAndLogDbError(ctx, "transactionRecurring validate", nil, err) if err != nil { diff --git a/internal/template/transaction/transaction.templ b/internal/template/transaction/transaction.templ index e55b208..a5a1a34 100644 --- a/internal/template/transaction/transaction.templ +++ b/internal/template/transaction/transaction.templ @@ -1,13 +1,16 @@ package transaction -import "fmt" -import "time" -import "spend-sparrow/internal/template/svg" -import "spend-sparrow/internal/types" -import "spend-sparrow/internal/account" -import "github.com/google/uuid" +import ( + "fmt" + "github.com/google/uuid" + "spend-sparrow/internal/account" + "spend-sparrow/internal/template/svg" + "spend-sparrow/internal/treasure_chest_types" + "spend-sparrow/internal/types" + "time" +) -templ Transaction(items templ.Component, filter types.TransactionItemsFilter, accounts []*account.Account, treasureChests []*types.TreasureChest) { +templ Transaction(items templ.Component, filter types.TransactionItemsFilter, accounts []*account.Account, treasureChests []*treasure_chest_types.TreasureChest) {
} -templ EditTransaction(transaction *types.Transaction, accounts []*account.Account, treasureChests []*types.TreasureChest) { +templ EditTransaction(transaction *types.Transaction, accounts []*account.Account, treasureChests []*treasure_chest_types.TreasureChest) { {{ var ( timestamp time.Time diff --git a/internal/template/treasurechest/default.go b/internal/template/treasurechest/default.go deleted file mode 100644 index 2062c31..0000000 --- a/internal/template/treasurechest/default.go +++ /dev/null @@ -1 +0,0 @@ -package treasurechest diff --git a/internal/handler/treasure_chest.go b/internal/treasure_chest/handler.go similarity index 72% rename from internal/handler/treasure_chest.go rename to internal/treasure_chest/handler.go index 9cff221..58c561c 100644 --- a/internal/handler/treasure_chest.go +++ b/internal/treasure_chest/handler.go @@ -1,11 +1,11 @@ -package handler +package treasure_chest import ( "net/http" "spend-sparrow/internal/core" "spend-sparrow/internal/service" tr "spend-sparrow/internal/template/transaction_recurring" - t "spend-sparrow/internal/template/treasurechest" + "spend-sparrow/internal/treasure_chest_types" "spend-sparrow/internal/types" "spend-sparrow/internal/utils" @@ -13,32 +13,32 @@ import ( "github.com/google/uuid" ) -type TreasureChest interface { +type Handler interface { Handle(router *http.ServeMux) } -type TreasureChestImpl struct { - s service.TreasureChest +type HandlerImpl struct { + s Service transactionRecurring service.TransactionRecurring r *core.Render } -func NewTreasureChest(s service.TreasureChest, transactionRecurring service.TransactionRecurring, r *core.Render) TreasureChest { - return TreasureChestImpl{ +func NewHandler(s Service, transactionRecurring service.TransactionRecurring, r *core.Render) Handler { + return HandlerImpl{ s: s, transactionRecurring: transactionRecurring, r: r, } } -func (h TreasureChestImpl) Handle(r *http.ServeMux) { - r.Handle("GET /treasurechest", h.handleTreasureChestPage()) - r.Handle("GET /treasurechest/{id}", h.handleTreasureChestItemComp()) - r.Handle("POST /treasurechest/{id}", h.handleUpdateTreasureChest()) - r.Handle("DELETE /treasurechest/{id}", h.handleDeleteTreasureChest()) +func (h HandlerImpl) Handle(r *http.ServeMux) { + r.Handle("GET /treasurechest", h.handleHandlerPage()) + r.Handle("GET /treasurechest/{id}", h.handleHandlerItemComp()) + r.Handle("POST /treasurechest/{id}", h.handleUpdateHandler()) + r.Handle("DELETE /treasurechest/{id}", h.handleDeleteHandler()) } -func (h TreasureChestImpl) handleTreasureChestPage() http.HandlerFunc { +func (h HandlerImpl) handleHandlerPage() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { core.UpdateSpan(r) @@ -62,12 +62,12 @@ func (h TreasureChestImpl) handleTreasureChestPage() http.HandlerFunc { monthlySums := h.calculateMonthlySums(treasureChests, transactionsRecurring) - comp := t.TreasureChest(treasureChests, monthlySums) + comp := TreasureChestComp(treasureChests, monthlySums) h.r.RenderLayout(r, w, comp, user) } } -func (h TreasureChestImpl) handleTreasureChestItemComp() http.HandlerFunc { +func (h HandlerImpl) handleHandlerItemComp() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { core.UpdateSpan(r) @@ -85,7 +85,7 @@ func (h TreasureChestImpl) handleTreasureChestItemComp() http.HandlerFunc { id := r.PathValue("id") if id == "new" { - comp := t.EditTreasureChest(nil, treasureChests, nil) + comp := EditTreasureChest(nil, treasureChests, nil) h.r.Render(r, w, comp) return } @@ -105,16 +105,16 @@ func (h TreasureChestImpl) handleTreasureChestItemComp() http.HandlerFunc { var comp templ.Component if r.URL.Query().Get("edit") == "true" { - comp = t.EditTreasureChest(treasureChest, treasureChests, transactionsRec) + comp = EditTreasureChest(treasureChest, treasureChests, transactionsRec) } else { monthlySums := h.calculateMonthlySums(treasureChests, transactionsRecurring) - comp = t.TreasureChestItem(treasureChest, monthlySums) + comp = TreasureChestItem(treasureChest, monthlySums) } h.r.Render(r, w, comp) } } -func (h TreasureChestImpl) handleUpdateTreasureChest() http.HandlerFunc { +func (h HandlerImpl) handleUpdateHandler() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { core.UpdateSpan(r) @@ -125,7 +125,7 @@ func (h TreasureChestImpl) handleUpdateTreasureChest() http.HandlerFunc { } var ( - treasureChest *types.TreasureChest + treasureChest *treasure_chest_types.TreasureChest err error ) id := r.PathValue("id") @@ -151,15 +151,15 @@ func (h TreasureChestImpl) handleUpdateTreasureChest() http.HandlerFunc { return } - treasureChests := make([]*types.TreasureChest, 1) + treasureChests := make([]*treasure_chest_types.TreasureChest, 1) treasureChests[0] = treasureChest monthlySums := h.calculateMonthlySums(treasureChests, transactionsRecurring) - comp := t.TreasureChestItem(treasureChest, monthlySums) + comp := TreasureChestItem(treasureChest, monthlySums) h.r.Render(r, w, comp) } } -func (h TreasureChestImpl) handleDeleteTreasureChest() http.HandlerFunc { +func (h HandlerImpl) handleDeleteHandler() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { core.UpdateSpan(r) @@ -179,8 +179,8 @@ func (h TreasureChestImpl) handleDeleteTreasureChest() http.HandlerFunc { } } -func (h TreasureChestImpl) calculateMonthlySums( - treasureChests []*types.TreasureChest, +func (h HandlerImpl) calculateMonthlySums( + treasureChests []*treasure_chest_types.TreasureChest, transactionsRecurring []*types.TransactionRecurring, ) map[uuid.UUID]int64 { monthlySums := make(map[uuid.UUID]int64) diff --git a/internal/service/treasure_chest.go b/internal/treasure_chest/service.go similarity index 79% rename from internal/service/treasure_chest.go rename to internal/treasure_chest/service.go index 5a1e5fa..302d56b 100644 --- a/internal/service/treasure_chest.go +++ b/internal/treasure_chest/service.go @@ -1,4 +1,4 @@ -package service +package treasure_chest import ( "context" @@ -8,35 +8,35 @@ import ( "slices" "spend-sparrow/internal/auth_types" "spend-sparrow/internal/core" - "spend-sparrow/internal/types" + "spend-sparrow/internal/treasure_chest_types" "github.com/google/uuid" "github.com/jmoiron/sqlx" ) -type TreasureChest interface { - Add(ctx context.Context, user *auth_types.User, parentId, name string) (*types.TreasureChest, error) - Update(ctx context.Context, user *auth_types.User, id, parentId, name string) (*types.TreasureChest, error) - Get(ctx context.Context, user *auth_types.User, id string) (*types.TreasureChest, error) - GetAll(ctx context.Context, user *auth_types.User) ([]*types.TreasureChest, error) +type Service interface { + Add(ctx context.Context, user *auth_types.User, parentId, name string) (*treasure_chest_types.TreasureChest, error) + Update(ctx context.Context, user *auth_types.User, id, parentId, name string) (*treasure_chest_types.TreasureChest, error) + Get(ctx context.Context, user *auth_types.User, id string) (*treasure_chest_types.TreasureChest, error) + GetAll(ctx context.Context, user *auth_types.User) ([]*treasure_chest_types.TreasureChest, error) Delete(ctx context.Context, user *auth_types.User, id string) error } -type TreasureChestImpl struct { +type ServiceImpl struct { db *sqlx.DB clock core.Clock random core.Random } -func NewTreasureChest(db *sqlx.DB, random core.Random, clock core.Clock) TreasureChest { - return TreasureChestImpl{ +func NewService(db *sqlx.DB, random core.Random, clock core.Clock) Service { + return ServiceImpl{ db: db, clock: clock, random: random, } } -func (s TreasureChestImpl) Add(ctx context.Context, user *auth_types.User, parentId, name string) (*types.TreasureChest, error) { +func (s ServiceImpl) Add(ctx context.Context, user *auth_types.User, parentId, name string) (*treasure_chest_types.TreasureChest, error) { if user == nil { return nil, core.ErrUnauthorized } @@ -63,7 +63,7 @@ func (s TreasureChestImpl) Add(ctx context.Context, user *auth_types.User, paren parentUuid = &parent.Id } - treasureChest := &types.TreasureChest{ + treasureChest := &treasure_chest_types.TreasureChest{ Id: newId, ParentId: parentUuid, UserId: user.Id, @@ -89,7 +89,7 @@ func (s TreasureChestImpl) Add(ctx context.Context, user *auth_types.User, paren return treasureChest, nil } -func (s TreasureChestImpl) Update(ctx context.Context, user *auth_types.User, idStr, parentId, name string) (*types.TreasureChest, error) { +func (s ServiceImpl) Update(ctx context.Context, user *auth_types.User, idStr, parentId, name string) (*treasure_chest_types.TreasureChest, error) { if user == nil { return nil, core.ErrUnauthorized } @@ -112,7 +112,7 @@ func (s TreasureChestImpl) Update(ctx context.Context, user *auth_types.User, id _ = tx.Rollback() }() - treasureChest := &types.TreasureChest{} + treasureChest := &treasure_chest_types.TreasureChest{} err = tx.GetContext(ctx, treasureChest, `SELECT * FROM treasure_chest WHERE user_id = ? AND id = ?`, user.Id, id) err = core.TransformAndLogDbError(ctx, "treasureChest Update", nil, err) if err != nil { @@ -171,7 +171,7 @@ func (s TreasureChestImpl) Update(ctx context.Context, user *auth_types.User, id return treasureChest, nil } -func (s TreasureChestImpl) Get(ctx context.Context, user *auth_types.User, id string) (*types.TreasureChest, error) { +func (s ServiceImpl) Get(ctx context.Context, user *auth_types.User, id string) (*treasure_chest_types.TreasureChest, error) { if user == nil { return nil, core.ErrUnauthorized } @@ -181,7 +181,7 @@ func (s TreasureChestImpl) Get(ctx context.Context, user *auth_types.User, id st return nil, fmt.Errorf("could not parse Id: %w", core.ErrBadRequest) } - var treasureChest types.TreasureChest + var treasureChest treasure_chest_types.TreasureChest err = s.db.GetContext(ctx, &treasureChest, `SELECT * FROM treasure_chest WHERE user_id = ? AND id = ?`, user.Id, uuid) err = core.TransformAndLogDbError(ctx, "treasureChest Get", nil, err) if err != nil { @@ -194,12 +194,12 @@ func (s TreasureChestImpl) Get(ctx context.Context, user *auth_types.User, id st return &treasureChest, nil } -func (s TreasureChestImpl) GetAll(ctx context.Context, user *auth_types.User) ([]*types.TreasureChest, error) { +func (s ServiceImpl) GetAll(ctx context.Context, user *auth_types.User) ([]*treasure_chest_types.TreasureChest, error) { if user == nil { return nil, core.ErrUnauthorized } - treasureChests := make([]*types.TreasureChest, 0) + treasureChests := make([]*treasure_chest_types.TreasureChest, 0) err := s.db.SelectContext(ctx, &treasureChests, `SELECT * FROM treasure_chest WHERE user_id = ?`, user.Id) err = core.TransformAndLogDbError(ctx, "treasureChest GetAll", nil, err) if err != nil { @@ -209,7 +209,7 @@ func (s TreasureChestImpl) GetAll(ctx context.Context, user *auth_types.User) ([ return SortTreasureChests(treasureChests), nil } -func (s TreasureChestImpl) Delete(ctx context.Context, user *auth_types.User, idStr string) error { +func (s ServiceImpl) Delete(ctx context.Context, user *auth_types.User, idStr string) error { if user == nil { return core.ErrUnauthorized } @@ -278,12 +278,12 @@ func (s TreasureChestImpl) Delete(ctx context.Context, user *auth_types.User, id return nil } -func SortTreasureChests(nodes []*types.TreasureChest) []*types.TreasureChest { +func SortTreasureChests(nodes []*treasure_chest_types.TreasureChest) []*treasure_chest_types.TreasureChest { var ( - roots []*types.TreasureChest + roots []*treasure_chest_types.TreasureChest ) - children := make(map[uuid.UUID][]*types.TreasureChest) - result := make([]*types.TreasureChest, 0) + children := make(map[uuid.UUID][]*treasure_chest_types.TreasureChest) + result := make([]*treasure_chest_types.TreasureChest, 0) for _, node := range nodes { if node.ParentId == nil { @@ -293,7 +293,7 @@ func SortTreasureChests(nodes []*types.TreasureChest) []*types.TreasureChest { } } - slices.SortFunc(roots, func(a, b *types.TreasureChest) int { + slices.SortFunc(roots, func(a, b *treasure_chest_types.TreasureChest) int { return compareStrings(a.Name, b.Name) }) @@ -302,7 +302,7 @@ func SortTreasureChests(nodes []*types.TreasureChest) []*types.TreasureChest { childList := children[root.Id] - slices.SortFunc(childList, func(a, b *types.TreasureChest) int { + slices.SortFunc(childList, func(a, b *treasure_chest_types.TreasureChest) int { return compareStrings(a.Name, b.Name) }) result = append(result, childList...) diff --git a/internal/template/treasurechest/treasure_chest.templ b/internal/treasure_chest/treasure_chest.templ similarity index 76% rename from internal/template/treasurechest/treasure_chest.templ rename to internal/treasure_chest/treasure_chest.templ index 5477b21..2a42448 100644 --- a/internal/template/treasurechest/treasure_chest.templ +++ b/internal/treasure_chest/treasure_chest.templ @@ -1,10 +1,13 @@ -package treasurechest +package treasure_chest -import "spend-sparrow/internal/template/svg" -import "spend-sparrow/internal/types" -import "github.com/google/uuid" +import ( + "github.com/google/uuid" + "spend-sparrow/internal/template/svg" + "spend-sparrow/internal/treasure_chest_types" + "spend-sparrow/internal/types" +) -templ TreasureChest(treasureChests []*types.TreasureChest, monthlySums map[uuid.UUID]int64) { +templ TreasureChestComp(treasureChests []*treasure_chest_types.TreasureChest, monthlySums map[uuid.UUID]int64) {