Skip to content

Commit

Permalink
HELP: we aresuffering with handleTransaction base64 return
Browse files Browse the repository at this point in the history
  • Loading branch information
MintzyG committed Feb 22, 2024
1 parent 325f0bc commit 8f84080
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function transact {
echo $tipo
echo $url

curl -s -X POST -H 'content-type: application/json' -d $body $url | jq
curl -X POST -H 'content-type: application/json' -d $body $url | jq
}

function statement {
Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
mkShell {
name = "rinha-go";
packages = with pkgs;
[go_1_22 nginx postgresql k6]
[go_1_22 nginx postgresql k6 jq]
++ lib.optional stdenv.isLinux [inotify-tools]
++ lib.optional stdenv.isDarwin [
darwin.apple_sdk.frameworks.CoreServices
Expand Down
4 changes: 2 additions & 2 deletions internal/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ func processTransaction(client *Client, trx Transaction) error {
return nil
}

func validTransaction(cilent Client, value int) bool {
return true
func validTransaction(client Client, value int) bool {
return client.Balance-value < -client.MaxLimit
}
29 changes: 25 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"net/http"
"strconv"

Expand All @@ -17,30 +19,49 @@ func main() {
e := echo.New()

// middlewares
e.Use(middleware.RequestID())
// e.Use(middleware.RequestID())
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "method=${method}, uri=${uri}, status=${status}\n",
}))

e.GET("/clientes/:id/transacoes", handleTransaction)
e.POST("/clientes/:id/transacoes", handleTransaction)
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")
if err != nil {
return c.JSON(http.StatusInternalServerError, nil)
}

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

t, err := MakeTransaction(customerID, attrs)
var response TransactionResponse
cc, err := MakeTransaction(customerID, attrs)
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 nil
fmt.Println("Ola5")
fmt.Println(string(body))
return c.JSON(http.StatusOK, string(body))
}

0 comments on commit 8f84080

Please sign in to comment.