feat: create list now runs in a transaction

This commit is contained in:
2026-09-01 17:03:04 +02:00
parent 62a5324e6d
commit 3778ab7d58
2 changed files with 18 additions and 8 deletions
+1 -1
View File
@@ -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)
+14 -4
View File
@@ -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)
var list *List
err := s.tx.WithinTx(ctx, func(ctx context.Context) error {
l, err := s.repo.CreateList(ctx, name)
if err != nil {
return nil, err
return err
}
err = s.repo.AddUserToList(ctx, list.ID, authUserID)
if err != nil {
return err
}
list = l
return nil
})
if err != nil {
return nil, err
}