SHARE
TWEET

Untitled

a guest Sep 16th, 2019 131 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // GetRecommendations gets all recommendations.
  2. func (s *Setup) GetRecommendations(w http.ResponseWriter, r *http.Request) {
  3.     params := r.URL.Query()
  4.  
  5.     //Redis check start
  6.     var rrKey string
  7.     if params["page"] != nil {
  8.         rrKey = fmt.Sprintf("recommendation?page=%s", params["page"][0])
  9.     } else {
  10.         rrKey = "recommendation"
  11.     }
  12.  
  13.     val, _ := s.rc.Get(rrKey).Result()
  14.  
  15.     if val != "" {
  16.         rr := &model.RecommendationPagination{}
  17.         if err := helper.UnmarshalBinary([]byte(val), rr); err != nil {
  18.             helper.DecodeError(w, r, s.l, errUnmarshal, http.StatusInternalServerError)
  19.             return
  20.         }
  21.         json.NewEncoder(w).Encode(&rr)
  22.         return
  23.     }
  24.     // Redis check end
  25.  
  26.     // Start pagination
  27.     total, err := s.h.GetRecommendationTotalRows() // total results
  28.  
  29.     if err != nil {
  30.         helper.DecodeError(w, r, s.l, errFetch, http.StatusInternalServerError)
  31.         return
  32.     }
  33.  
  34.     newPage := 1
  35.     if params["page"] != nil && params["page"][0] != "" {
  36.         newPage, err = strconv.Atoi(params["page"][0])
  37.         if err != nil {
  38.             helper.DecodeError(w, r, s.l, errParseInt, http.StatusInternalServerError)
  39.             return
  40.         }
  41.     }
  42.  
  43.     chapter := &tome.Chapter{
  44.         NewPage:      newPage,
  45.         TotalResults: total,
  46.     }
  47.  
  48.     if err := chapter.Paginate(); err != nil {
  49.         helper.DecodeError(w, r, s.l, err.Error(), http.StatusInternalServerError)
  50.         return
  51.     }
  52.     // End pagination
  53.  
  54.     rec, err := s.h.GetRecommendations(chapter.Offset, chapter.Limit)
  55.     if err != nil {
  56.         helper.DecodeError(w, r, s.l, errFetch, http.StatusInternalServerError)
  57.         return
  58.     }
  59.  
  60.     result := []*model.RecommendationResponse{}
  61.  
  62.     for _, rr := range rec.Data {
  63.         recG, err := s.h.GetRecommendationGenres(rr.ID)
  64.         if err != nil {
  65.             helper.DecodeError(w, r, s.l, errFetch, http.StatusInternalServerError)
  66.             return
  67.         }
  68.         recK, err := s.h.GetRecommendationKeywords(rr.ID)
  69.         if err != nil {
  70.             helper.DecodeError(w, r, s.l, errFetch, http.StatusInternalServerError)
  71.             return
  72.         }
  73.         recFinal := &model.RecommendationResponse{
  74.             Recommendation: rr,
  75.             Genres:         recG,
  76.             Keywords:       recK,
  77.         }
  78.         result = append(result, recFinal)
  79.     }
  80.  
  81.     resultFinal := &model.RecommendationPagination{
  82.         Data:    result,
  83.         Chapter: chapter,
  84.     }
  85.  
  86.     // Redis set check start
  87.     rr, err := helper.MarshalBinary(resultFinal)
  88.     if err != nil {
  89.         helper.DecodeError(w, r, s.l, errMarhsal, http.StatusInternalServerError)
  90.         return
  91.     }
  92.     if err := s.rc.Set(rrKey, rr, redisTimeout).Err(); err != nil {
  93.         helper.DecodeError(w, r, s.l, errKeySet, http.StatusInternalServerError)
  94.         return
  95.     }
  96.     // Redis set check end
  97.  
  98.     w.WriteHeader(http.StatusOK)
  99.     json.NewEncoder(w).Encode(resultFinal)
  100. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top