-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: circuitverse-practice-section
Are you sure you want to change the base?
Changes from 24 commits
c0ef621
3c25201
bbba47a
e648c0d
f5b4af6
06153e5
a7088cf
5596f8b
2c7d08b
f5411e0
b3163be
d1164ad
894cb72
269bd4f
fabe0d4
4b01c19
4460689
6d3f7b1
cb5a4ff
f294787
b537403
f6a16ca
decdfcb
75bdba5
b551389
b3b26e0
7bc691d
4aebca3
6d34c74
e597f60
73fdab7
3274b23
f5165ed
652823b
f499b42
10a6ac6
2c5a3b2
703ae68
ae3f0e1
e0b6cec
c7daad0
38903cd
9daaa48
bd85dd3
508baff
2cb6490
36a4fd5
9ff671f
897527a
511b237
b005cdc
6a2b130
5a08114
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES | ||
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 |
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 |
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) | ||
|
||
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) | ||
|
||
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 |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It wil be better to rename the model to |
||
has_many :questions, dependent: :destroy | ||
end |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be better to rename the model to |
||
has_many :questions, dependent: :destroy | ||
end |
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 |
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 |
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 |
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 |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
||
|
@@ -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" | ||
|
@@ -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 | ||
|
@@ -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" | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this added |
||
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 | ||
|
@@ -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" | ||
|
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" } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Use faker instead of hard coded value |
||||||
end | ||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use For test_data and circuit_boilerplate, we may put a blank JSON instead of blank string |
||
end |
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 |
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 |
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 |
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 |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this file