Skip to content

Implement clean architecture across backend services and frontend api layer - #1

Merged
mfarim merged 5 commits into
mainfrom
refactor/clean-architecture-implementation
Oct 13, 2026
Merged

mfarim merged 5 commits into
mainfrom
refactor/clean-architecture-implementation

Conversation

@mfarim

@mfarim mfarim commented Oct 13, 2026

Copy link
Copy Markdown
Owner

Overview

This PR refactors both the Ruby on Rails 8 Backend and React 19 + TypeScript Frontend to follow Clean Architecture principles. It establishes clear separation of concerns, decouples persistence and domain logic from HTTP delivery, introduces a typed frontend API service layer, and improves testability and maintainability.


Architectural Changes

1. Backend (Ruby on Rails 8)

  • Repository Layer (app/repositories/):

    • Introduced BaseRepository providing generalized CRUD operations.
    • Implemented domain-specific repositories: UserRepository, ClassroomRepository, ExaminationRepository, ExamAttemptRepository, and AssignmentRepository.
    • Abstracted direct ActiveRecord queries out of controllers.
  • Use Cases / Interactors (app/use_cases/):

    • Implemented UseCaseResult monad for consistent success/failure responses without swallowing runtime exceptions.
    • Auth domain: Auth::AuthenticateUser (BCrypt password check & JWT generation), Auth::RegisterStudent (transactional user + student profile creation).
    • CBT / Exam domain: Exams::StartExamAttempt (timer calculation, question/option shuffling, resume state), Exams::SaveExamAnswer (autosave answer validation), and Exams::SubmitExamAttempt (automatic grading & passing score calculation).
    • Assignment domain: Assignments::SubmitAssignment and Assignments::GradeAssignment.
  • Thin Controllers (app/controllers/api/v1/):

    • BaseController: added handle_result(result) helper to map use case outcomes directly to camelCase JSON API responses.
    • Refactored AuthController, ClassroomsController, ExaminationsController, ExamRunnerController, and AssignmentsController to delegate business logic to use cases and repositories.
  • Environment & Compatibility:

    • Added explicit csv gem dependency in Gemfile for Ruby 3.4/4.0 standard library compatibility.
    • Configured PostgreSQL fallback port (5435) in config/database.yml matching the Docker Compose configuration.

2. Frontend (React 19 + TypeScript)

  • Typed API Service Layer (src/services/):

    • Created modular services isolating HTTP communication from UI components:
      • authService.ts: login, register, profile, impersonation.
      • classroomService.ts, subjectService.ts, teacherService.ts, studentService.ts.
      • examService.ts: exam management, questions bank, CBT runner endpoints, proctoring/violation telemetry.
      • assignmentService.ts, materialService.ts, announcementService.ts, gradeService.ts.
      • Barrel export via index.ts.
  • State Management & Components:

    • Refactored authStore.ts (Zustand) to delegate network operations to authService.
    • Refactored Classrooms.tsx to consume classroomService and teacherService.
    • Fixed React compiler and linter warnings in ExamRunner.tsx and ExamMonitor.tsx by defining useCallback hooks with correct dependency chains before effects.

Verification & Testing

  • Backend RSpec Test Suite:
    • Ran bundle exec rspec — all tests passing:
      14 examples, 0 failures (Randomized with seed 53739)
  • Zeitwerk Autoloader Check:
    • Ran bundle exec rails zeitwerk:check — all constant names and namespaces validated.
  • Frontend Build & Linter:
    • Ran npm run build (tsc -b && vite build) — 0 TypeScript errors.
    • Ran npm run lint — 0 syntax / compiler errors.

Checklist

  • Code adheres to repository styling conventions and Clean Architecture standards.
  • All RSpec unit and request tests pass.
  • Frontend builds without TypeScript or bundling errors.
  • Backward compatibility preserved for all API request/response contracts.

@mfarim mfarim self-assigned this Oct 13, 2026
@mfarim mfarim added the enhancement New feature or request label Oct 13, 2026
@mfarim
mfarim merged commit 7f90484 into main Oct 13, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant