Skip to content

Commit

Permalink
FIX: refactor deleteXXX functions to check for existence before makin…
Browse files Browse the repository at this point in the history
…g API call.

FIX: refactor deleteXXX functions to check for existence before making API call.
  • Loading branch information
pstaabp committed Aug 31, 2022
1 parent 2560ddc commit 7aff436
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 59 deletions.
14 changes: 8 additions & 6 deletions src/stores/courses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ export const useCourseStore = defineStore('courses', {
* This deletes the course in the database and the store.
*/
async deleteCourse(course: Course): Promise<void> {
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;
}
}
}
}
Expand Down
31 changes: 16 additions & 15 deletions src/stores/problem_sets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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
Expand Down Expand Up @@ -274,18 +275,18 @@ export const useProblemSetStore = defineStore('problem_sets', {
*/
async deleteUserSet(user_set: UserSet): Promise<void> {
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');
}
},

Expand Down
41 changes: 23 additions & 18 deletions src/stores/set_problems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,18 @@ export const useSetProblemStore = defineStore('set_problems', {
*/
async deleteSetProblem(problem: SetProblem): Promise<void> {
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
Expand Down Expand Up @@ -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');
}
}
}
Expand Down
36 changes: 17 additions & 19 deletions src/stores/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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');
}
},

Expand Down Expand Up @@ -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<void> {
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() {
Expand Down
1 change: 0 additions & 1 deletion tests/stores/set_problems.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({});

Expand Down

0 comments on commit 7aff436

Please sign in to comment.