-
Notifications
You must be signed in to change notification settings - Fork 0
/
fundsHandling.go
62 lines (52 loc) · 1.82 KB
/
fundsHandling.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package funds
import (
"encoding/json"
"errors"
"net/http"
"github.com/Kaibling/psychic-octo-stock/lib/apierrors"
"github.com/Kaibling/psychic-octo-stock/lib/transmission"
"github.com/Kaibling/psychic-octo-stock/lib/utility"
"github.com/Kaibling/psychic-octo-stock/models"
"github.com/Kaibling/psychic-octo-stock/repositories"
"github.com/go-chi/chi"
)
type fundsData struct {
Amount float64 `json:"amount"`
Currency string `json:"currency"`
Provider string `json:"provider"`
Operation string `json:"operation"` //CHARGE, WITHDRAW
}
func fundsPost(w http.ResponseWriter, r *http.Request) {
response := transmission.GetResponse(r)
var newfundsData fundsData
erra := json.NewDecoder(r.Body).Decode(&newfundsData)
if erra != nil {
response.Send("", "post data not parsable", http.StatusUnprocessableEntity)
return
}
userID := chi.URLParam(r, "id")
userRepo := utility.GetContext("userRepo", r).(*repositories.UserRepository)
err := executeFundOperation(userID, newfundsData, userRepo)
if erra != nil {
response.Send("", err.Error(), err.HttpStatus())
return
}
loadedUser, _ := userRepo.GetByID(userID)
response.Send(loadedUser, "", http.StatusOK)
}
func executeFundOperation(userID string, data fundsData, userRepo *repositories.UserRepository) apierrors.ApiError {
switch operation := data.Operation; operation {
case "CHARGE":
//todo dont ignore Provider
addFunds(userID, models.MonetaryUnit{Amount: data.Amount, Currency: data.Currency}, userRepo)
case "WITHDRAW":
return apierrors.NewGeneralError(errors.New("lol, nope"))
default:
return apierrors.NewGeneralError(errors.New("operation '" + operation + "' not supported"))
}
return nil
}
func addFunds(userID string, mu models.MonetaryUnit, userRepo *repositories.UserRepository) apierrors.ApiError {
userRepo.AddFunds(userID, mu)
return nil
}