Skip to content
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

[DEPRECATED PR]: structure flow to create the assignment #39

Closed

Conversation

Zzz212zzZ
Copy link

@Zzz212zzZ Zzz212zzZ commented Apr 21, 2024

General Info

  • Pivotal Tracker Story
  • Breaking?
    This is a deprecated pr messed up with many unrelated commits. So I created a new branch and made a new pr with clean commits, plus amended the code according to the comments in this deprecated pr.

Changes

Explain your changes here (in such a way that you would understand why you made them a year from now).

  1. Adjust the Assignment Table. Now it should rely on Course_to_lms Table instead of lms Table.
    image
  2. Implement the create method within the assignment controller

Testing

  • The assignment can be created only when both the course and lms exist before. It uses a foreign key of course_to_lms_id to ensure this.
  • We also do not want to create a duplicated test, i.e., there shouldn't exist such a record that has the same course_to_lms_id and external_assignment_id that we want to create.

Documentation

Updated E-R diagram

Checklist

  • Name of branch corresponds to story

@Zzz212zzZ Zzz212zzZ force-pushed the 187434339-structure-flow-to-create-the-assignment branch from cab3d24 to 569dc2e Compare April 22, 2024 18:21
Comment on lines +52 to 56
def validate_ids!
if params[:name].blank? || params[:external_assignment_id].blank? || params[:course_id].blank? || params[:lms_id].blank?
render json: { error: 'Params required' }, status: :bad_request
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also added a validation class to do some of this validation as well (eg: ensuring that they are integers). The reason why this is important is because we need to make sure we are not introducing any vulnerabilities with user provided input.

def assignment_exists?(course_to_lms, assignment_name, external_assignment_id)
existing_assignment = Assignment.find_by(course_to_lms_id: course_to_lms.id, name: assignment_name, external_assignment_id: external_assignment_id)
if existing_assignment
render json: { message: 'Record already exists' }, status: :ok
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This render causes side effects which we always try to avoid. Instead, we should try to make all renders happen from the controller method which the route calls.

Comment on lines +43 to +50
def create_and_render_assignment(course_to_lms, assignment_name, external_assignment_id)
assignment = Assignment.new(course_to_lms_id: course_to_lms.id, name: assignment_name, external_assignment_id: external_assignment_id)
if assignment.save
render json: assignment, status: :created
else
render json: assignment.errors, status: :unprocessable_entity
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say it might be better to include this in the controller method logic itself since it renders stuff. Since the method is clear about the fact that it renders, it makes it a bit clearer, but I still tend to avoid it since it is "side-effecty"

Comment on lines +58 to +60
def validate_ids!
if params[:course_id].blank? || params[:lms_id].blank? || params[:external_course_id].blank?
render json: { error: 'course_id and lms_id are required' }, status: :bad_request
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets also use the validation library for these as well

Comment on lines +22 to +23
t.bigint "course_to_lms_id", null: false
t.index ["course_to_lms_id"], name: "index_assignments_on_course_to_lms_id"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assignment is owned by a course which is owned by an lms so shouldn't we use that relation to get this value instead of adding it to every assignment?

t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "external_assignment_id"
t.index ["lms_id"], name: "index_assignments_on_lms_id"
t.bigint "course_to_lms_id", null: false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is supposed to be a reference to a join table, it should prob be a foreign key only

…434339-structure-flow-to-create-the-assignment
@Zzz212zzZ Zzz212zzZ marked this pull request as draft April 23, 2024 08:46
@Zzz212zzZ Zzz212zzZ changed the title [Add]: structure flow to create the assignment [DEPRECATED PR]: structure flow to create the assignment Apr 23, 2024
@Zzz212zzZ Zzz212zzZ closed this Apr 28, 2024
@Zzz212zzZ Zzz212zzZ deleted the 187434339-structure-flow-to-create-the-assignment branch April 28, 2024 04:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants