PaulPaulAga

Untitled

Apr 10th, 2021
648
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. func (mr *dbMovieRepository) updateRating(movieId uint) error {
  3.     psql := sq.StatementBuilder.PlaceholderFormat(sq.Dollar)
  4.     likeQuery, likeQueryArgs, err := psql.Select("count(*)").
  5.         From("movie_votes").
  6.         Where(sq.Eq{"movie_id": movieId}).Where("value > 0").ToSql()
  7.     if err != nil {
  8.         log.Log.Warn(fmt.Sprintf("Can't build like count request: %v", err))
  9.         return err
  10.     }
  11.  
  12.     dislikeQuery, dislikeQueryArgs, err := psql.Select("count(*)").
  13.         From("movie_votes").
  14.         Where(sq.Eq{"movie_id": movieId}).Where("value < 0").ToSql()
  15.     if err != nil {
  16.         log.Log.Warn(fmt.Sprintf("Can't build dislike count request: %v", err))
  17.         return err
  18.     }
  19.  
  20.     viewsQuery, viewQueryArgs, err := psql.Select("count(*)").
  21.         From("movie_votes").
  22.         Where(sq.Eq{"movie_id": movieId}).Where("value > 0").ToSql()
  23.     if err != nil {
  24.         log.Log.Warn(fmt.Sprintf("Can't build views count request: %v", err))
  25.         return err
  26.     }
  27.  
  28.     likes := 0
  29.     data, err := mr.db.Query(likeQuery, likeQueryArgs...)
  30.     if err != nil {
  31.         log.Log.Warn(fmt.Sprintf("Can't get like count %v", err))
  32.         return err
  33.     }
  34.     if len(data) > 0 {
  35.         likes = cast.ToInt(data[0][0])
  36.     }
  37.  
  38.     dislikes := 0
  39.     data, err = mr.db.Query(dislikeQuery, dislikeQueryArgs...)
  40.     if err != nil {
  41.         log.Log.Warn(fmt.Sprintf("Can't get dislike countpath: %v", err))
  42.         return err
  43.     }
  44.     if len(data) > 0 {
  45.         dislikes = cast.ToInt(data[0][0])
  46.     }
  47.  
  48.     views := 0
  49.     data, err = mr.db.Query(viewsQuery, viewQueryArgs...)
  50.     if err != nil {
  51.         log.Log.Warn(fmt.Sprintf("Can't get view count path: %v", err))
  52.         return err
  53.     }
  54.     if len(data) > 0 {
  55.         views = cast.ToInt(data[0][0])
  56.     }
  57.  
  58.     newRating := countRating(likes, dislikes, views)
  59.     ratingUpdateQuery, ratingUpdateQueryArgs, err := psql.Update("movies").
  60.         Set("rating", newRating).
  61.         Where(sq.Eq{"id": newRating}).ToSql()
  62.     if err != nil {
  63.         log.Log.Warn(fmt.Sprintf("Can't build rating update request: %v", err))
  64.         return err
  65.     }
  66.     data, err = mr.db.Query(ratingUpdateQuery, ratingUpdateQueryArgs...)
  67.     if err != nil {
  68.         log.Log.Warn(fmt.Sprintf("Can't update rating: %v", err))
  69.         return err
  70.     }
  71.    
  72.     return nil
  73. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×