suggestions.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package handlers
  2. import (
  3. "net/http"
  4. "git.linuxforward.com/byom/byom-trends/common"
  5. "git.linuxforward.com/byom/byom-trends/logger"
  6. "git.linuxforward.com/byom/byom-trends/services/suggestions"
  7. "git.linuxforward.com/byom/byom-trends/store"
  8. "github.com/gin-gonic/gin"
  9. "github.com/google/uuid"
  10. )
  11. type SuggestionsHandler struct {
  12. store *store.DataStore
  13. analyzer *suggestions.Analyzer
  14. logger *logger.Entry
  15. }
  16. func NewSuggestionsHandler(store *store.DataStore, analyzer *suggestions.Analyzer) *SuggestionsHandler {
  17. return &SuggestionsHandler{
  18. store: store,
  19. analyzer: analyzer,
  20. logger: logger.NewLogger("suggestions-handler"),
  21. }
  22. }
  23. func (h *SuggestionsHandler) GetSuggestions(c *gin.Context) {
  24. var req common.GetSuggestionsRequest
  25. if err := c.ShouldBindJSON(&req); err != nil {
  26. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request format"})
  27. return
  28. }
  29. var trends []common.ContentTrend
  30. for _, social := range req.Socials {
  31. socialTrends, err := h.store.GetTrendsByUsername(c, social.Username)
  32. if err != nil {
  33. h.logger.WithError(err).Error("failed to get trends")
  34. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get trends"})
  35. return
  36. }
  37. trends = append(trends, socialTrends...)
  38. }
  39. suggestions, err := h.analyzer.AnalyzeTrendsForProducts(trends)
  40. if err != nil {
  41. h.logger.WithError(err).Error("failed to analyze trends")
  42. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to analyze trends"})
  43. return
  44. }
  45. if err := h.store.SaveSuggestions(c, req.UserID, suggestions); err != nil {
  46. h.logger.WithError(err).Error("failed to save suggestions")
  47. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save suggestions"})
  48. return
  49. }
  50. c.JSON(http.StatusOK, suggestions)
  51. }
  52. func (h *SuggestionsHandler) GetUserSuggestions(c *gin.Context) {
  53. userID := c.Param("user_id")
  54. if userID == "" {
  55. c.JSON(http.StatusBadRequest, gin.H{"error": "User ID is required"})
  56. return
  57. }
  58. suggestions, err := h.store.GetSuggestionsByUserID(c, userID)
  59. if err != nil {
  60. h.logger.WithError(err).Error("failed to get suggestions")
  61. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get suggestions"})
  62. return
  63. }
  64. c.JSON(http.StatusOK, suggestions)
  65. }
  66. func (h *SuggestionsHandler) GetSuggestion(c *gin.Context) {
  67. id, err := uuid.Parse(c.Param("id"))
  68. if err != nil {
  69. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid suggestion ID"})
  70. return
  71. }
  72. suggestion, err := h.store.GetSuggestionByID(c, id)
  73. if err != nil {
  74. h.logger.WithError(err).Error("failed to get suggestion")
  75. c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get suggestion"})
  76. return
  77. }
  78. if suggestion == nil {
  79. c.JSON(http.StatusNotFound, gin.H{"error": "Suggestion not found"})
  80. return
  81. }
  82. c.JSON(http.StatusOK, suggestion)
  83. }