Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Implement ratings

  • Loading branch information...
brtjkzl committed Aug 10, 2018
1 parent 1694f6e commit 1841b3396fef7b5b2920562f61fa4bb25efc0d5b
@@ -9,10 +9,13 @@ inherit_mode:

AllCops:
Exclude:
- 'bin/**/*'
- "bin/**/*"

Style/FrozenStringLiteralComment:
Enabled: false

Bundler/OrderedGems:
Enabled: false

Style/LambdaCall:
Enabled: false
@@ -1,5 +1,5 @@
export const API_GAMES = "/api/games";
export const API_SCORES = "/api/scores";
export const API_SCORE = "/api/scores/:id";
export const API_RATINGS = "/api/ratings";
export const API_RATING = "/api/ratings/:id";
export const API_USER_GAMES = "/api/user/games";
export const API_USER_GAME = "/api/user/games/:id";
@@ -1,4 +1,4 @@
import { getUserGames, updateUserGame, getScore, getScores } from "./api";
import { getUserGames, updateUserGame, getRating, getRatings } from "./api";
import {
resolveGames,
resolveGameUpdate,
@@ -14,7 +14,7 @@ export const fetchGames = () => (dispatch, getState) => {

dispatch(resolveGames(games));

getScores(userGamesIds).then(response =>
getRatings(userGamesIds).then(response =>
dispatch(resolveGamesBulkUpdate(response.data))
);
});
@@ -43,6 +43,6 @@ export const rateGame = (id, rating) => (dispatch, getState) => {
updateUserGame(id, token, { game: { rating } }).then(() => {
dispatch(resolveGameUpdate({ id, rating }));

getScore(id).then(response => dispatch(resolveGameUpdate(response.data)));
getRating(id).then(response => dispatch(resolveGameUpdate(response.data)));
});
};
@@ -1,16 +1,16 @@
import { get, put } from "axios";
import {
API_SCORES,
API_SCORE,
API_RATING,
API_RATINGS,
API_USER_GAMES,
API_USER_GAME
} from "../../routes/api";
import { setAuthToken, filtersSerializer } from "./utils";

export const getScore = id => get(API_SCORE.replace(":id", id));
export const getRating = id => get(API_RATING.replace(":id", id));

export const getScores = params =>
get(API_SCORES, { params, paramsSerializer: filtersSerializer });
export const getRatings = params =>
get(API_RATINGS, { params, paramsSerializer: filtersSerializer });

export const getUserGames = (token, params) =>
get(API_USER_GAMES, {
@@ -4,7 +4,7 @@ import {
resolveResults,
rejectResults
} from "./actionCreators";
import { getScores, getUserGames } from "../collection/api";
import { getRatings, getUserGames } from "../collection/api";
import { resolveGamesBulkUpdate } from "../collection/actionCreators";

export const searchQuery = query => (dispatch, getState) => {
@@ -19,7 +19,7 @@ export const searchQuery = query => (dispatch, getState) => {

dispatch(resolveResults(results));

getScores(gamesIds).then(response =>
getRatings(gamesIds).then(response =>
dispatch(resolveGamesBulkUpdate(response.data))
);

@@ -0,0 +1,28 @@
class Api::RatingsController < ApiController
def index
conditions = apply_filters({ game_id: :id })
averages = UserGame.where(conditions).group(:game_id).average(:rating)
counts = UserGame.where(conditions).group(:game_id).count(:rating)

@ratings = counts.map do |game_id, count|
Game.rating({
id: game_id,
count: count,
average:averages[game_id]
})
end
end

def show
game_id = params[:id]
user_game = UserGame.where(game_id: game_id)
average = user_game.average(:rating)
count = user_game.count(:rating)

@rating = Game.rating({
id: game_id,
average: average,
count: count
})
end
end

This file was deleted.

@@ -1,7 +1,7 @@
class Api::User::GamesController < Api::UserController
def index
query = apply_optional_filters({ user: current_user }, id: :game_id)
@user_games = UserGame.where(query).includes(:platforms, game: [:platforms])
conditions = apply_filters({ game_id: :id }, { user: current_user })
@user_games = UserGame.where(conditions).includes(:platforms, game: [:platforms])
end

def update
@@ -12,14 +12,14 @@ def authorize
end
end

def apply_optional_filters(query, filters)
filters.each do |filter, key|
def apply_filters(filters, conditions = {})
filters.each do |column, filter|
if params[:filter] && values = params[:filter][filter]
query.merge!(Hash[key, values.split(',')])
conditions.merge!(Hash[column, values.split(",")])
end
end

query
conditions
end

private
@@ -11,4 +11,12 @@ def self.save_igdb_results(results)
saved_game.id
end
end

def self.rating(id:, average:, count:)
{
id: id.to_i,
score: average.to_f.round(1),
votes: count
}
end
end
@@ -3,4 +3,4 @@ json.array! @games do |game|
json.platforms game.platforms do |platform|
json.(platform, :id, :slug)
end
end
end
@@ -0,0 +1,3 @@
json.array! @ratings do |rating|
json.(rating, :id, :score, :votes)
end
@@ -0,0 +1 @@
json.(@rating, :id, :score, :votes)

This file was deleted.

This file was deleted.

@@ -8,4 +8,4 @@ json.array! @user_games do |user_game|
json.userPlatforms user_game.platforms do |platform|
json.(platform, :id, :slug)
end
end
end
@@ -5,7 +5,7 @@
end

resources :games, only: [:index]
resources :scores, only: [:index, :show]
resources :ratings, only: [:index, :show]
end

get "/auth/:provider/callback", to: "auth#create"

0 comments on commit 1841b33

Please sign in to comment.
You can’t perform that action at this time.