Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions api/account.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package api

import (
"database/sql"
"errors"
"net/http"

db "github.com/Samudra-G/simplebank/db/sqlc"
"github.com/Samudra-G/simplebank/token"
"github.com/gin-gonic/gin"
"github.com/lib/pq"
)

type createAccountRequest struct {
Expand All @@ -31,12 +29,10 @@ func (server *Server) createAccount(ctx *gin.Context) {

account, err := server.store.CreateAccount(ctx, arg)
if err != nil {
if pqErr, ok := err.(*pq.Error); ok {
switch pqErr.Code.Name() {
case "foreign_key_violation", "unique_violation":
ctx.JSON(http.StatusForbidden, errorResponse(err))
return
}
errCode := db.ErrorCode(err)
if errCode == db.ForeignKeyViolation || errCode == db.UniqueViolation{
ctx.JSON(http.StatusForbidden, errorResponse(err))
return
}
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
Expand All @@ -57,7 +53,7 @@ func (server *Server) getAccount(ctx *gin.Context) {

account, err := server.store.GetAccount(ctx, req.ID)
if err != nil {
if err == sql.ErrNoRows {
if errors.Is(err, db.ErrRecordNotFound) {
ctx.JSON(http.StatusNotFound, errorResponse(err))
return
}
Expand Down
2 changes: 1 addition & 1 deletion api/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestGetAccountAPI(t *testing.T){
store.EXPECT().
GetAccount(gomock.Any(), gomock.Eq(account.ID)).
Times(1).
Return(db.Account{}, sql.ErrNoRows)
Return(db.Account{}, db.ErrRecordNotFound)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusNotFound, recorder.Code)
Expand Down
10 changes: 3 additions & 7 deletions api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/Samudra-G/simplebank/util"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/lib/pq"
)

type createUserRequest struct {
Expand Down Expand Up @@ -57,12 +56,9 @@ func (server *Server) createUser(ctx *gin.Context) {

user, err := server.store.CreateUser(ctx, arg)
if err != nil {
if pqErr, ok := err.(*pq.Error); ok {
switch pqErr.Code.Name() {
case "unique_violation":
ctx.JSON(http.StatusForbidden, errorResponse(err))
return
}
if db.ErrorCode(err) == db.UniqueViolation{
ctx.JSON(http.StatusForbidden, errorResponse(err))
return
}
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
Expand Down
3 changes: 1 addition & 2 deletions api/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/Samudra-G/simplebank/util"
"github.com/gin-gonic/gin"
"github.com/golang/mock/gomock"
"github.com/lib/pq"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -118,7 +117,7 @@ func TestCreateUserAPI(t *testing.T) {
store.EXPECT().
CreateUser(gomock.Any(), gomock.Any()).
Times(1).
Return(db.User{}, &pq.Error{Code: "23505"})
Return(db.User{},db.ErrUniqueViolation)
},
checkResponse: func(t *testing.T, rec *httptest.ResponseRecorder) {
require.Equal(t, http.StatusForbidden, rec.Code)
Expand Down
17 changes: 7 additions & 10 deletions db/sqlc/account.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions db/sqlc/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sqlc

import (
"context"
"database/sql"
"testing"
"time"

Expand All @@ -19,7 +18,7 @@ func createRandomAccount(t *testing.T) Account{
Currency: util.RandomCurrency(),
}

account, err := testQueries.CreateAccount(context.Background(), arg)
account, err := testStore.CreateAccount(context.Background(), arg)
require.NoError(t, err)
require.NotEmpty(t, account)

Expand All @@ -38,7 +37,7 @@ func TestCreateAccount(t *testing.T) {

func TestGetAccount(t *testing.T) {
account1 := createRandomAccount(t)
account2, err := testQueries.GetAccount(context.Background(), account1.ID)
account2, err := testStore.GetAccount(context.Background(), account1.ID)

require.NoError(t, err)
require.NotEmpty(t, account2)
Expand All @@ -59,7 +58,7 @@ func TestUpdateAccount(t *testing.T) {
Balance: util.RandomMoney(),
}

account2, err := testQueries.UpdateAccount(context.Background(), arg)
account2, err := testStore.UpdateAccount(context.Background(), arg)

require.NoError(t, err)
require.NotEmpty(t, account2)
Expand All @@ -74,12 +73,12 @@ func TestUpdateAccount(t *testing.T) {

func TestDeleteAccount(t *testing.T) {
account1 := createRandomAccount(t)
err := testQueries.DeleteAccount(context.Background(), account1.ID)
err := testStore.DeleteAccount(context.Background(), account1.ID)
require.NoError(t, err)

account2, err := testQueries.GetAccount(context.Background(), account1.ID)
account2, err := testStore.GetAccount(context.Background(), account1.ID)
require.Error(t, err)
require.EqualError(t, err, sql.ErrNoRows.Error())
require.EqualError(t, err, ErrRecordNotFound.Error())
require.Empty(t, account2)
}

Expand All @@ -94,7 +93,7 @@ func TestListAccount(t *testing.T) {
Offset: 0,
}

accounts, err := testQueries.ListAccounts(context.Background(), arg)
accounts, err := testStore.ListAccounts(context.Background(), arg)
require.NoError(t, err)
require.NotEmpty(t, accounts)

Expand Down
13 changes: 7 additions & 6 deletions db/sqlc/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions db/sqlc/entry.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions db/sqlc/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/Samudra-G/simplebank/util"
"github.com/stretchr/testify/require"
)

func createRandomEntry(t *testing.T, account Account) Entry {
Expand All @@ -15,7 +15,7 @@ func createRandomEntry(t *testing.T, account Account) Entry {
Amount: util.RandomMoney(),
}

entry, err := testQueries.CreateEntry(context.Background(), arg)
entry, err := testStore.CreateEntry(context.Background(), arg)
require.NoError(t, err)
require.NotEmpty(t, entry)

Expand All @@ -36,7 +36,7 @@ func TestCreateEntry(t *testing.T) {
func TestGetEntry(t *testing.T) {
account := createRandomAccount(t)
entry1 := createRandomEntry(t, account)
entry2, err := testQueries.GetEntry(context.Background(), entry1.ID)
entry2, err := testStore.GetEntry(context.Background(), entry1.ID)
require.NoError(t, err)
require.NotEmpty(t, entry2)

Expand All @@ -58,7 +58,7 @@ func TestListEntries(t *testing.T) {
Offset: 5,
}

entries, err := testQueries.ListEntries(context.Background(), arg)
entries, err := testStore.ListEntries(context.Background(), arg)
require.NoError(t, err)
require.Len(t, entries, 5)

Expand Down
26 changes: 26 additions & 0 deletions db/sqlc/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package sqlc

import (
"errors"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
const (
ForeignKeyViolation = "23503"
UniqueViolation = "23505"
)

var ErrRecordNotFound = pgx.ErrNoRows
var ErrUniqueViolation = &pgconn.PgError{
Code: UniqueViolation,
}

func ErrorCode(err error) string {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return pgErr.Code
}

return ""
}
24 changes: 24 additions & 0 deletions db/sqlc/exec_tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package sqlc

import (
"context"
"fmt"
)

//execTx executes a function within a database transaction
func (store *SQLStore) execTx(ctx context.Context, fn func(*Queries) error) error {
tx, err := store.connPool.Begin(ctx)
if err != nil {
return err
}
q := New(tx) //New accepts dbtx interface
err = fn(q)
if err != nil {
if rbErr := tx.Rollback(ctx); rbErr != nil {
return fmt.Errorf("tx error: %v, rb err: %v", err, rbErr)
}
return err
}

return tx.Commit(ctx)
}
Loading
Loading