|
| 1 | +// transaction_handler.go provides HTTP endpoints for shop transactions. |
| 2 | +// Thin handlers: bind request, call service, return JSON. No business logic. |
| 3 | +package armory |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "net/http" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/labstack/echo/v4" |
| 11 | + |
| 12 | + "github.com/keyxmakerx/chronicle/internal/apperror" |
| 13 | + "github.com/keyxmakerx/chronicle/internal/plugins/auth" |
| 14 | + "github.com/keyxmakerx/chronicle/internal/plugins/campaigns" |
| 15 | +) |
| 16 | + |
| 17 | +// TransactionHandler serves transaction REST endpoints. |
| 18 | +type TransactionHandler struct { |
| 19 | + svc TransactionService |
| 20 | +} |
| 21 | + |
| 22 | +// NewTransactionHandler creates a new transaction handler. |
| 23 | +func NewTransactionHandler(svc TransactionService) *TransactionHandler { |
| 24 | + return &TransactionHandler{svc: svc} |
| 25 | +} |
| 26 | + |
| 27 | +// Purchase handles POST /campaigns/:id/armory/purchase. |
| 28 | +// Validates stock, creates transaction, decrements shop inventory. |
| 29 | +func (h *TransactionHandler) Purchase(c echo.Context) error { |
| 30 | + cc := campaigns.GetCampaignContext(c) |
| 31 | + if cc == nil { |
| 32 | + return apperror.NewMissingContext() |
| 33 | + } |
| 34 | + |
| 35 | + var input CreateTransactionInput |
| 36 | + if err := json.NewDecoder(c.Request().Body).Decode(&input); err != nil { |
| 37 | + return apperror.NewBadRequest("invalid JSON body") |
| 38 | + } |
| 39 | + |
| 40 | + userID := auth.GetUserID(c) |
| 41 | + result, err := h.svc.Purchase(c.Request().Context(), cc.Campaign.ID, userID, input) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + return c.JSON(http.StatusCreated, result) |
| 47 | +} |
| 48 | + |
| 49 | +// CreateTransaction handles POST /campaigns/:id/armory/transactions. |
| 50 | +// Records a manual transaction (gift, transfer, restock). |
| 51 | +func (h *TransactionHandler) CreateTransaction(c echo.Context) error { |
| 52 | + cc := campaigns.GetCampaignContext(c) |
| 53 | + if cc == nil { |
| 54 | + return apperror.NewMissingContext() |
| 55 | + } |
| 56 | + |
| 57 | + var input CreateTransactionInput |
| 58 | + if err := json.NewDecoder(c.Request().Body).Decode(&input); err != nil { |
| 59 | + return apperror.NewBadRequest("invalid JSON body") |
| 60 | + } |
| 61 | + |
| 62 | + userID := auth.GetUserID(c) |
| 63 | + tx, err := h.svc.CreateTransaction(c.Request().Context(), cc.Campaign.ID, userID, input) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + return c.JSON(http.StatusCreated, tx) |
| 69 | +} |
| 70 | + |
| 71 | +// ListTransactions handles GET /campaigns/:id/armory/transactions. |
| 72 | +// Returns paginated transactions with optional filters. |
| 73 | +func (h *TransactionHandler) ListTransactions(c echo.Context) error { |
| 74 | + cc := campaigns.GetCampaignContext(c) |
| 75 | + if cc == nil { |
| 76 | + return apperror.NewMissingContext() |
| 77 | + } |
| 78 | + |
| 79 | + opts := DefaultTransactionListOptions() |
| 80 | + if p := c.QueryParam("page"); p != "" { |
| 81 | + if n, err := strconv.Atoi(p); err == nil && n > 0 { |
| 82 | + opts.Page = n |
| 83 | + } |
| 84 | + } |
| 85 | + if pp := c.QueryParam("per_page"); pp != "" { |
| 86 | + if n, err := strconv.Atoi(pp); err == nil && n > 0 && n <= 100 { |
| 87 | + opts.PerPage = n |
| 88 | + } |
| 89 | + } |
| 90 | + if sid := c.QueryParam("shop"); sid != "" { |
| 91 | + opts.ShopEntityID = sid |
| 92 | + } |
| 93 | + if bid := c.QueryParam("buyer"); bid != "" { |
| 94 | + opts.BuyerEntityID = bid |
| 95 | + } |
| 96 | + if iid := c.QueryParam("item"); iid != "" { |
| 97 | + opts.ItemEntityID = iid |
| 98 | + } |
| 99 | + if tt := c.QueryParam("type"); tt != "" { |
| 100 | + opts.TransactionType = tt |
| 101 | + } |
| 102 | + |
| 103 | + txs, total, err := h.svc.ListTransactions(c.Request().Context(), cc.Campaign.ID, opts) |
| 104 | + if err != nil { |
| 105 | + return apperror.NewInternal(err) |
| 106 | + } |
| 107 | + |
| 108 | + // Return empty array, not null. |
| 109 | + if txs == nil { |
| 110 | + txs = []Transaction{} |
| 111 | + } |
| 112 | + |
| 113 | + return c.JSON(http.StatusOK, map[string]any{ |
| 114 | + "data": txs, |
| 115 | + "total": total, |
| 116 | + "page": opts.Page, |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +// ListShopTransactions handles GET /campaigns/:id/armory/shops/:eid/transactions. |
| 121 | +// Returns transactions for a specific shop entity. |
| 122 | +func (h *TransactionHandler) ListShopTransactions(c echo.Context) error { |
| 123 | + cc := campaigns.GetCampaignContext(c) |
| 124 | + if cc == nil { |
| 125 | + return apperror.NewMissingContext() |
| 126 | + } |
| 127 | + |
| 128 | + entityID := c.Param("eid") |
| 129 | + if entityID == "" { |
| 130 | + return apperror.NewBadRequest("entity ID is required") |
| 131 | + } |
| 132 | + |
| 133 | + opts := DefaultTransactionListOptions() |
| 134 | + if p := c.QueryParam("page"); p != "" { |
| 135 | + if n, err := strconv.Atoi(p); err == nil && n > 0 { |
| 136 | + opts.Page = n |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + txs, total, err := h.svc.ListShopTransactions(c.Request().Context(), entityID, opts) |
| 141 | + if err != nil { |
| 142 | + return apperror.NewInternal(err) |
| 143 | + } |
| 144 | + |
| 145 | + if txs == nil { |
| 146 | + txs = []Transaction{} |
| 147 | + } |
| 148 | + |
| 149 | + return c.JSON(http.StatusOK, map[string]any{ |
| 150 | + "data": txs, |
| 151 | + "total": total, |
| 152 | + "page": opts.Page, |
| 153 | + }) |
| 154 | +} |
0 commit comments