Skip to content

Commit

Permalink
Update account handlers and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Delavalom authored Oct 7, 2023
1 parent 13cfd28 commit dad8e5a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 5 additions & 0 deletions api/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func (server *Server) createAccount(ctx *gin.Context) {
}
account, err := server.store.CreateAccount(ctx, arg)
if err != nil {
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 Down
7 changes: 4 additions & 3 deletions api/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (
)

func TestGetAccountAPI(t *testing.T) {
account := randomAccount()
user, _ := randomUser(t)
account := randomAccount(user.Username)

testCases := []struct {
name string
Expand Down Expand Up @@ -89,10 +90,10 @@ func TestGetAccountAPI(t *testing.T) {
}
}

func randomAccount() db.Account {
func randomAccount(username string) db.Account {
return db.Account{
ID: util.RandomInt(1, 1000),
Owner: util.RandomOwner(),
Owner: username,
Balance: util.RandomMoney(),
Currency: util.RandomCurrency(),
}
Expand Down

0 comments on commit dad8e5a

Please sign in to comment.