package http import ( "context" "encoding/json" "errors" "github.com/matchsystems/werr" "log" "mic-wallet/common" "mic-wallet/server/core" logicErr "mic-wallet/server/logic/err" "net/http" ) func NewTransaction(ctx context.Context, w http.ResponseWriter, r *http.Request, d ApiDependencies) { defer func() { if err := r.Body.Close(); err != nil { log.Printf("error closing request body: %s", err.Error()) } }() req := &common.NewTxOpts{} if err := json.NewDecoder(r.Body).Decode(req); err != nil { http.Error(w, "invalid request body", http.StatusBadRequest) return } if err := d.WriteTx(req); err != nil { if errors.Is(werr.UnwrapAll(err), core.ErrTxDuplicate) { http.Error(w, "transaction already exists", http.StatusConflict) return } w.WriteHeader(http.StatusInternalServerError) } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) } func GetTransaction(w http.ResponseWriter, r *http.Request) *logicErr.Err { return nil } func GetTransactions(w http.ResponseWriter, r *http.Request) *logicErr.Err { txHash := r.URL.Query().Get("tx_sh") if txHash != "" { return nil } pubKey := r.URL.Query().Get("pub_key") if pubKey != "" { return nil } return nil } func MountTransactionRoutes(ctx context.Context, mux *http.ServeMux, thisRoute string) { thisRoute += "/transaction" mux.HandleFunc(thisRoute, func(w http.ResponseWriter, r *http.Request) { var err *logicErr.Err switch r.Method { case http.MethodGet: err = GetTransactions(w, r) case http.MethodPost: err = NewTransaction(w, r) } logicErr.HandleHttp(ctx, w, r, err) }) }