Skip to content

Commit

Permalink
Mapping create account endpoint to app
Browse files Browse the repository at this point in the history
  • Loading branch information
KrylixZA committed Jul 24, 2024
1 parent 1aa76a3 commit 5fd1ec5
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ root = true
# Unix-style newlines with a newline ending every file
[*]
indent_style = space
indent_size = 2
indent_size = 4
end_of_line = lf
insert_final_newline = true
7 changes: 7 additions & 0 deletions src/accounts/account.go
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"`
}
9 changes: 9 additions & 0 deletions src/accounts/account_types.go
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
)
95 changes: 95 additions & 0 deletions src/accounts/accounts.go
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
}
28 changes: 0 additions & 28 deletions src/controllers/accounts.go

This file was deleted.

10 changes: 10 additions & 0 deletions src/dapr/state.accounts.yaml
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
5 changes: 3 additions & 2 deletions src/main.go
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")
}

0 comments on commit 5fd1ec5

Please sign in to comment.