2025-11-30 17:12:50 +00:00
|
|
|
package account
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
|
2025-11-30 19:19:33 +00:00
|
|
|
"github.com/get-drexa/drexa/internal/httperr"
|
2025-12-04 00:48:43 +00:00
|
|
|
"github.com/get-drexa/drexa/internal/reqctx"
|
2025-11-30 17:12:50 +00:00
|
|
|
"github.com/get-drexa/drexa/internal/user"
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type HTTPHandler struct {
|
2026-01-01 18:29:52 +00:00
|
|
|
accountService *Service
|
|
|
|
|
db *bun.DB
|
|
|
|
|
authMiddleware fiber.Handler
|
2025-11-30 17:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-01 18:29:52 +00:00
|
|
|
func NewHTTPHandler(accountService *Service, db *bun.DB, authMiddleware fiber.Handler) *HTTPHandler {
|
|
|
|
|
return &HTTPHandler{
|
|
|
|
|
accountService: accountService,
|
|
|
|
|
db: db,
|
|
|
|
|
authMiddleware: authMiddleware,
|
|
|
|
|
}
|
2025-11-30 17:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-01 18:29:52 +00:00
|
|
|
func (h *HTTPHandler) RegisterRoutes(api fiber.Router) {
|
2025-12-15 00:13:10 +00:00
|
|
|
api.Get("/accounts", h.authMiddleware, h.listAccounts)
|
2026-01-01 18:29:52 +00:00
|
|
|
api.Get("/accounts/:accountID", h.authMiddleware, h.getAccount)
|
2025-11-30 17:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-15 00:13:10 +00:00
|
|
|
func (h *HTTPHandler) listAccounts(c *fiber.Ctx) error {
|
|
|
|
|
u := reqctx.AuthenticatedUser(c).(*user.User)
|
|
|
|
|
accounts, err := h.accountService.ListAccounts(c.Context(), h.db, u.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return httperr.Internal(err)
|
|
|
|
|
}
|
|
|
|
|
return c.JSON(accounts)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-30 17:12:50 +00:00
|
|
|
func (h *HTTPHandler) getAccount(c *fiber.Ctx) error {
|
2026-01-01 18:29:52 +00:00
|
|
|
u := reqctx.AuthenticatedUser(c).(*user.User)
|
|
|
|
|
accountID, err := uuid.Parse(c.Params("accountID"))
|
2025-11-30 17:12:50 +00:00
|
|
|
if err != nil {
|
2026-01-01 18:29:52 +00:00
|
|
|
return c.SendStatus(fiber.StatusNotFound)
|
2025-11-30 17:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-01 18:29:52 +00:00
|
|
|
acc, err := h.accountService.AccountByID(c.Context(), h.db, u.ID, accountID)
|
2025-11-30 17:12:50 +00:00
|
|
|
if err != nil {
|
2026-01-01 18:29:52 +00:00
|
|
|
if errors.Is(err, ErrAccountNotFound) {
|
|
|
|
|
return c.SendStatus(fiber.StatusNotFound)
|
2025-11-30 17:12:50 +00:00
|
|
|
}
|
2025-11-30 19:19:33 +00:00
|
|
|
return httperr.Internal(err)
|
2025-11-30 17:12:50 +00:00
|
|
|
}
|
2026-01-01 18:29:52 +00:00
|
|
|
return c.JSON(acc)
|
2025-11-30 17:12:50 +00:00
|
|
|
}
|