home_handler.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package handlers
  2. import (
  3. "net/http"
  4. "strconv"
  5. "cmr-backend/internal/httpx"
  6. "cmr-backend/internal/service"
  7. )
  8. type HomeHandler struct {
  9. homeService *service.HomeService
  10. }
  11. func NewHomeHandler(homeService *service.HomeService) *HomeHandler {
  12. return &HomeHandler{homeService: homeService}
  13. }
  14. func (h *HomeHandler) GetHome(w http.ResponseWriter, r *http.Request) {
  15. result, err := h.homeService.GetHome(r.Context(), buildListCardsInput(r))
  16. if err != nil {
  17. httpx.WriteError(w, err)
  18. return
  19. }
  20. httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
  21. }
  22. func (h *HomeHandler) GetCards(w http.ResponseWriter, r *http.Request) {
  23. result, err := h.homeService.ListCards(r.Context(), buildListCardsInput(r))
  24. if err != nil {
  25. httpx.WriteError(w, err)
  26. return
  27. }
  28. httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
  29. }
  30. func buildListCardsInput(r *http.Request) service.ListCardsInput {
  31. limit := 20
  32. if raw := r.URL.Query().Get("limit"); raw != "" {
  33. if parsed, err := strconv.Atoi(raw); err == nil {
  34. limit = parsed
  35. }
  36. }
  37. return service.ListCardsInput{
  38. ChannelCode: r.URL.Query().Get("channelCode"),
  39. ChannelType: r.URL.Query().Get("channelType"),
  40. PlatformAppID: r.URL.Query().Get("platformAppId"),
  41. TenantCode: r.URL.Query().Get("tenantCode"),
  42. Slot: r.URL.Query().Get("slot"),
  43. Limit: limit,
  44. }
  45. }