From 3778ab7d584a2647688f2c5f455aee691c63caf7 Mon Sep 17 00:00:00 2001 From: Robin Dittmar Date: Tue, 1 Sep 2026 17:03:04 +0200 Subject: [PATCH] feat: create list now runs in a transaction --- internal/api/router/router.go | 2 +- internal/domain/list.go | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/internal/api/router/router.go b/internal/api/router/router.go index 2a3f3f2..fb94676 100644 --- a/internal/api/router/router.go +++ b/internal/api/router/router.go @@ -25,7 +25,7 @@ func NewMux(cfg Config) http.Handler { inviteService := domain.NewInviteService(store.Invite) userService := domain.NewUserService(store.User) registrationService := domain.NewRegistrationService(store, userService, inviteService) - listService := domain.NewListService(store.List) + listService := domain.NewListService(store, store.List) authHandler := handler.NewAuthHandler(authService) inviteHandler := handler.NewInviteHandler(inviteService) diff --git a/internal/domain/list.go b/internal/domain/list.go index a3f9240..22df20f 100644 --- a/internal/domain/list.go +++ b/internal/domain/list.go @@ -49,11 +49,12 @@ type ListRepository interface { } type ListService struct { + tx Transactor repo ListRepository } -func NewListService(r ListRepository) *ListService { - return &ListService{repo: r} +func NewListService(tx Transactor, r ListRepository) *ListService { + return &ListService{tx: tx, repo: r} } func (s *ListService) CreateList(ctx context.Context, authUserID string, name string) (*List, error) { @@ -61,12 +62,21 @@ func (s *ListService) CreateList(ctx context.Context, authUserID string, name st return nil, ErrListNameMissing } - list, err := s.repo.CreateList(ctx, name) - if err != nil { - return nil, err - } + var list *List + err := s.tx.WithinTx(ctx, func(ctx context.Context) error { + l, err := s.repo.CreateList(ctx, name) + if err != nil { + return err + } - err = s.repo.AddUserToList(ctx, list.ID, authUserID) + err = s.repo.AddUserToList(ctx, list.ID, authUserID) + if err != nil { + return err + } + + list = l + return nil + }) if err != nil { return nil, err }