Skip to content

Commit

Permalink
wip: invalid transaction suffering
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedsoupe committed Feb 23, 2024
1 parent 8f84080 commit 6cc15dd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 31 deletions.
61 changes: 47 additions & 14 deletions internal/transaction.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package internal

import (
"fmt"
"math/rand"
"net/http"
"time"

"github.com/labstack/echo/v4"
. "github.com/zoedsoupe/exo/changeset"
)

type Transaction struct {
Expand All @@ -17,51 +18,83 @@ type Transaction struct {
CreatedAt time.Time
}

type TransactionRequest struct {
Description string `json:"descricao"`
Value int `json:"valor"`
Type string `json:"tipo"`
}

type TransactionResponse struct {
Saldo int `json:"saldo"`
Limite int `json:"limite"`
}

var transactions []Transaction

var client = [5]Client{
var clients = [5]Client{
{1, "A", 1000 * 100, 0},
{2, "B", 800 * 100, 0},
{3, "C", 10000 * 100, 0},
{4, "D", 100000 * 100, 0},
{5, "E", 5000 * 100, 0},
}

func MakeTransaction(ID int, Value map[string]interface{}) (Client, error) {
var t Transaction
func MakeTransaction(ID int, req TransactionRequest) (Client, error) {
var client Client
if ID < 1 || ID > 5 {
return client, echo.NewHTTPError(http.StatusNotFound, nil)
return client, echo.NewHTTPError(http.StatusNotFound, "not found")
}
client = clients[ID]

c := Cast[Transaction](Value).
ValidateChange("Type", InclusionValidator{Allowed: []interface{}{"c", "d"}}).
ValidateChange("Description", LengthValidator{Min: 1, Max: 10})

t, err := ApplyNew[Transaction](c)
t, err := transactionRequestToTransaction(ID, req)
if err != nil {
fmt.Println(err.Error())
return client, echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}

transactions = append(transactions, t)
if err := processTransaction(&client, t); err != nil {
return client, echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}

return client, nil
}

func transactionRequestToTransaction(ID int, req TransactionRequest) (Transaction, error) {
var t Transaction

if len(req.Description) > 10 {
return t, echo.NewHTTPError(http.StatusUnprocessableEntity, "invalid desc")
}

if (req.Type != "c") && (req.Type != "d") {
return t, echo.NewHTTPError(http.StatusUnprocessableEntity, "invalid type")
}

t.ID = rand.Int()
t.CustomerID = ID
t.CreatedAt = time.Now()
t.Type = req.Type
t.Description = req.Description
t.Value = req.Value

return t, nil
}

func processTransaction(client *Client, trx Transaction) error {
if trx.Type == "c" {
client.Balance += trx.Value
return nil
}

if !validTransaction(*client, trx.Value) {
return echo.NewHTTPError(http.StatusUnprocessableEntity, nil)
if invalidTransaction(*client, trx.Value) {
return echo.NewHTTPError(http.StatusUnprocessableEntity, "can't process trx")
}

client.Balance -= trx.Value
return nil
}

func validTransaction(client Client, value int) bool {
return client.Balance-value < -client.MaxLimit
func invalidTransaction(client Client, value int) bool {
return client.Balance-value < (-1 * client.MaxLimit)
}
27 changes: 10 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,33 @@ func main() {
e.Logger.Fatal(e.Start(*port))
}

type TransactionResponse struct {
Saldo int `json:"saldo"`
Limite int `json:"limite"`
}

func handleTransaction(c echo.Context) error {
fmt.Println("Ola")
param := c.Param("id")

var attrs map[string]interface{}
err := (&echo.DefaultBinder{}).BindBody(c, &attrs)
fmt.Println("Ola2")
var req TransactionRequest
err := (&echo.DefaultBinder{}).BindBody(c, &req)
if err != nil {
return c.JSON(http.StatusInternalServerError, nil)
return c.JSON(http.StatusInternalServerError, "bind req")
}

customerID, err := strconv.Atoi(param)
fmt.Println("Ola3")
if err != nil {
return c.JSON(http.StatusUnprocessableEntity, nil)
return c.JSON(http.StatusUnprocessableEntity, "atoi")
}

var response TransactionResponse
cc, err := MakeTransaction(customerID, attrs)
cc, err := MakeTransaction(customerID, req)
if err != nil {
return c.JSON(http.StatusUnprocessableEntity, err.Error())
}
response.Saldo = cc.Balance
response.Limite = cc.MaxLimit

body, err := json.Marshal(response)
fmt.Println("Ola4")
if err != nil {
return c.JSON(http.StatusUnprocessableEntity, nil)
return c.JSON(http.StatusUnprocessableEntity, "marshal response")
}

fmt.Println("Ola5")
fmt.Println(string(body))
return c.JSON(http.StatusOK, string(body))
return c.JSON(http.StatusOK, body)
}

0 comments on commit 6cc15dd

Please sign in to comment.