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

Refactor delete methods in the database #112

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

pstaabp
Copy link
Member

@pstaabp pstaabp commented Aug 18, 2022

This refactors all of the deleteXXX methods in the database layer.

  • Previously deleteXXX would return the deleted object.
  • At the db layer, nothing is returned.
  • At the mojolicious layer, a message saying XXX was deleted successfully.

In addition

  • all perl tests now delete objects then check that they are successfully deleted.
  • the client-side store tests also delete the object then checks the database for successful deletion.

Note: The UI never used a deleted object so no UI changes should occur.

@pstaabp pstaabp force-pushed the refactor-db-delete branch from e620138 to 2560ddc Compare August 26, 2022 17:18
Copy link
Member

@drdrew42 drdrew42 left a comment

Choose a reason for hiding this comment

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

I'm concerned about lack of Exceptions on delete calls. Roughly speaking, IMO, the approach should be:

  1. Fetch the object to be deleted
  2. If the object doesn't exist, throw NotFoundException
  3. Delete the object

And on the UI side:

  1. Check that the object exists in the store
  2. If the object doesn't exist, display error message and halt
  3. Make API call
  4. Handle API response (update store or handle exception message)

Comment on lines 437 to +439
sub removePoolProblem ($self, %args) {
my $prob = $self->getPoolProblem(info => $args{info}, as_result_set => 1);
DB::Exception::PoolProblemNotInPool->throw(info => $args{info}) unless defined($prob);

my $prob2 = $prob->delete;
return $prob2 if $args{as_result_set};
return { $prob2->get_inflated_columns };
$self->getPoolProblem(info => $args{info}, as_result_set => 1)->delete;
return;
Copy link
Member

Choose a reason for hiding this comment

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

Not part of this PR, but why is this called remove instead of delete?

Copy link
Member Author

Choose a reason for hiding this comment

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

My thought is that we are removing a problem from a pool, not deleting the problem. Happy to switch to delete though.

Comment on lines 280 to 282
const index = this.db_user_sets.findIndex((set) => set.user_set_id === user_set.user_set_id);
if (index < 0) {
logger.error('[user store/deleteUserSet]: the user set was not found in the store');
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we be checking that the user set is present before making the API call? This is an error that 'should never happen', but it doesn't make sense to check afterwards.

Also, I worry about having removed the Exceptions on the DB side for attempting to delete objects that don't exist... that should be an error still, and the response wouldn't be 200 OK.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will switch the order of these. About the exception, see my comment below.

Comment on lines 174 to 176
const index = this.set_problems.findIndex(prob => prob.set_problem_id === problem.set_problem_id);
if (index < 0) {
logger.error('[stores/set_problems/deleteSetProblem]: the set problem was not found in the store');
Copy link
Member

Choose a reason for hiding this comment

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

Check before API

Comment on lines 267 to 269
.findIndex(user_problem => user_problem.user_problem_id === user_problem.user_problem_id);
if (index < 0) {
logger.error('[stores/set_problems/deleteUserProblem]: the set problem was not found in the store');
Copy link
Member

Choose a reason for hiding this comment

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

Check before API

Comment on lines 163 to 165
const index = this.users.findIndex((u) => u.user_id === user.user_id);
// splice is used so vue3 reacts to changes.
this.users.splice(index, 1);
return new User(response.data as ParseableUser);
if (index < 0) {
logger.error('[user store/deleteUser]: the user was not found in the store');
Copy link
Member

Choose a reason for hiding this comment

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

Check before API

Comment on lines 272 to 274
const index = this.db_course_users.findIndex((u) => u.course_user_id === course_user.course_user_id);

// splice is used so vue3 reacts to changes.
this.db_course_users.splice(index, 1);
const deleted_course_user = new DBCourseUser(response.data as ParseableCourseUser);
const user = this.users.find(u => u.user_id === deleted_course_user.user_id);
return new CourseUser(Object.assign({}, user?.toObject(), deleted_course_user.toObject()));
if (index < 0) {
logger.error('[user store/deleteCourseUser]: the user was not found in the store');
Copy link
Member

Choose a reason for hiding this comment

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

Check before API

@pstaabp
Copy link
Member Author

pstaabp commented Aug 31, 2022

There are still exceptions thrown. For example, that you mention above, on ProblemPool, the getProblemPool will throw an exception if it doesn't exist. This is how all of the deleteXXX methods work as well.

…g API call.

FIX: refactor deleteXXX functions to check for existence before making API call.
@pstaabp pstaabp force-pushed the refactor-db-delete branch from 566a80f to 7aff436 Compare August 31, 2022 12:11
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.

2 participants