summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2024-06-07 00:19:18 -0400
committerrealtradam <[email protected]>2024-06-07 00:19:18 -0400
commitde44507f2a75e36c4cea05f250f0f3d24e1dd123 (patch)
tree10bc5973c62203693199a59ccb6327cb77107559
parent22c1d8d71de2bd52513f684196fa66a53e7362aa (diff)
downloadgameHolster-de44507f2a75e36c4cea05f250f0f3d24e1dd123.tar.gz
gameHolster-de44507f2a75e36c4cea05f250f0f3d24e1dd123.zip
add tags
-rw-r--r--app/controllers/api/v1/games_controller.rb82
-rw-r--r--app/controllers/api/v1/tags_controller.rb15
-rw-r--r--app/controllers/platform_tags_controller.rb8
-rw-r--r--app/models/game.rb1
-rw-r--r--app/models/tag.rb3
-rw-r--r--config/routes.rb14
-rw-r--r--db/migrate/20240606184819_create_tags.rb10
-rw-r--r--db/migrate/20240606200659_create_tags_games_join_table.rb8
-rw-r--r--db/schema.rb19
-rw-r--r--db/seeds.rb20
-rw-r--r--test/controllers/api/v1/tags_controller_test.rb7
-rw-r--r--test/controllers/platform_tags_controller_test.rb7
-rw-r--r--test/controllers/tags_controller_test.rb18
-rw-r--r--test/fixtures/tags.yml9
-rw-r--r--test/models/tag_test.rb7
15 files changed, 182 insertions, 46 deletions
diff --git a/app/controllers/api/v1/games_controller.rb b/app/controllers/api/v1/games_controller.rb
index 8a44d89..f40c9de 100644
--- a/app/controllers/api/v1/games_controller.rb
+++ b/app/controllers/api/v1/games_controller.rb
@@ -1,46 +1,44 @@
require "zip"
-
class Api::V1::GamesController < ApplicationController
#skip_before_action :verify_authenticity_token
- before_action :allow_iframe, only: [:play]
+ before_action :allow_iframe, only: [:show_file]
def create
+ puts "----- PARAMS PLATFORM TAG ----------"
+ pp params["game"]["platform_tag"]
user = User.find_by(access_token_digest: cookies[:session])
- user = User.first # temporary for debug
+ #user = User.first # temporary for debug
if(!user)
- render json: {}, status: 401
+ render json: {session: cookies[:session]}, status: 401
else
pp params
- @game = user.games.new(game_params)#Game.new(game_params)
+ @game = user.games.new(game_params.except(:status, :platform_tag))
@game.titleSlug = game_params[:title].parameterize
-
- pp params
+ @game.status = game_params[:status].to_i
+ params["game"]["platform_tag"].each do |tag|
+ tag_obj = Tag.find_by(tag_type: "platform", name: tag)
+ if tag_obj
+ @game.tags << tag_obj
+ end
+ end
Zip::File.open(params[:game][:zip]) do |zipfile|
+
zipfile.each do |entry|
if entry.file?
path_name = entry.name.rpartition('/')
name_extension = path_name.last.rpartition('.')
- #puts " --- "
- #puts "Found file: #{entry.name.rpartition('/').last} at #{entry.name.rpartition('/').first}"
- #puts " --- "
+
Tempfile.open([name_extension.first, name_extension[1] + name_extension.last]) do |temp_file|
entry.extract(temp_file.path) { true }
- @game.game_files.attach(io: File.open(temp_file.path), filename: path_name.last);
+ @game.game_files.attach(io: File.open(temp_file.path), filename: path_name.last)
@game.game_files.last.blob.filepath = path_name.first.delete_suffix('/').delete_prefix('/')
- @game.game_files.last.blob.save
- @game.save
- #@game.game_files.last.path = path_name.first
- puts ""
- puts ""
- puts " GAME FILES"
- pp @game.game_files.size
- pp @game.game_files.last
- puts ""
- puts ""
- #activerecord_file.save
+
+ # saving the game wont have the blob saved so we need to do it manually
+ @game.game_files.last.blob.save
end
+
end
end
end
@@ -58,7 +56,7 @@ class Api::V1::GamesController < ApplicationController
def index
game = Game.all.order(created_at: :desc)
#render json: game
- render json: game.to_json(include: [:game_files, :card_img, :char_img, :title_img])
+ render json: game.to_json(include: [:game_files, :card_img, :char_img, :title_img, :tags])
end
# single game or list of user's games
@@ -69,17 +67,19 @@ class Api::V1::GamesController < ApplicationController
if params[:game].nil?
# get list of user games
games = Game.where(user_id: user.id).order(created_at: :desc)
- render json: games
+ render json: games.to_json(include: [:tags])
else
game = Game.find_by! user_id: user.id, titleSlug: params[:game]
- render json: game
+ render json: game.to_json(include: [:tags])
# get game
end
end
# :user/:game/*path/:file
- def play
+ def show_file
user = User.find_by user_name: params[:user]
+
+ # if no user given then just show all games
if(user.nil?)
game = Game.all.order(created_at: :desc)
render json: game
@@ -87,27 +87,28 @@ class Api::V1::GamesController < ApplicationController
end
game = Game.find_by user_id: user.id, titleSlug: params[:game]
+
+ # if no game given then just show all games from that user
if(game.nil?)
game = Game.all.order(created_at: :desc)
render json: game
return
end
+ # format and file is seperated in rails
filename = params[:file]
if !params[:format].nil?
filename = "#{filename}.#{params[:format]}"
end
- puts
- puts "HERE"
- puts params[:path]
- puts
+ # if we have no path, make it a blank string
+ # this lets us later match with files that are in the root
params[:path] ||= ""
- result = game.game_files.blobs.find_by(filename: filename, filepath: params[:path].delete_suffix('/').delete_prefix('/'))
- #result = game.game_files.blobs.find_by(filename: filename, filepath: params[:path])
- result ||= game.game_files.blobs.find_by(filename: filename)
+ result = game.game_files.blobs.find_by(filename: filename, filepath: params[:path].delete_suffix('/').delete_prefix('/')) # TODO check if we need to do the prefix/suffix deletion at all
+ # we shouldnt need this
+ #result ||= game.game_files.blobs.find_by(filename: filename)
if(result.nil?)
game = Game.all.order(created_at: :desc)
render json: { filename: filename, filepath: params[:path] }
@@ -116,7 +117,6 @@ class Api::V1::GamesController < ApplicationController
end
format = filename.rpartition('.').last
-
if format == "html"
render html: result.download.html_safe
elsif format == "js"
@@ -125,15 +125,13 @@ class Api::V1::GamesController < ApplicationController
# redirect_to url_for(result)
#end
elsif format == "gz"
+ response.headers['Content-Encoding'] = 'gzip'
second_ext = filename.rpartition('.').first.rpartition('.').last
if second_ext == 'js'
- response.headers['Content-Encoding'] = 'gzip'
send_data result.download.html_safe, filename: filename, disposition: "inline", type: "application/javascript"
elsif second_ext == 'wasm'
- response.headers['Content-Encoding'] = 'gzip'
send_data result.download.html_safe, filename: filename, disposition: "inline", type: "application/wasm"
elsif second_ext == 'data'
- response.headers['Content-Encoding'] = 'gzip'
send_data result.download.html_safe, filename: filename, disposition: "inline", type: "application/octet-stream"
else
send_data result.download.html_safe, filename: filename, disposition: "inline"
@@ -141,11 +139,9 @@ class Api::V1::GamesController < ApplicationController
else
send_data result.download.html_safe, filename: filename, disposition: "inline"
end
-
- #render html: game.game_files.first.download.html_safe #Game.first.game_file.download.html_safe
end
- #get 'imggames/:user/:game/:file', to: 'games#show_img'
+ #get 'imggames/:user/:game?type=___', to: 'games#show_img'
def show_img
user = User.find_by! user_name: params[:user]
game = Game.find_by! user_id: user.id, titleSlug: params[:game]
@@ -168,11 +164,15 @@ class Api::V1::GamesController < ApplicationController
params.require(:game).permit(
:title,
:description,
+ :github_link,
:img_rendering,
+ :status,
+ :order,
:card_img,
:char_img,
:title_img,
- :zip
+ :zip,
+ :platform_tag
#game_files: []
)
end
diff --git a/app/controllers/api/v1/tags_controller.rb b/app/controllers/api/v1/tags_controller.rb
new file mode 100644
index 0000000..4b31de8
--- /dev/null
+++ b/app/controllers/api/v1/tags_controller.rb
@@ -0,0 +1,15 @@
+class Api::V1::TagsController < ApplicationController
+
+ def index
+ if !params[:tag_type].nil?
+ tag = Tag.where(tag_type: params[:tag_type]).order(name: :asc)
+
+ render json: tag.to_json
+ else
+ tag = Tag.all.order(tag_type: :desc, name: :asc)
+ #render json: tag
+ render json: tag.to_json
+ end
+ end
+
+end
diff --git a/app/controllers/platform_tags_controller.rb b/app/controllers/platform_tags_controller.rb
new file mode 100644
index 0000000..538fe59
--- /dev/null
+++ b/app/controllers/platform_tags_controller.rb
@@ -0,0 +1,8 @@
+class PlatformTagsController < ApplicationController
+ def index
+ end
+ def create
+ tag = PlatformTag.create!(user_params)
+ render json: { status: "OK", message "Tag created" }, status: 201
+ end
+end
diff --git a/app/models/game.rb b/app/models/game.rb
index b4b98b1..1eb75ff 100644
--- a/app/models/game.rb
+++ b/app/models/game.rb
@@ -10,4 +10,5 @@ class Game < ApplicationRecord
has_one_attached :card_img
has_one_attached :char_img
has_one_attached :title_img
+ has_and_belongs_to_many :tags
end
diff --git a/app/models/tag.rb b/app/models/tag.rb
new file mode 100644
index 0000000..e92f654
--- /dev/null
+++ b/app/models/tag.rb
@@ -0,0 +1,3 @@
+class Tag < ApplicationRecord
+ has_and_belongs_to_many :users
+end
diff --git a/config/routes.rb b/config/routes.rb
index 6f86bb7..ba4c647 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,10 +1,13 @@
Rails.application.routes.draw do
+ get 'tags/index'
+ get 'tags/create'
+ get 'tags/destroy'
#
# isolated domain, do not allow auth here
#constraints host: 'localhost' do
# GAMES
- get 'game/:user/:game/*path/:file', to: 'api/v1/games#play', constraints: { file: /[^\/]+/ }
- get 'game/:user/:game/:file', to: 'api/v1/games#play'
+ get 'game/:user/:game/*path/:file', to: 'api/v1/games#show_file', constraints: { file: /[^\/]+/ }
+ get 'game/:user/:game/:file', to: 'api/v1/games#show_file', constraints: { file: /[^\/]+/ }
#end
namespace :api do
@@ -17,11 +20,14 @@ Rails.application.routes.draw do
# GAMES
post 'games', to: 'games#create'
get 'games', to: 'games#index'
- #get 'games/:user/', to: 'games#show'
- #get 'games/:user/:game', to: 'games#show'
+ get 'games/:user/', to: 'games#show'
+ get 'games/:user/:game', to: 'games#show'
get 'games_img/:user/:game', to: 'games#show_img'
#resources :games
+ # TAGS
+ get 'tags', to: 'tags#index'
+
# AUTH
get 'auth/callback', to: 'auth#callback'
get 'auth/data', to: 'auth#data'
diff --git a/db/migrate/20240606184819_create_tags.rb b/db/migrate/20240606184819_create_tags.rb
new file mode 100644
index 0000000..9b33939
--- /dev/null
+++ b/db/migrate/20240606184819_create_tags.rb
@@ -0,0 +1,10 @@
+class CreateTags < ActiveRecord::Migration[7.1]
+ def change
+ create_table :tags do |t|
+ t.string :tag_type, null: false
+ t.string :name, null: false
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20240606200659_create_tags_games_join_table.rb b/db/migrate/20240606200659_create_tags_games_join_table.rb
new file mode 100644
index 0000000..7a0dae4
--- /dev/null
+++ b/db/migrate/20240606200659_create_tags_games_join_table.rb
@@ -0,0 +1,8 @@
+class CreateTagsGamesJoinTable < ActiveRecord::Migration[7.1]
+ def change
+ create_join_table :tags, :games do |t|
+ t.index :tag_id
+ t.index :game_id
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 58a01aa..f4cabcc 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[7.1].define(version: 2024_05_27_231908) do
+ActiveRecord::Schema[7.1].define(version: 2024_06_06_200659) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -47,13 +47,30 @@ ActiveRecord::Schema[7.1].define(version: 2024_05_27_231908) do
t.string "title"
t.string "titleSlug"
t.string "description"
+ t.string "github_link"
t.string "img_rendering"
+ t.integer "status", default: 0
+ t.integer "order", default: 0
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "user_id", null: false
t.index ["user_id"], name: "index_games_on_user_id"
end
+ create_table "games_tags", id: false, force: :cascade do |t|
+ t.bigint "tag_id", null: false
+ t.bigint "game_id", null: false
+ t.index ["game_id"], name: "index_games_tags_on_game_id"
+ t.index ["tag_id"], name: "index_games_tags_on_tag_id"
+ end
+
+ create_table "tags", force: :cascade do |t|
+ t.string "tag_type", null: false
+ t.string "name", null: false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ end
+
create_table "users", force: :cascade do |t|
t.string "user_name"
t.string "identifier"
diff --git a/db/seeds.rb b/db/seeds.rb
index 4fbd6ed..83840d6 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -7,3 +7,23 @@
# ["Action", "Comedy", "Drama", "Horror"].each do |genre_name|
# MovieGenre.find_or_create_by!(name: genre_name)
# end
+
+platform_tags = [
+ "web",
+ "desktop",
+ "mobile",
+ "nintendo 64",
+ "other"
+]
+platform_tags.each do |tag|
+ Tag.find_or_create_by!(name: tag, tag_type: "platform")
+end
+
+game_tags = [
+ "action",
+ "tech demo",
+ "idk something",
+]
+game_tags.each do |tag|
+ Tag.find_or_create_by!(name: tag, tag_type: "game")
+end
diff --git a/test/controllers/api/v1/tags_controller_test.rb b/test/controllers/api/v1/tags_controller_test.rb
new file mode 100644
index 0000000..642edc6
--- /dev/null
+++ b/test/controllers/api/v1/tags_controller_test.rb
@@ -0,0 +1,7 @@
+require "test_helper"
+
+class Api::V1::TagsControllerTest < ActionDispatch::IntegrationTest
+ # test "the truth" do
+ # assert true
+ # end
+end
diff --git a/test/controllers/platform_tags_controller_test.rb b/test/controllers/platform_tags_controller_test.rb
new file mode 100644
index 0000000..eb6257d
--- /dev/null
+++ b/test/controllers/platform_tags_controller_test.rb
@@ -0,0 +1,7 @@
+require "test_helper"
+
+class PlatformTagsControllerTest < ActionDispatch::IntegrationTest
+ # test "the truth" do
+ # assert true
+ # end
+end
diff --git a/test/controllers/tags_controller_test.rb b/test/controllers/tags_controller_test.rb
new file mode 100644
index 0000000..bbf5c97
--- /dev/null
+++ b/test/controllers/tags_controller_test.rb
@@ -0,0 +1,18 @@
+require "test_helper"
+
+class TagsControllerTest < ActionDispatch::IntegrationTest
+ test "should get index" do
+ get tags_index_url
+ assert_response :success
+ end
+
+ test "should get create" do
+ get tags_create_url
+ assert_response :success
+ end
+
+ test "should get destroy" do
+ get tags_destroy_url
+ assert_response :success
+ end
+end
diff --git a/test/fixtures/tags.yml b/test/fixtures/tags.yml
new file mode 100644
index 0000000..5b866c5
--- /dev/null
+++ b/test/fixtures/tags.yml
@@ -0,0 +1,9 @@
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ type:
+ name: MyString
+
+two:
+ type:
+ name: MyString
diff --git a/test/models/tag_test.rb b/test/models/tag_test.rb
new file mode 100644
index 0000000..1846cdb
--- /dev/null
+++ b/test/models/tag_test.rb
@@ -0,0 +1,7 @@
+require "test_helper"
+
+class TagTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end