@@ -83,106 +83,90 @@ func (r *CASBackendRepo) FindFallbackBackend(ctx context.Context, orgID uuid.UUI
83
83
84
84
// Create creates a new CAS backend in the given organization
85
85
// If it's set as default, it will unset the previous default backend
86
- func (r * CASBackendRepo ) Create (ctx context.Context , opts * biz.CASBackendCreateOpts ) (b * biz.CASBackend , err error ) {
87
- tx , err := r .data .DB .Tx (ctx )
88
- if err != nil {
89
- return nil , fmt .Errorf ("failed to create transaction: %w" , err )
90
- }
86
+ func (r * CASBackendRepo ) Create (ctx context.Context , opts * biz.CASBackendCreateOpts ) (* biz.CASBackend , error ) {
87
+ var (
88
+ backend * ent.CASBackend
89
+ err error
90
+ )
91
+ if err := WithTx (ctx , r .data .DB , func (tx * ent.Tx ) error {
92
+ // 1 - unset default backend for all the other backends in the org
93
+ if opts .Default {
94
+ if err := tx .CASBackend .Update ().
95
+ Where (casbackend .HasOrganizationWith (organization .ID (opts .OrgID ))).
96
+ Where (casbackend .Default (true )).
97
+ SetDefault (false ).
98
+ Exec (ctx ); err != nil {
99
+ return fmt .Errorf ("failed to clear previous default backend: %w" , err )
100
+ }
101
+ }
91
102
92
- defer func () {
93
- // Unblock the row if there was an error
103
+ // 2 - create the new backend and set it as default if needed
104
+ backend , err = tx .CASBackend .Create ().
105
+ SetName (opts .Name ).
106
+ SetOrganizationID (opts .OrgID ).
107
+ SetLocation (opts .Location ).
108
+ SetDescription (opts .Description ).
109
+ SetFallback (opts .Fallback ).
110
+ SetProvider (opts .Provider ).
111
+ SetDefault (opts .Default ).
112
+ SetSecretName (opts .SecretName ).
113
+ SetMaxBlobSizeBytes (opts .MaxBytes ).
114
+ Save (ctx )
94
115
if err != nil {
95
- _ = tx .Rollback ()
96
- }
97
- }()
98
-
99
- // 1 - unset default backend for all the other backends in the org
100
- if opts .Default {
101
- if err := tx .CASBackend .Update ().
102
- Where (casbackend .HasOrganizationWith (organization .ID (opts .OrgID ))).
103
- Where (casbackend .Default (true )).
104
- SetDefault (false ).
105
- Exec (ctx ); err != nil {
106
- return nil , fmt .Errorf ("failed to clear previous default backend: %w" , err )
107
- }
108
- }
116
+ if ent .IsConstraintError (err ) {
117
+ return biz .NewErrAlreadyExists (err )
118
+ }
109
119
110
- // 2 - create the new backend and set it as default if needed
111
- backend , err := tx .CASBackend .Create ().
112
- SetName (opts .Name ).
113
- SetOrganizationID (opts .OrgID ).
114
- SetLocation (opts .Location ).
115
- SetDescription (opts .Description ).
116
- SetFallback (opts .Fallback ).
117
- SetProvider (opts .Provider ).
118
- SetDefault (opts .Default ).
119
- SetSecretName (opts .SecretName ).
120
- SetMaxBlobSizeBytes (opts .MaxBytes ).
121
- Save (ctx )
122
- if err != nil {
123
- if ent .IsConstraintError (err ) {
124
- return nil , biz .NewErrAlreadyExists (err )
120
+ return fmt .Errorf ("failed to create backend: %w" , err )
125
121
}
126
-
127
- return nil , fmt .Errorf ("failed to create backend: %w" , err )
128
- }
129
-
130
- // 3 - commit the transaction
131
- if err := tx .Commit (); err != nil {
132
- return nil , fmt .Errorf ("failed to commit transaction: %w" , err )
122
+ return nil
123
+ }); err != nil {
124
+ return nil , err
133
125
}
134
126
135
127
// Return the backend from the DB to have consistent marshalled object
136
128
return r .FindByID (ctx , backend .ID )
137
129
}
138
130
139
- func (r * CASBackendRepo ) Update (ctx context.Context , opts * biz.CASBackendUpdateOpts ) (b * biz.CASBackend , err error ) {
140
- tx , err := r .data .DB .Tx (ctx )
141
- if err != nil {
142
- return nil , fmt .Errorf ("failed to create transaction: %w" , err )
143
- }
144
-
145
- defer func () {
146
- // Unblock the row if there was an error
147
- if err != nil {
148
- _ = tx .Rollback ()
131
+ func (r * CASBackendRepo ) Update (ctx context.Context , opts * biz.CASBackendUpdateOpts ) (* biz.CASBackend , error ) {
132
+ var (
133
+ backend * ent.CASBackend
134
+ err error
135
+ )
136
+ if err = WithTx (ctx , r .data .DB , func (tx * ent.Tx ) error {
137
+ // 1 - unset default backend for all the other backends in the org
138
+ if opts .Default {
139
+ if err := tx .CASBackend .Update ().
140
+ Where (casbackend .HasOrganizationWith (organization .ID (opts .OrgID ))).
141
+ Where (casbackend .Default (true )).
142
+ SetDefault (false ).
143
+ Exec (ctx ); err != nil {
144
+ return fmt .Errorf ("failed to clear previous default backend: %w" , err )
145
+ }
149
146
}
150
- }()
151
-
152
- // 1 - unset default backend for all the other backends in the org
153
- if opts .Default {
154
- if err := tx .CASBackend .Update ().
155
- Where (casbackend .HasOrganizationWith (organization .ID (opts .OrgID ))).
156
- Where (casbackend .Default (true )).
157
- SetDefault (false ).
158
- Exec (ctx ); err != nil {
159
- return nil , fmt .Errorf ("failed to clear previous default backend: %w" , err )
160
- }
161
- }
162
147
163
- // 2 - Chain the list of updates
164
- // TODO: allow setting values as empty, currently it's not possible.
165
- // We do it in other models by providing pointers to string + setNillableX methods
166
- updateChain := tx .CASBackend .UpdateOneID (opts .ID ).SetDefault (opts .Default )
167
- if opts .Description != "" {
168
- updateChain = updateChain .SetDescription (opts .Description )
169
- }
148
+ // 2 - Chain the list of updates
149
+ // TODO: allow setting values as empty, currently it's not possible.
150
+ // We do it in other models by providing pointers to string + setNillableX methods
151
+ updateChain := tx .CASBackend .UpdateOneID (opts .ID ).SetDefault (opts .Default )
152
+ if opts .Description != "" {
153
+ updateChain = updateChain .SetDescription (opts .Description )
154
+ }
170
155
171
- // If secretName is provided we set it
172
- if opts .SecretName != "" {
173
- updateChain = updateChain .SetSecretName (opts .SecretName )
174
- }
156
+ // If secretName is provided we set it
157
+ if opts .SecretName != "" {
158
+ updateChain = updateChain .SetSecretName (opts .SecretName )
159
+ }
175
160
176
- backend , err := updateChain .Save (ctx )
177
- if err != nil {
161
+ backend , err = updateChain .Save (ctx )
162
+ if err != nil {
163
+ return err
164
+ }
165
+ return nil
166
+ }); err != nil {
178
167
return nil , err
179
168
}
180
169
181
- // 3 - commit the transaction
182
- if err := tx .Commit (); err != nil {
183
- return nil , fmt .Errorf ("failed to commit transaction: %w" , err )
184
- }
185
-
186
170
return r .FindByID (ctx , backend .ID )
187
171
}
188
172
0 commit comments