-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic backend logic for solving quiz
- Loading branch information
1 parent
4ecf16a
commit 8747fc8
Showing
13 changed files
with
152 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class SolvingQuizController < ApplicationController | ||
before_action :authenticate_user! | ||
|
||
def start_quiz | ||
quiz = Quiz.find(params[:quiz_id]) | ||
quiz_attempt = quiz.quiz_attempts.build(quiz_attempt_params) | ||
if quiz_attempt.save | ||
render json: quiz_attempt | ||
else | ||
render json: {error: quiz_attempt.errors.full_messages}, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def get_info_with_questions | ||
#quiz with questions and answers but without info about correct answers | ||
quiz = Quiz.find(params[:quiz_id]) | ||
render json: quiz, include: [:questions, :answers.except(:is_correct)] | ||
end | ||
|
||
def submit_answer | ||
quiz_attempt = QuizAttempt.find(params[:quiz_attempt_id]) | ||
answer = Answer.find(params[:answer_id]) | ||
user_answer_params = {quiz_attempt_id: quiz_attempt.id, answer_id: answer.id} | ||
user_answer = quiz_attempt.user_answers.build(user_answer_params) | ||
if user_answer.save | ||
render json: user_answer | ||
else | ||
render json: {error: user_answer.errors.full_messages}, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def finish_quiz | ||
quiz_attempt = QuizAttempt.find(params[:quiz_attempt_id]) | ||
quiz_attempt_params = {finished: true} | ||
quiz_attempt.update(quiz_attempt_params) | ||
render json: quiz_attempt | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
class Quiz < ApplicationRecord | ||
belongs_to :user | ||
has_many :questions, dependent: :destroy | ||
has_many :quiz_attempts, dependent: :destroy | ||
validates :name, presence: true | ||
validates :description, presence: true | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class QuizAttempt < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :quiz | ||
has_many :user_answers, dependent: :destroy | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
class UserAnswer < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :quiz_attempt | ||
belongs_to :answer | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class CreateQuizAttempts < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :quiz_attempts do |t| | ||
t.integer :user_id, null: false | ||
t.integer :quiz_id, null: false | ||
t.datetime :started_at | ||
t.datetime :finished_at | ||
t.boolean :is_active, default: true, null: false | ||
|
||
t.timestamps | ||
end | ||
|
||
add_foreign_key :quiz_attempts, :users | ||
add_foreign_key :quiz_attempts, :quizzes | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
backend/db/migrate/20230923163558_change_user_answers_user_id_to_quiz_attempt_id.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class ChangeUserAnswersUserIdToQuizAttemptId < ActiveRecord::Migration[7.0] | ||
def change | ||
rename_column :user_answers, :user_id, :quiz_attempt_id | ||
add_foreign_key :user_answers, :quiz_attempts | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters