-
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.
Mapping create account endpoint to app
- Loading branch information
Showing
7 changed files
with
125 additions
and
31 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
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,7 @@ | ||
package accounts | ||
|
||
type account struct { | ||
AccountID string `json:"accountId"` | ||
AccountType AccountType `json:"accountType"` | ||
Balance float64 `json:"balance"` | ||
} |
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,9 @@ | ||
package accounts | ||
|
||
type AccountType int | ||
|
||
const ( | ||
Cheque AccountType = iota | ||
Savings | ||
Credit | ||
) |
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,95 @@ | ||
package accounts | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
dapr "github.com/dapr/go-sdk/client" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
const state = "accounts-state" | ||
const validationFailureErrorMessage = "model validation failure while attempting to map request to type" | ||
const notFoundErrorMessage = "reesource not found" | ||
const internalServerErrorMessage = "an environmental, non-specific error has occurred" | ||
|
||
func GetAccountsForUser(c *gin.Context) { | ||
userId := c.Param("userId") | ||
|
||
accounts, err := getAccountsForUser(userId, c) | ||
if err != nil { | ||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": internalServerErrorMessage}) | ||
return | ||
} | ||
|
||
if accounts == nil || len(accounts) == 0 { | ||
c.IndentedJSON(http.StatusNotFound, gin.H{"message": notFoundErrorMessage}) | ||
return | ||
} | ||
|
||
c.IndentedJSON(http.StatusOK, accounts) | ||
} | ||
|
||
func CreateAccountForUser(c *gin.Context) { | ||
userId := c.Param("userId") | ||
var account account | ||
|
||
accounts, err := getAccountsForUser(userId, c) | ||
if err != nil { | ||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": internalServerErrorMessage}) | ||
return | ||
} | ||
|
||
if err := c.ShouldBindJSON(&account); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"message": validationFailureErrorMessage}) | ||
return | ||
} | ||
|
||
accounts = append(accounts, account) | ||
if err := saveAccountsForUser(userId, accounts, c); err != nil { | ||
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": internalServerErrorMessage}) | ||
return | ||
} | ||
|
||
c.IndentedJSON(http.StatusCreated, nil) | ||
} | ||
|
||
func getAccountsForUser(userId string, c *gin.Context) (accounts []account, err error) { | ||
client, err := dapr.NewClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer client.Close() | ||
|
||
daprStateItem, err := client.GetState(c, state, userId, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = json.Unmarshal(daprStateItem.Value, &accounts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return accounts, nil | ||
} | ||
|
||
func saveAccountsForUser(userId string, accounts []account, c *gin.Context) (err error) { | ||
client, err := dapr.NewClient() | ||
if err != nil { | ||
return err | ||
} | ||
defer client.Close() | ||
|
||
accountBytes, err := json.Marshal(accounts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = client.SaveState(c, state, userId, accountBytes, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file was deleted.
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,10 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: accounts-state | ||
spec: | ||
type: state.redis | ||
version: v1 | ||
metadata: | ||
- name: redisHost | ||
value: redis:6379 |
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 |
---|---|---|
@@ -1,13 +1,14 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/KrylixZA/bank-with-dapr/controllers" | ||
"github.com/KrylixZA/bank-with-dapr/accounts" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func main() { | ||
router := gin.Default() | ||
router.GET("/accounts/:id", controllers.GetAccounts) | ||
router.GET("/accounts/:userId", accounts.GetAccountsForUser) | ||
router.POST("/accounts/:userId", accounts.CreateAccountForUser) | ||
|
||
router.Run("localhost:8080") | ||
} |