From 7aff4369698db093b5b02c5d05c1717cfba7de3d Mon Sep 17 00:00:00 2001 From: Peter Staab Date: Wed, 31 Aug 2022 08:11:21 -0400 Subject: [PATCH] FIX: refactor deleteXXX functions to check for existence before making API call. FIX: refactor deleteXXX functions to check for existence before making API call. --- src/stores/courses.ts | 14 ++++++----- src/stores/problem_sets.ts | 31 ++++++++++++----------- src/stores/set_problems.ts | 41 +++++++++++++++++-------------- src/stores/users.ts | 36 +++++++++++++-------------- tests/stores/set_problems.spec.ts | 1 - 5 files changed, 64 insertions(+), 59 deletions(-) diff --git a/src/stores/courses.ts b/src/stores/courses.ts index fa31472b..11e63684 100644 --- a/src/stores/courses.ts +++ b/src/stores/courses.ts @@ -78,12 +78,14 @@ export const useCourseStore = defineStore('courses', { * This deletes the course in the database and the store. */ async deleteCourse(course: Course): Promise { - const response = await api.delete(`courses/${course.course_id}`); - if (response.status === 200) { - const index = this.courses.findIndex(c => c.course_id === course.course_id); - this.courses.splice(index, 1); - } else { - throw response.data as ResponseError; + const index = this.courses.findIndex(c => c.course_id === course.course_id); + if (index >= 0) { + const response = await api.delete(`courses/${course.course_id}`); + if (response.status === 200) { + this.courses.splice(index, 1); + } else { + throw response.data as ResponseError; + } } } } diff --git a/src/stores/problem_sets.ts b/src/stores/problem_sets.ts index fded4414..cb3eecce 100644 --- a/src/stores/problem_sets.ts +++ b/src/stores/problem_sets.ts @@ -181,17 +181,18 @@ export const useProblemSetStore = defineStore('problem_sets', { * Delete the given ProblemSet from the database and the store. */ async deleteProblemSet(set: ProblemSet): Promise { - const response = await api.delete(`courses/${set.course_id}/sets/${set.set_id}`); - if (response.status === 200) { - const index = this.problem_sets.findIndex((s) => s.set_id === set.set_id); - if (index < 0) { - logger.error('[problem_set store/deleteProblemSet]: the problem set was not found in the store'); + const index = this.problem_sets.findIndex((s) => s.set_id === set.set_id); + if (index >= 0) { + const response = await api.delete(`courses/${set.course_id}/sets/${set.set_id}`); + if (response.status === 200) { + this.problem_sets.splice(index, 1); + } else { // splice is used so vue3 reacts to changes. - this.problem_sets.splice(index, 1); + logger.error(JSON.stringify(response)); } } else { - logger.error(JSON.stringify(response)); + logger.error('[problem_set store/deleteProblemSet]: the problem set was not found in the store'); } }, // UserSet actions @@ -274,18 +275,18 @@ export const useProblemSetStore = defineStore('problem_sets', { */ async deleteUserSet(user_set: UserSet): Promise { const course_id = useSessionStore().course.course_id; - const response = await - api.delete(`courses/${course_id}/sets/${user_set.set_id}/users/${user_set.course_user_id ?? 0}`); - if (response.status === 200) { - 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'); - } else { + const index = this.db_user_sets.findIndex((set) => set.user_set_id === user_set.user_set_id); + if (index >= 0) { + const response = await + api.delete(`courses/${course_id}/sets/${user_set.set_id}/users/${user_set.course_user_id ?? 0}`); + if (response.status === 200) { // splice is used so vue3 reacts to changes. this.db_user_sets.splice(index, 1); + } else { + logger.error(JSON.stringify(response)); } } else { - logger.error(JSON.stringify(response)); + logger.error('[user store/deleteUserSet]: the user set was not found in the store'); } }, diff --git a/src/stores/set_problems.ts b/src/stores/set_problems.ts index 10f49c32..04b2b734 100644 --- a/src/stores/set_problems.ts +++ b/src/stores/set_problems.ts @@ -168,15 +168,18 @@ export const useSetProblemStore = defineStore('set_problems', { */ async deleteSetProblem(problem: SetProblem): Promise { const course_id = useSessionStore().course.course_id; - - await api.delete(`courses/${course_id}/sets/${ - problem.set_id}/problems/${problem.set_problem_id}`); 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'); - } else { + if (index >= 0) { + const response = await api.delete(`courses/${course_id}/sets/${problem.set_id + }/problems/${problem.set_problem_id}`); + if (response.status === 200) { // splice is used so vue3 reacts to changes. - this.set_problems.splice(index, 1); + this.set_problems.splice(index, 1); + } else { + logger.error(JSON.stringify(response)); + } + } else { + logger.error('[stores/set_problems/deleteSetProblem]: the set problem was not found in the store'); } }, // UserProblem actions @@ -256,20 +259,22 @@ export const useSetProblemStore = defineStore('set_problems', { const course_id = useSessionStore().course.course_id; const set_problem = this.set_problems.find(prob => prob.set_problem_id === user_problem.set_problem_id); const problem_set_store = useProblemSetStore(); - const user_set = problem_set_store.findUserSet({ user_set_id: user_problem.user_set_id, }); - if (user_set == undefined) { - throw 'deleteUserProblem: returned undefined user set'; - } - await api.delete(`courses/${course_id}/sets/${set_problem?.set_id ?? 0 - }/users/${user_set.user_id}/problems/${user_problem.user_problem_id}`); - const index = this.db_user_problems .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'); - } else { + if (index >= 0) { + const user_set = problem_set_store.findUserSet({ user_set_id: user_problem.user_set_id, }); + if (user_set == undefined) throw 'deleteUserProblem: returned undefined user set'; + + const response = await api.delete(`courses/${course_id}/sets/${set_problem?.set_id ?? 0 + }/users/${user_set?.user_id}/problems/${user_problem.user_problem_id}`); + if (response.status === 200) { // splice is used so vue3 reacts to changes. - this.set_problems.splice(index, 1); + this.set_problems.splice(index, 1); + } else { + logger.error(JSON.stringify(response)); + } + } else { + logger.error('[stores/set_problems/deleteUserProblem]: the set problem was not found in the store'); } } } diff --git a/src/stores/users.ts b/src/stores/users.ts index 8045648f..d80a5161 100644 --- a/src/stores/users.ts +++ b/src/stores/users.ts @@ -156,19 +156,18 @@ export const useUserStore = defineStore('user', { * Deletes the given User in the database and in the store. */ async deleteUser(user: User): Promise { - const session_store = useSessionStore(); - const course_id = session_store.course.course_id; - const response = await api.delete(`courses/${course_id}/global-users/${user.user_id ?? 0}`); - if (response.status === 200) { - const index = this.users.findIndex((u) => u.user_id === user.user_id); - if (index < 0) { - logger.error('[user store/deleteUser]: the user was not found in the store'); - } else { - // splice is used so vue3 reacts to changes. + const course_id = useSessionStore().course.course_id; + const index = this.users.findIndex((u) => u.user_id === user.user_id); + if (index >= 0) { + const response = await api.delete(`courses/${course_id}/global-users/${user.user_id ?? 0}`); + if (response.status === 200) { + // splice is used so vue3 reacts to changes. this.users.splice(index, 1); + } else { + logger.error(JSON.stringify(response)); } } else { - logger.error(JSON.stringify(response)); + logger.error('[user store/deleteUser]: the user was not found in the store'); } }, @@ -267,18 +266,17 @@ export const useUserStore = defineStore('user', { * Deletes a Course User from the store and the database. */ async deleteCourseUser(course_user: CourseUser): Promise { - const response = await api.delete(`courses/${course_user.course_id}/users/${course_user.user_id}`); - if (response.status === 200) { - const index = this.db_course_users.findIndex((u) => u.course_user_id === course_user.course_user_id); - if (index < 0) { - logger.error('[user store/deleteCourseUser]: the user was not found in the store'); - } else { + const index = this.db_course_users.findIndex((u) => u.course_user_id === course_user.course_user_id); + if (index >= 0) { + const response = await api.delete(`courses/${course_user.course_id}/users/${course_user.user_id}`); + if (response.status === 200) { // splice is used so vue3 reacts to changes. this.db_course_users.splice(index, 1); + } else { + logger.error(JSON.stringify(response)); } - } else if (response.status === 250) { - logger.error(response.data); - throw response.data as ResponseError; + } else { + logger.error('[user store/deleteCourseUser]: the user was not found in the store'); } }, clearAll() { diff --git a/tests/stores/set_problems.spec.ts b/tests/stores/set_problems.spec.ts index 4c70660b..8c629515 100644 --- a/tests/stores/set_problems.spec.ts +++ b/tests/stores/set_problems.spec.ts @@ -29,7 +29,6 @@ import { Dictionary, generic } from 'src/common/models'; import { loadCSV, cleanIDs } from '../utils'; import { checkPassword } from 'src/common/api-requests/session'; -import { logger } from 'src/boot/logger'; const app = createApp({});