forked from saasbook/flextensions
-
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]: structure flow to create the assignment new [new pr] (#43)
* Adjust Assignment Table, which should rely on course_to_lms instead of only lms * Add assignment model * Implement route of create in assignment controller * Add course, lms and course_to_lms model for Assignment Controller test * Add test for create in assignment controller * Refactor to pass the Codeclimate * Refactor the controller, by adding intger validation and putting ender from helper function to create function * Fix minor issue based on pr comments * Add one test to reach a full LOC test coverage * Add validation check for lms * Use validation helper * Update app/helpers/canvas_validation_helper.rb Co-authored-by: Connor <[email protected]> * Remove unecessary Rspec --------- Co-authored-by: Connor <[email protected]>
- Loading branch information
1 parent
6dbd465
commit 6b89523
Showing
7 changed files
with
163 additions
and
28 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 |
---|---|---|
@@ -1,20 +1,62 @@ | ||
module Api | ||
module V1 | ||
class AssignmentsController < ApplicationController | ||
module V1 | ||
class AssignmentsController < ApplicationController | ||
include CanvasValidationHelper | ||
before_action :validate_ids!, only: [:create] | ||
|
||
def index | ||
render json: 'not yet implemented', status: 501 | ||
def index | ||
render json: { message: 'not yet implemented'} , status: 501 | ||
end | ||
|
||
# POST /courses/:course_id/lms/:lms_id/assignments | ||
def create | ||
# Check if the course_to_lms association exists | ||
course_to_lms = CourseToLms.find_by(course_id: params[:course_id], lms_id: params[:lms_id]) | ||
|
||
unless course_to_lms | ||
render json: { error: 'No such Course_LMS association' }, status: :not_found | ||
return | ||
end | ||
|
||
def create | ||
render json: 'not yet implemented', status: 501 | ||
# Check if the assignment already exists | ||
if Assignment.exists?(course_to_lms_id: course_to_lms.id, name: params[:name], external_assignment_id: params[:external_assignment_id]) | ||
render json: { message: 'Record already exists' }, status: :ok | ||
return | ||
end | ||
|
||
def destroy | ||
render json: 'not yet implemented', status: 501 | ||
# Create and render the assignment | ||
assignment = Assignment.new(course_to_lms_id: course_to_lms.id, name: params[:name], external_assignment_id: params[:external_assignment_id]) | ||
if assignment.save | ||
render json: assignment, status: :created | ||
else | ||
render json: assignment.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
render json: { message: 'not yet implemented'} , status: 501 | ||
end | ||
|
||
private | ||
|
||
|
||
|
||
def validate_ids! | ||
begin | ||
params.require([:course_id, :lms_id, :name, :external_assignment_id]) | ||
rescue ActionController::ParameterMissing => e | ||
render json: { error: e.message }, status: :bad_request | ||
return | ||
end | ||
|
||
|
||
# Validate that course_id and lms_id are integers | ||
unless is_valid_course_id(params[:course_id].to_i) && is_valid_lms_id(params[:lms_id].to_i) | ||
render json: { error: 'course_id and lms_id must be integers' }, status: :bad_request | ||
return | ||
end | ||
end | ||
|
||
end | ||
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
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,7 +1,8 @@ | ||
# app/models/assignment.rb | ||
class Assignment < ApplicationRecord | ||
#Relationship with Lms | ||
belongs_to :lms | ||
|
||
#Relationship with Extension | ||
belongs_to :course_to_lms | ||
|
||
validates :name, presence: true | ||
validates :external_assignment_id, presence: true | ||
has_many :extensions | ||
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
8 changes: 8 additions & 0 deletions
8
db/migrate/20240420232708_change_lms_id_to_course_to_lms_id_in_assignments.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,8 @@ | ||
class ChangeLmsIdToCourseToLmsIdInAssignments < ActiveRecord::Migration[7.1] | ||
def change | ||
remove_index :assignments, :lms_id | ||
remove_column :assignments, :lms_id, :bigint | ||
add_column :assignments, :course_to_lms_id, :bigint, null: false | ||
add_foreign_key :assignments, :course_to_lmss, column: :course_to_lms_id | ||
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
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