-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add register user and create profile tx
- Loading branch information
Showing
5 changed files
with
114 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
func (store *SQLStore) execTx(ctx context.Context, fn func(*Queries) error) error { | ||
tx, err := store.connPool.Begin(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
// * Note [codermuss]: Begin db transaction | ||
q := New(tx) | ||
// * Note [codermuss]: execute query | ||
err = fn(q) | ||
// * Note [codermuss]: if there is an error Rollback the query | ||
if err != nil { | ||
if rbErr := tx.Rollback(ctx); rbErr != nil { | ||
return fmt.Errorf("tx err: %v, rb err: %v", err, rbErr) | ||
} | ||
return err | ||
} | ||
return tx.Commit(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/jackc/pgx/v5/pgtype" | ||
) | ||
|
||
type RegisterUserTxParams struct { | ||
InsertUserParams | ||
// * Note [codermuss]: This function will be executed after the user is inserted, | ||
// * Note [codermuss]: inside the same transaction. And its output error will be used to decide | ||
// * Note [codermuss]: whether to commit or rollback the transaction. | ||
AfterCreate func(user User) error | ||
} | ||
type RegisterUserTxResult struct { | ||
User User | ||
Profile Profile | ||
} | ||
|
||
// * Note [codermuss]: This method responsible with creating user. | ||
// * Note [codermuss]: It uses execTx to handle DB Transaction error | ||
func (store *SQLStore) RegisterUserTx(ctx context.Context, arg RegisterUserTxParams) (RegisterUserTxResult, error) { | ||
var result RegisterUserTxResult | ||
|
||
err := store.execTx(ctx, func(q *Queries) error { | ||
var err error | ||
result.User, err = q.InsertUser(ctx, arg.InsertUserParams) | ||
if err != nil { | ||
return err | ||
} | ||
profileArg := InsertProfileParams{ | ||
UserID: result.User.ID, | ||
Bio: pgtype.Text{Valid: true, String: ""}, | ||
PostCount: pgtype.Int4{ | ||
Valid: true, Int32: 0, | ||
}, | ||
LikeCount: pgtype.Int4{ | ||
Valid: true, Int32: 0, | ||
}, | ||
FollowerCount: pgtype.Int4{ | ||
Valid: true, Int32: 0, | ||
}, | ||
} | ||
|
||
result.Profile, err = q.InsertProfile(ctx, profileArg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return arg.AfterCreate(result.User) | ||
}) | ||
return result, err | ||
} |