Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: schemas,controllers for the practice section #4967

Open
wants to merge 53 commits into
base: circuitverse-practice-section
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
c0ef621
feat: created schemas,controllers for the question bank management sy…
jaydip1235 Jun 2, 2024
3c25201
fix: removed skip_csrf_protection
jaydip1235 Jun 2, 2024
bbba47a
fix: added frozen literal line comment
jaydip1235 Jun 2, 2024
e648c0d
fix: fixed space issue
jaydip1235 Jun 2, 2024
f5b4af6
fix: removed training spaces
jaydip1235 Jun 2, 2024
06153e5
fix: added frozen literal line comment
jaydip1235 Jun 2, 2024
a7088cf
fix: included %i
jaydip1235 Jun 2, 2024
5596f8b
fix: fixed spaces
jaydip1235 Jun 4, 2024
2c7d08b
fix: fixed catergories controller spaces
jaydip1235 Jun 4, 2024
f5411e0
fix: fixed catergories controller spaces
jaydip1235 Jun 4, 2024
b3163be
fix: fixed catergories controller spaces
jaydip1235 Jun 4, 2024
d1164ad
fix: fixed catergories controller spaces
jaydip1235 Jun 4, 2024
894cb72
fix: fixed catergories controller spaces
jaydip1235 Jun 4, 2024
269bd4f
fix: fixed catergories controller spaces
jaydip1235 Jun 4, 2024
fabe0d4
fix: fixed catergories controller spaces
jaydip1235 Jun 4, 2024
4b01c19
fix: fixed catergories controller spaces
jaydip1235 Jun 4, 2024
4460689
fix: fixed catergories controller spaces
jaydip1235 Jun 4, 2024
6d3f7b1
fix: fixed catergories controller spaces
jaydip1235 Jun 5, 2024
cb5a4ff
fix: fixed spaces
jaydip1235 Jun 5, 2024
f294787
fix: fixed spaces
jaydip1235 Jun 5, 2024
b537403
fix: fixed spaces
jaydip1235 Jun 5, 2024
f6a16ca
fix: fixed spaces
jaydip1235 Jun 5, 2024
decdfcb
fix: fixed spaces
jaydip1235 Jun 5, 2024
75bdba5
fix: fixed spaces
jaydip1235 Jun 5, 2024
b551389
fix: fixed spaces
jaydip1235 Jun 5, 2024
b3b26e0
fix: fixed insecure mass assignment
jaydip1235 Jun 5, 2024
7bc691d
fix: added test for categories
jaydip1235 Jun 7, 2024
4aebca3
fix: removed apis
jaydip1235 Jun 7, 2024
6d34c74
fix: removed apis
jaydip1235 Jun 7, 2024
e597f60
fix: removed apis
jaydip1235 Jun 7, 2024
73fdab7
fix: added keys in user.json
jaydip1235 Jun 7, 2024
3274b23
fix: moved to controllers
jaydip1235 Jun 7, 2024
f5165ed
fix: moved to controllers
jaydip1235 Jun 7, 2024
652823b
fix: removed unnecessary files
jaydip1235 Jun 8, 2024
f499b42
fix: removed unnecessary files
jaydip1235 Jun 8, 2024
10a6ac6
fix: test fixes
jaydip1235 Jun 8, 2024
2c5a3b2
fix: test fixes
jaydip1235 Jun 8, 2024
703ae68
fix: test fixes
jaydip1235 Jun 8, 2024
ae3f0e1
fix: test fixes
jaydip1235 Jun 8, 2024
e0b6cec
fix: test fixes
jaydip1235 Jun 9, 2024
c7daad0
fix: test fixes
jaydip1235 Jun 9, 2024
38903cd
feat: all the controllers added (test not added)
jaydip1235 Jun 16, 2024
9daaa48
feat: all the controllers added (test not added)
jaydip1235 Jun 16, 2024
bd85dd3
added myself as a mentor (#4965)
priyanshagra Jun 16, 2024
508baff
feat: all the controllers added (test not added)
jaydip1235 Jun 16, 2024
2cb6490
feat: all the controllers added (test not added)
jaydip1235 Jun 16, 2024
36a4fd5
fix: removed controller logic
jaydip1235 Jun 20, 2024
9ff671f
fix: removed controller logic
jaydip1235 Jun 20, 2024
897527a
fix: removed controller logic
jaydip1235 Jun 20, 2024
511b237
fix: removed controller logic
jaydip1235 Jun 20, 2024
b005cdc
fix: removed controller logic
jaydip1235 Jun 20, 2024
6a2b130
fix: removed controller logic
jaydip1235 Jun 20, 2024
5a08114
Merge branch 'master' into question-bank-schema
jaydip1235 Jun 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .bash_profile.save
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file

26 changes: 26 additions & 0 deletions app/controllers/api/v1/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class Api::V1::CategoriesController < ApplicationController
before_action :authenticate_user!
# GET /api/v1/categories
def index
@categories = Category.all
render json: @categories
end

# POST /api/v1/categories
def create
@category = Category.new(category_params)
if @category.save
render json: @category, status: :created
else
render json: @category.errors, status: :unprocessable_entity
end
end

private
tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved

def category_params
tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved
params.require(:category).permit(:name)
end
end
27 changes: 27 additions & 0 deletions app/controllers/api/v1/difficulty_levels_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class Api::V1::DifficultyLevelsController < ApplicationController
before_action :authenticate_user!

# GET /api/v1/difficulty_levels
def index
@difficulty_levels = DifficultyLevel.all
render json: @difficulty_levels
end

# POST /api/v1/difficulty_levels
def create
@difficulty_level = DifficultyLevel.new(difficulty_level_params)
if @difficulty_level.save
render json: @difficulty_level, status: :created
else
render json: @difficulty_level.errors, status: :unprocessable_entity
end
end

tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved
private

def difficulty_level_params
tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved
params.require(:difficulty_level).permit(:name, :value)
end
end
49 changes: 49 additions & 0 deletions app/controllers/api/v1/questions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

class Api::V1::QuestionsController < ApplicationController
before_action :authenticate_user!
before_action :authorize_moderator, only: %i[create update destroy]
before_action :set_question, only: %i[update destroy]

# POST /api/v1/questions
def create
@question = Question.new(question_params)
Fixed Show fixed Hide fixed
if @question.save
render json: @question, status: :created
else
render json: @question.errors, status: :unprocessable_entity
end
end

# PUT /api/v1/questions/:id
def update
if @question.update(question_params)
Fixed Show fixed Hide fixed
render json: @question, status: :ok
else
render json: @question.errors, status: :unprocessable_entity
end
end

# DELETE /api/v1/questions/:id
def destroy
@question.destroy
head :no_content
end

private

def authorize_moderator
tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved
return if current_user.question_bank_moderator?

render json: { error: "Unauthorized" }, status: :unauthorized
end

def set_question
@question = Question.find(params[:id])
end

def question_params
params.require(:question).permit(:heading, :statement, :category_id, :difficulty_level_id, test_data: {},
circuit_boilerplate: {})
tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved
end
end
4 changes: 4 additions & 0 deletions app/helpers/api/v1/categories_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module Api::V1::CategoriesHelper
tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved
end
4 changes: 4 additions & 0 deletions app/helpers/api/v1/difficulty_levels_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Difficulty level has been moved to enum. So this will not required anymore.


module Api::V1::DifficultyLevelsHelper
tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved
end
4 changes: 4 additions & 0 deletions app/helpers/api/v1/questions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module Api::V1::QuestionsHelper
tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved
end
5 changes: 5 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class Category < ApplicationRecord
tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved
tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wil be better to rename the model to QuestionCategory

has_many :questions, dependent: :destroy
end
5 changes: 5 additions & 0 deletions app/models/difficulty_level.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class DifficultyLevel < ApplicationRecord
tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be better to rename the model to QuestionDifficultyLevel

has_many :questions, dependent: :destroy
end
6 changes: 6 additions & 0 deletions app/models/question.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class Question < ApplicationRecord
tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved
belongs_to :category
belongs_to :difficulty_level
end
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,20 @@ class User < ApplicationRecord
has_one_attached :profile_picture
before_validation { profile_picture.purge if remove_picture == "1" }

store_accessor :submission_history, :submissions
attr_accessor :remove_picture

validates :name, presence: true, format: { without: /\A["!@#$%^&]*\z/,
message: "can only contain letters and spaces" }

validates :email, presence: true, format: /\A[^@,\s]+@[^@,\s]+\.[^@,\s]+\z/

validates :public, inclusion: { in: [true, false] }

scope :subscribed, -> { where(subscribed: true) }

validates :question_bank_moderator, inclusion: { in: [true, false] }

include PgSearch::Model

pg_search_scope :text_search, against: %i[name educational_institute country]
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@
put "unsubscribe"
end
end

resources :difficulty_levels, only: [:create, :index]
resources :categories, only: [:create, :index]
resources :questions, only: [:create, :update, :destroy]
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20240601071847_create_difficulty_levels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateDifficultyLevels < ActiveRecord::Migration[7.0]
def change
create_table :difficulty_levels do |t|
t.string :name
t.integer :value

t.timestamps
end
end
end
9 changes: 9 additions & 0 deletions db/migrate/20240601071931_create_categories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateCategories < ActiveRecord::Migration[7.0]
def change
create_table :categories do |t|
t.string :name

t.timestamps
end
end
end
14 changes: 14 additions & 0 deletions db/migrate/20240601072328_create_questions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateQuestions < ActiveRecord::Migration[7.0]
def change
create_table :questions do |t|
t.string :heading
t.text :statement
t.references :category, null: false, foreign_key: true
t.references :difficulty_level, null: false, foreign_key: true
t.jsonb :test_data
t.jsonb :circuit_boilerplate

t.timestamps
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20240601072607_update_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class UpdateUsers < ActiveRecord::Migration[7.0]
def change
safety_assured do
change_table :users do |t|
t.jsonb :submission_history, array: true, default: []
t.boolean :public, default: true
t.boolean :question_bank_moderator, default: false
end
end
end
end
33 changes: 32 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_08_25_140331) do
ActiveRecord::Schema[7.0].define(version: 2024_06_01_072607) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -107,6 +107,12 @@
t.index ["group_id"], name: "index_assignments_on_group_id"
end

create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "collaborations", force: :cascade do |t|
t.bigint "user_id"
t.bigint "project_id"
Expand Down Expand Up @@ -167,6 +173,13 @@
t.index ["user_id"], name: "index_custom_mails_on_user_id"
end

create_table "difficulty_levels", force: :cascade do |t|
t.string "name"
t.integer "value"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "featured_circuits", force: :cascade do |t|
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
Expand Down Expand Up @@ -383,6 +396,19 @@
t.index ["user_id"], name: "index_push_subscriptions_on_user_id"
end

create_table "questions", force: :cascade do |t|
t.string "heading"
t.text "statement"
t.bigint "category_id", null: false
t.bigint "difficulty_level_id", null: false
t.jsonb "test_data"
t.jsonb "circuit_boilerplate"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["category_id"], name: "index_questions_on_category_id"
t.index ["difficulty_level_id"], name: "index_questions_on_difficulty_level_id"
end

create_table "stars", force: :cascade do |t|
t.bigint "user_id"
t.bigint "project_id"
Expand Down Expand Up @@ -451,6 +477,9 @@
t.string "educational_institute"
t.boolean "subscribed", default: true
t.string "locale"
t.jsonb "submission_history", default: [], array: true
t.boolean "public", default: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this added public field required ?

t.boolean "question_bank_moderator", default: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Expand Down Expand Up @@ -494,6 +523,8 @@
add_foreign_key "projects", "assignments"
add_foreign_key "projects", "projects", column: "forked_project_id"
add_foreign_key "projects", "users", column: "author_id"
add_foreign_key "questions", "categories"
add_foreign_key "questions", "difficulty_levels"
add_foreign_key "stars", "projects"
add_foreign_key "stars", "users"
add_foreign_key "taggings", "projects"
Expand Down
7 changes: 7 additions & 0 deletions spec/factories/categories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

FactoryBot.define do
tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved
factory :category do
name { "MyString" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
name { "MyString" }
name { Faker::Lorem.word }

Use faker instead of hard coded value

end
end
8 changes: 8 additions & 0 deletions spec/factories/difficulty_levels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the model has been removed and moved to enum, this file may be removed


FactoryBot.define do
tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved
factory :difficulty_level do
name { "MyString" }
value { 1 }
end
end
12 changes: 12 additions & 0 deletions spec/factories/questions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

FactoryBot.define do
tanmoysrt marked this conversation as resolved.
Show resolved Hide resolved
factory :question do
heading { "MyString" }
statement { "MyText" }
category { nil }
difficulty_level { nil }
test_data { "" }
circuit_boilerplate { "" }
end
Comment on lines +4 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Faker . Ref- https://github.com/faker-ruby/faker?tab=readme-ov-file#usage

For test_data and circuit_boilerplate, we may put a blank JSON instead of blank string

end
17 changes: 17 additions & 0 deletions spec/helpers/api/v1/categories_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require "rails_helper"

# Specs in this file have access to a helper object that includes
# the Api::V1::CategoriesHelper. For example:
#
# describe Api::V1::CategoriesHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe Api::V1::CategoriesHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
17 changes: 17 additions & 0 deletions spec/helpers/api/v1/difficulty_levels_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require "rails_helper"

# Specs in this file have access to a helper object that includes
# the Api::V1::DifficultyLevelsHelper. For example:
#
# describe Api::V1::DifficultyLevelsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe Api::V1::DifficultyLevelsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
17 changes: 17 additions & 0 deletions spec/helpers/api/v1/questions_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require "rails_helper"

# Specs in this file have access to a helper object that includes
# the Api::V1::QuestionsHelper. For example:
#
# describe Api::V1::QuestionsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe Api::V1::QuestionsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
7 changes: 7 additions & 0 deletions spec/models/category_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Category, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
7 changes: 7 additions & 0 deletions spec/models/difficulty_level_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe DifficultyLevel, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading
Loading