Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@
# Ignore key files for decrypting credentials and more.
/config/*.key

# RSpec runtime files
/spec/examples.txt

3 changes: 3 additions & 0 deletions backend/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ gem "rack-cors"
# Redis client for cache and Action Cable
gem "redis", "~> 5.1"

# CSV gem (bundled gem in Ruby 3.4+)
gem "csv"

# Roo for reading Excel (.xlsx, .xls) files
gem "roo", "~> 2.10"

Expand Down
3 changes: 3 additions & 0 deletions backend/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ GEM
concurrent-ruby (1.3.8)
connection_pool (3.0.2)
crass (1.0.7)
csv (3.3.6)
database_cleaner-active_record (2.2.2)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0)
Expand Down Expand Up @@ -339,6 +340,7 @@ DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap
caxlsx (~> 4.2)
csv
database_cleaner-active_record
debug
factory_bot_rails
Expand Down Expand Up @@ -378,6 +380,7 @@ CHECKSUMS
concurrent-ruby (1.3.8) sha256=b2f1be836e968ccc78ccfce277ea79c72a88633f22306782c16ff23fb415d1e1
connection_pool (3.0.2) sha256=33fff5ba71a12d2aa26cb72b1db8bba2a1a01823559fb01d29eb74c286e62e0a
crass (1.0.7) sha256=94868719948664c89ddcaf0a37c65048413dfcb1c869470a5f7a7ceb5390b295
csv (3.3.6) sha256=aba61e7e507a66f03d45cb1f3c4b6359861c3504038b422962875dce099e4456
database_cleaner-active_record (2.2.2) sha256=88296b9f3088c31f7c0d4fcec10f68e4b71c96698043916de59b04debec10388
database_cleaner-core (2.1.0) sha256=b2875266d9b26b716e8b669c883e01c5250839f6f2ec56422b5e79aa97fb6927
date (3.6.0) sha256=18ac945c4e24ebd93534f8eab8c2fd8c5604b3e977105b3798d62a1e8d27b580
Expand Down
111 changes: 59 additions & 52 deletions backend/app/controllers/api/v1/assignments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@ class AssignmentsController < BaseController
before_action :set_assignment, only: [:show, :update, :destroy, :submit, :submissions, :discussions, :create_discussion]

def index
scope = Assignment.includes(:teacher, :subject, :classroom).order(due_date: :asc)
scope = assignment_repository.list_for_user(current_user)
student = current_user.student

if current_user.teacher?
scope = scope.where(teacher_id: current_user.teacher&.id)
elsif student
scope = scope.where(classroom_id: student.classroom_id, status: 'published')
end

submissions_map = student ? AssignmentSubmission.where(student_id: student.id).index_by(&:assignment_id) : {}

data = scope.map { |a| assignment_payload(a, submissions_map[a.id]) }
Expand All @@ -30,7 +23,7 @@ def create
return if performed?

teacher = current_user.teacher || Teacher.first
assignment = Assignment.create!(
assignment = assignment_repository.create!(
teacher: teacher,
subject_id: params[:subjectId],
classroom_id: params[:classroomId],
Expand Down Expand Up @@ -69,25 +62,26 @@ def destroy
require_teacher!
return if performed?

@assignment.destroy!
assignment_repository.delete!(@assignment.id)
json_response(data: nil, message: "Assignment deleted successfully")
end

def submit
require_student!
return if performed?

student = current_user.student
return render_error("Student record not found") unless student

sub = AssignmentSubmission.find_or_initialize_by(assignment: @assignment, student: student)
sub.file_path = params[:filePath] if params[:filePath].present?
sub.notes = params[:notes] if params[:notes].present?
sub.submitted_at = Time.current
sub.status = Time.current > @assignment.due_date ? 'late' : 'submitted'
sub.save!
result = submit_assignment_use_case.call(
assignment_id: @assignment.id,
student: current_user.student,
file_path: params[:filePath],
notes: params[:notes]
)

json_response(data: submission_payload(sub), message: "Assignment submitted successfully")
if result.success?
json_response(data: submission_payload(result.data), message: "Assignment submitted successfully")
else
render_error(result.error, status: result.status, errors: result.errors)
end
end

def submissions
Expand All @@ -103,14 +97,17 @@ def grade_submission
require_teacher!
return if performed?

sub = AssignmentSubmission.find(params[:submission_id])
sub.update!(
result = grade_assignment_use_case.call(
submission_id: params[:submission_id],
score: params[:score],
feedback: params[:feedback],
graded_at: Time.current,
status: 'graded'
feedback: params[:feedback]
)
json_response(data: submission_payload(sub), message: "Submission graded successfully")

if result.success?
json_response(data: submission_payload(result.data), message: "Submission graded successfully")
else
render_error(result.error, status: result.status, errors: result.errors)
end
end

def discussions
Expand All @@ -132,31 +129,43 @@ def create_discussion

private

def assignment_repository
@assignment_repository ||= AssignmentRepository.new
end

def submit_assignment_use_case
@submit_assignment_use_case ||= Assignments::SubmitAssignment.new(
assignment_repo: assignment_repository
)
end

def grade_assignment_use_case
@grade_assignment_use_case ||= Assignments::GradeAssignment.new
end

def set_assignment
@assignment = Assignment.find(params[:id])
@assignment = assignment_repository.find(params[:id])
end

def assignment_payload(a, submission = nil)
{
id: a.id,
teacherId: a.teacher_id,
teacherName: a.teacher&.name,
title: a.title,
description: a.description,
instructions: a.instructions,
subjectId: a.subject_id,
subjectName: a.subject&.name,
classroomId: a.classroom_id,
classroomName: a.classroom&.name,
title: a.title,
description: a.description,
instructions: a.instructions,
teacherId: a.teacher_id,
teacherName: a.teacher&.name,
maxScore: a.max_score,
dueDate: a.due_date&.iso8601,
allowLateSubmission: a.allow_late_submission,
status: a.status,
createdAt: a.created_at&.iso8601,
hasSubmitted: submission.present?,
studentScore: submission&.score,
studentSubmissionStatus: submission&.status,
studentSubmittedAt: submission&.submitted_at&.iso8601
submissionsCount: a.submissions.count,
submission: submission ? submission_payload(submission) : nil,
createdAt: a.created_at&.iso8601
}
end

Expand All @@ -165,29 +174,27 @@ def submission_payload(s)
id: s.id,
assignmentId: s.assignment_id,
studentId: s.student_id,
studentName: s.student.name,
studentNis: s.student.nis,
studentName: s.student&.name,
nis: s.student&.nis,
filePath: s.file_path,
notes: s.notes,
submittedAt: s.submitted_at&.iso8601,
score: s.score,
feedback: s.feedback,
status: s.status,
submittedAt: s.submitted_at&.iso8601,
gradedAt: s.graded_at&.iso8601
status: s.status
}
end

def discussion_payload(c, all_comments)
replies = all_comments.select { |r| r.parent_id == c.id }.map { |r| discussion_payload(r, all_comments) }
def discussion_payload(comment, all_comments)
replies = all_comments.select { |c| c.parent_id == comment.id }
{
id: c.id,
assignmentId: c.assignment_id,
userId: c.user_id,
userName: c.user.name,
userRole: c.user.role,
message: c.message,
createdAt: c.created_at&.iso8601,
replies: replies
id: comment.id,
userId: comment.user_id,
userName: comment.user&.name,
userRole: comment.user&.role,
message: comment.message,
createdAt: comment.created_at&.iso8601,
replies: replies.map { |r| discussion_payload(r, all_comments) }
}
end
end
Expand Down
60 changes: 27 additions & 33 deletions backend/app/controllers/api/v1/auth_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,22 @@ class AuthController < BaseController
skip_before_action :authenticate_user!, only: [:login, :register]

def login
user = User.find_by(email: params[:email])
if user&.authenticate(params[:password])
token = JsonWebToken.encode(user_id: user.id)
json_response(data: build_auth_payload(user, token), message: "Login successful")
else
render_error("Invalid email or password", status: :unauthorized)
end
result = authenticate_user_use_case.call(
email: params[:email],
password: params[:password]
)
handle_result(result, message: "Login successful")
end

def register
User.transaction do
user = User.create!(
name: params[:name],
email: params[:email],
password: params[:password],
role: 'ROLE_STUDENT',
active: true
)

nis = params[:nis].presence || "NIS#{Time.current.to_i % 1000000}"
Student.create!(
user: user,
name: user.name,
email: user.email,
nis: nis,
gender: params[:gender].presence || 'M',
active: true
)

token = JsonWebToken.encode(user_id: user.id)
json_response(data: build_auth_payload(user, token), message: "Registration successful", status: :created)
end
rescue ActiveRecord::RecordInvalid => e
render_error(e.message, status: :unprocessable_entity)
result = register_student_use_case.call(
name: params[:name],
email: params[:email],
password: params[:password],
nis: params[:nis],
gender: params[:gender]
)
handle_result(result, message: "Registration successful")
end

def me
Expand All @@ -57,21 +39,33 @@ def impersonate
require_admin!
return if performed?

target_user = User.find(params[:user_id])
target_user = user_repository.find(params[:user_id])
token = JsonWebToken.encode(user_id: target_user.id)
payload = build_auth_payload(target_user, token, impersonated_by: current_user.id)
json_response(data: payload, message: "Impersonation started successfully")
end

def stop_impersonate
admin_user = User.find_by(role: 'ROLE_ADMIN') || current_user
admin_user = user_repository.find_admin || current_user
token = JsonWebToken.encode(user_id: admin_user.id)
payload = build_auth_payload(admin_user, token)
json_response(data: payload, message: "Returned to admin session")
end

private

def user_repository
@user_repository ||= UserRepository.new
end

def authenticate_user_use_case
@authenticate_user_use_case ||= Auth::AuthenticateUser.new(user_repo: user_repository)
end

def register_student_use_case
@register_student_use_case ||= Auth::RegisterStudent.new(user_repo: user_repository)
end

def build_auth_payload(user, token, impersonated_by: nil)
{
accessToken: token,
Expand Down
8 changes: 8 additions & 0 deletions backend/app/controllers/api/v1/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def require_student!
render_error("Forbidden: Student access required", status: :forbidden) unless @current_user&.student? || @current_user&.admin?
end

def handle_result(result, message: "Success")
if result.success?
json_response(data: result.data, message: message, status: result.status)
else
render_error(result.error, status: result.status, errors: result.errors)
end
end

def json_response(data: nil, message: "Success", status: :ok, errors: nil)
camelized_data = camelize_keys(data.as_json)

Expand Down
12 changes: 8 additions & 4 deletions backend/app/controllers/api/v1/classrooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ClassroomsController < BaseController
before_action :set_classroom, only: [:show, :update, :destroy]

def index
classrooms = Classroom.includes(:homeroom_teacher).order(:level, :name)
classrooms = classroom_repository.list_with_homeroom
data = classrooms.map { |c| classroom_payload(c) }
json_response(data: data)
end
Expand All @@ -17,7 +17,7 @@ def create
require_admin!
return if performed?

classroom = Classroom.create!(classroom_params)
classroom = classroom_repository.create!(classroom_params)
json_response(data: classroom_payload(classroom), message: "Classroom created successfully", status: :created)
end

Expand All @@ -33,14 +33,18 @@ def destroy
require_admin!
return if performed?

@classroom.destroy!
classroom_repository.delete!(@classroom.id)
json_response(data: nil, message: "Classroom deleted successfully")
end

private

def classroom_repository
@classroom_repository ||= ClassroomRepository.new
end

def set_classroom
@classroom = Classroom.find(params[:id])
@classroom = classroom_repository.find(params[:id])
end

def classroom_params
Expand Down
Loading
Loading