ft/add db.createAccountTx transaction
This commit is contained in:
parent
574ff62559
commit
800132d683
@ -29,7 +29,7 @@ func (server *Server) createAccount(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
arg := db.CreateAccountParams{
|
||||
arg := db.CreateAccountTxParams{
|
||||
Passwordhash: req.Passwordhash,
|
||||
Firstname: req.Firstname,
|
||||
Lastname: req.Lastname,
|
||||
@ -46,7 +46,7 @@ func (server *Server) createAccount(ctx *gin.Context) {
|
||||
},
|
||||
}
|
||||
|
||||
account, err := server.store.CreateAccount(ctx, arg)
|
||||
account, err := server.store.CreateAccountTx(ctx, arg)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
|
||||
return
|
||||
|
@ -44,7 +44,7 @@ func TestCreateAccountAPI(t *testing.T) {
|
||||
"creator": account.Creator,
|
||||
},
|
||||
buildStubs: func(store *mockdb.MockStore) {
|
||||
arg := db.CreateAccountParams{
|
||||
arg := db.CreateAccountTxParams{
|
||||
Passwordhash: account.Passwordhash,
|
||||
Firstname: account.Firstname,
|
||||
Lastname: account.Lastname,
|
||||
@ -58,14 +58,29 @@ func TestCreateAccountAPI(t *testing.T) {
|
||||
Creator: account.Creator,
|
||||
}
|
||||
|
||||
exp := db.CreateAccountTxResult{
|
||||
Account: account,
|
||||
}
|
||||
|
||||
store.EXPECT().
|
||||
CreateAccount(gomock.Any(), gomock.Eq(arg)).
|
||||
CreateAccountTx(gomock.Any(), gomock.Eq(arg)).
|
||||
Times(1).
|
||||
Return(account, nil)
|
||||
Return(exp, nil)
|
||||
},
|
||||
checkResponse: func(recorder *httptest.ResponseRecorder) {
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
requireBodyMatchAccount(t, recorder.Body, account)
|
||||
data, err := io.ReadAll(recorder.Body)
|
||||
require.NoError(t, err)
|
||||
|
||||
var getAccount db.CreateAccountTxResult
|
||||
err = json.Unmarshal(data, &getAccount)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t,
|
||||
db.CreateAccountTxResult{
|
||||
Account: account,
|
||||
},
|
||||
getAccount,
|
||||
)
|
||||
},
|
||||
},
|
||||
// {
|
||||
@ -89,9 +104,8 @@ func TestCreateAccountAPI(t *testing.T) {
|
||||
},
|
||||
buildStubs: func(store *mockdb.MockStore) {
|
||||
store.EXPECT().
|
||||
CreateAccount(gomock.Any(), gomock.Any()).
|
||||
Times(0).
|
||||
Return(db.Account{}, sql.ErrConnDone)
|
||||
CreateAccountTx(gomock.Any(), gomock.Any()).
|
||||
Times(0)
|
||||
},
|
||||
checkResponse: func(recorder *httptest.ResponseRecorder) {
|
||||
require.Equal(t, http.StatusBadRequest, recorder.Code)
|
||||
@ -114,9 +128,9 @@ func TestCreateAccountAPI(t *testing.T) {
|
||||
},
|
||||
buildStubs: func(store *mockdb.MockStore) {
|
||||
store.EXPECT().
|
||||
CreateAccount(gomock.Any(), gomock.Any()).
|
||||
CreateAccountTx(gomock.Any(), gomock.Any()).
|
||||
Times(1).
|
||||
Return(db.Account{}, sql.ErrConnDone)
|
||||
Return(db.CreateAccountTxResult{}, sql.ErrConnDone)
|
||||
},
|
||||
checkResponse: func(recorder *httptest.ResponseRecorder) {
|
||||
require.Equal(t, http.StatusInternalServerError, recorder.Code)
|
||||
|
@ -54,6 +54,21 @@ func (mr *MockStoreMockRecorder) CreateAccount(arg0, arg1 any) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateAccount", reflect.TypeOf((*MockStore)(nil).CreateAccount), arg0, arg1)
|
||||
}
|
||||
|
||||
// CreateAccountTx mocks base method.
|
||||
func (m *MockStore) CreateAccountTx(arg0 context.Context, arg1 db.CreateAccountTxParams) (db.CreateAccountTxResult, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "CreateAccountTx", arg0, arg1)
|
||||
ret0, _ := ret[0].(db.CreateAccountTxResult)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// CreateAccountTx indicates an expected call of CreateAccountTx.
|
||||
func (mr *MockStoreMockRecorder) CreateAccountTx(arg0, arg1 any) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateAccountTx", reflect.TypeOf((*MockStore)(nil).CreateAccountTx), arg0, arg1)
|
||||
}
|
||||
|
||||
// CreateDocumentMail mocks base method.
|
||||
func (m *MockStore) CreateDocumentMail(arg0 context.Context, arg1 db.CreateDocumentMailParams) (db.Document, error) {
|
||||
m.ctrl.T.Helper()
|
||||
@ -301,6 +316,21 @@ func (mr *MockStoreMockRecorder) GetAccount(arg0, arg1 any) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAccount", reflect.TypeOf((*MockStore)(nil).GetAccount), arg0, arg1)
|
||||
}
|
||||
|
||||
// GetAccountForUpdate mocks base method.
|
||||
func (m *MockStore) GetAccountForUpdate(arg0 context.Context, arg1 int64) (db.Account, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetAccountForUpdate", arg0, arg1)
|
||||
ret0, _ := ret[0].(db.Account)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// GetAccountForUpdate indicates an expected call of GetAccountForUpdate.
|
||||
func (mr *MockStoreMockRecorder) GetAccountForUpdate(arg0, arg1 any) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAccountForUpdate", reflect.TypeOf((*MockStore)(nil).GetAccountForUpdate), arg0, arg1)
|
||||
}
|
||||
|
||||
// GetDocument mocks base method.
|
||||
func (m *MockStore) GetDocument(arg0 context.Context, arg1 int64) (db.Document, error) {
|
||||
m.ctrl.T.Helper()
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
|
||||
type Store interface {
|
||||
Querier
|
||||
CreateAccountTx(ctx context.Context, arg CreateAccountTxParams) (CreateAccountTxResult, error)
|
||||
}
|
||||
|
||||
// Store provides all functions to execute db queries and transactions
|
||||
@ -17,7 +18,7 @@ type SQLStore struct {
|
||||
}
|
||||
|
||||
func NewStore(db *sql.DB) Store {
|
||||
return SQLStore{
|
||||
return &SQLStore{
|
||||
db: db,
|
||||
Queries: New(db),
|
||||
}
|
||||
|
51
db/sqlc/tx_create_account.go
Normal file
51
db/sqlc/tx_create_account.go
Normal file
@ -0,0 +1,51 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CreateAccountTxParams struct {
|
||||
Passwordhash string `json:"passwordhash"`
|
||||
Firstname string `json:"firstname"`
|
||||
Lastname string `json:"lastname"`
|
||||
Birthday time.Time `json:"birthday"`
|
||||
Email string `json:"email"`
|
||||
Phone sql.NullString `json:"phone"`
|
||||
City string `json:"city"`
|
||||
Zip string `json:"zip"`
|
||||
Street string `json:"street"`
|
||||
Country string `json:"country"`
|
||||
Creator string `json:"creator"`
|
||||
}
|
||||
|
||||
type CreateAccountTxResult struct {
|
||||
Account Account `json:"account"`
|
||||
}
|
||||
|
||||
func (store *SQLStore) CreateAccountTx(ctx context.Context, arg CreateAccountTxParams) (CreateAccountTxResult, error) {
|
||||
var result CreateAccountTxResult
|
||||
|
||||
err := store.execTx(ctx, func(q *Queries) error {
|
||||
var err error
|
||||
|
||||
result.Account, err = q.CreateAccount(ctx, CreateAccountParams(arg)) //{
|
||||
// Passwordhash: arg.Passwordhash,
|
||||
// Firstname: arg.Firstname,
|
||||
// Lastname: arg.Lastname,
|
||||
// Birthday: arg.Birthday,
|
||||
// City: arg.City,
|
||||
// Zip: arg.Zip,
|
||||
// Street: arg.Street,
|
||||
// Country: arg.Country,
|
||||
// Creator: arg.Creator,
|
||||
// Phone: arg.Phone,
|
||||
// Email: arg.Email,
|
||||
// })
|
||||
|
||||
return err
|
||||
})
|
||||
|
||||
return result, err
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user