Skip to content

Commit 566a80f

Browse files
committed
FIX: refactor deleteXXX functions to check for existence before making API call.
1 parent 2560ddc commit 566a80f

File tree

5 files changed

+67
-60
lines changed

5 files changed

+67
-60
lines changed

src/stores/courses.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,14 @@ export const useCourseStore = defineStore('courses', {
7878
* This deletes the course in the database and the store.
7979
*/
8080
async deleteCourse(course: Course): Promise<void> {
81-
const response = await api.delete(`courses/${course.course_id}`);
82-
if (response.status === 200) {
83-
const index = this.courses.findIndex(c => c.course_id === course.course_id);
84-
this.courses.splice(index, 1);
85-
} else {
86-
throw response.data as ResponseError;
81+
const index = this.courses.findIndex(c => c.course_id === course.course_id);
82+
if (index >= 0) {
83+
const response = await api.delete(`courses/${course.course_id}`);
84+
if (response.status === 200) {
85+
this.courses.splice(index, 1);
86+
} else {
87+
throw response.data as ResponseError;
88+
}
8789
}
8890
}
8991
}

src/stores/problem_sets.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,18 @@ export const useProblemSetStore = defineStore('problem_sets', {
181181
* Delete the given ProblemSet from the database and the store.
182182
*/
183183
async deleteProblemSet(set: ProblemSet): Promise<void> {
184-
const response = await api.delete(`courses/${set.course_id}/sets/${set.set_id}`);
185-
if (response.status === 200) {
186-
const index = this.problem_sets.findIndex((s) => s.set_id === set.set_id);
187-
if (index < 0) {
188-
logger.error('[problem_set store/deleteProblemSet]: the problem set was not found in the store');
184+
const index = this.problem_sets.findIndex((s) => s.set_id === set.set_id);
185+
if (index >= 0) {
186+
const response = await api.delete(`courses/${set.course_id}/sets/${set.set_id}`);
187+
if (response.status === 200) {
188+
this.problem_sets.splice(index, 1);
189+
189190
} else {
190191
// splice is used so vue3 reacts to changes.
191-
this.problem_sets.splice(index, 1);
192+
logger.error(JSON.stringify(response));
192193
}
193194
} else {
194-
logger.error(JSON.stringify(response));
195+
logger.error('[problem_set store/deleteProblemSet]: the problem set was not found in the store');
195196
}
196197
},
197198
// UserSet actions
@@ -274,18 +275,19 @@ export const useProblemSetStore = defineStore('problem_sets', {
274275
*/
275276
async deleteUserSet(user_set: UserSet): Promise<void> {
276277
const course_id = useSessionStore().course.course_id;
277-
const response = await
278-
api.delete(`courses/${course_id}/sets/${user_set.set_id}/users/${user_set.course_user_id ?? 0}`);
279-
if (response.status === 200) {
280-
const index = this.db_user_sets.findIndex((set) => set.user_set_id === user_set.user_set_id);
281-
if (index < 0) {
282-
logger.error('[user store/deleteUserSet]: the user set was not found in the store');
283-
} else {
278+
const index = this.db_user_sets.findIndex((set) => set.user_set_id === user_set.user_set_id);
279+
if (index >= 0) {
280+
const response = await
281+
api.delete(`courses/${course_id}/sets/${user_set.set_id}/users/${user_set.course_user_id ?? 0}`);
282+
if (response.status === 200) {
284283
// splice is used so vue3 reacts to changes.
285284
this.db_user_sets.splice(index, 1);
285+
} else {
286+
logger.error(JSON.stringify(response));
286287
}
287-
} else {
288-
logger.error(JSON.stringify(response));
288+
}
289+
else {
290+
logger.error('[user store/deleteUserSet]: the user set was not found in the store');
289291
}
290292
},
291293

src/stores/set_problems.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,19 @@ export const useSetProblemStore = defineStore('set_problems', {
168168
*/
169169
async deleteSetProblem(problem: SetProblem): Promise<void> {
170170
const course_id = useSessionStore().course.course_id;
171-
172-
await api.delete(`courses/${course_id}/sets/${
173-
problem.set_id}/problems/${problem.set_problem_id}`);
174171
const index = this.set_problems.findIndex(prob => prob.set_problem_id === problem.set_problem_id);
175-
if (index < 0) {
176-
logger.error('[stores/set_problems/deleteSetProblem]: the set problem was not found in the store');
177-
} else {
172+
if (index >= 0) {
173+
174+
const response = await api.delete(`courses/${course_id}/sets/${problem.set_id
175+
}/problems/${problem.set_problem_id}`);
176+
if (response.status === 200) {
178177
// splice is used so vue3 reacts to changes.
179-
this.set_problems.splice(index, 1);
178+
this.set_problems.splice(index, 1);
179+
} else {
180+
logger.error(JSON.stringify(response));
181+
}
182+
} else {
183+
logger.error('[stores/set_problems/deleteSetProblem]: the set problem was not found in the store');
180184
}
181185
},
182186
// UserProblem actions
@@ -256,20 +260,22 @@ export const useSetProblemStore = defineStore('set_problems', {
256260
const course_id = useSessionStore().course.course_id;
257261
const set_problem = this.set_problems.find(prob => prob.set_problem_id === user_problem.set_problem_id);
258262
const problem_set_store = useProblemSetStore();
259-
const user_set = problem_set_store.findUserSet({ user_set_id: user_problem.user_set_id, });
260-
if (user_set == undefined) {
261-
throw 'deleteUserProblem: returned undefined user set';
262-
}
263-
await api.delete(`courses/${course_id}/sets/${set_problem?.set_id ?? 0
264-
}/users/${user_set.user_id}/problems/${user_problem.user_problem_id}`);
265-
266263
const index = this.db_user_problems
267264
.findIndex(user_problem => user_problem.user_problem_id === user_problem.user_problem_id);
268-
if (index < 0) {
269-
logger.error('[stores/set_problems/deleteUserProblem]: the set problem was not found in the store');
270-
} else {
265+
if (index >= 0) {
266+
const user_set = problem_set_store.findUserSet({ user_set_id: user_problem.user_set_id, });
267+
if (user_set == undefined) throw 'deleteUserProblem: returned undefined user set';
268+
269+
const response = await api.delete(`courses/${course_id}/sets/${set_problem?.set_id ?? 0
270+
}/users/${user_set?.user_id}/problems/${user_problem.user_problem_id}`);
271+
if (response.status === 200) {
271272
// splice is used so vue3 reacts to changes.
272-
this.set_problems.splice(index, 1);
273+
this.set_problems.splice(index, 1);
274+
} else {
275+
logger.error(JSON.stringify(response));
276+
}}
277+
else {
278+
logger.error('[stores/set_problems/deleteUserProblem]: the set problem was not found in the store');
273279
}
274280
}
275281
}

src/stores/users.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,18 @@ export const useUserStore = defineStore('user', {
156156
* Deletes the given User in the database and in the store.
157157
*/
158158
async deleteUser(user: User): Promise<void> {
159-
const session_store = useSessionStore();
160-
const course_id = session_store.course.course_id;
161-
const response = await api.delete(`courses/${course_id}/global-users/${user.user_id ?? 0}`);
162-
if (response.status === 200) {
163-
const index = this.users.findIndex((u) => u.user_id === user.user_id);
164-
if (index < 0) {
165-
logger.error('[user store/deleteUser]: the user was not found in the store');
166-
} else {
167-
// splice is used so vue3 reacts to changes.
159+
const course_id = useSessionStore().course.course_id;
160+
const index = this.users.findIndex((u) => u.user_id === user.user_id);
161+
if (index >= 0) {
162+
const response = await api.delete(`courses/${course_id}/global-users/${user.user_id ?? 0}`);
163+
if (response.status === 200) {
164+
// splice is used so vue3 reacts to changes.
168165
this.users.splice(index, 1);
166+
} else {
167+
logger.error(JSON.stringify(response));
169168
}
170169
} else {
171-
logger.error(JSON.stringify(response));
170+
logger.error('[user store/deleteUser]: the user was not found in the store');
172171
}
173172
},
174173

@@ -267,18 +266,17 @@ export const useUserStore = defineStore('user', {
267266
* Deletes a Course User from the store and the database.
268267
*/
269268
async deleteCourseUser(course_user: CourseUser): Promise<void> {
270-
const response = await api.delete(`courses/${course_user.course_id}/users/${course_user.user_id}`);
271-
if (response.status === 200) {
272-
const index = this.db_course_users.findIndex((u) => u.course_user_id === course_user.course_user_id);
273-
if (index < 0) {
274-
logger.error('[user store/deleteCourseUser]: the user was not found in the store');
275-
} else {
269+
const index = this.db_course_users.findIndex((u) => u.course_user_id === course_user.course_user_id);
270+
if (index >= 0) {
271+
const response = await api.delete(`courses/${course_user.course_id}/users/${course_user.user_id}`);
272+
if (response.status === 200) {
276273
// splice is used so vue3 reacts to changes.
277274
this.db_course_users.splice(index, 1);
275+
} else {
276+
logger.error(JSON.stringify(response));
278277
}
279-
} else if (response.status === 250) {
280-
logger.error(response.data);
281-
throw response.data as ResponseError;
278+
} else {
279+
logger.error('[user store/deleteCourseUser]: the user was not found in the store');
282280
}
283281
},
284282
clearAll() {

tests/stores/set_problems.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import { Dictionary, generic } from 'src/common/models';
2929

3030
import { loadCSV, cleanIDs } from '../utils';
3131
import { checkPassword } from 'src/common/api-requests/session';
32-
import { logger } from 'src/boot/logger';
3332

3433
const app = createApp({});
3534

0 commit comments

Comments
 (0)