Skip to content

Commit 58f9f6b

Browse files
4rthurCaiA-lexisL
authored andcommitted
fix: update jsons for api usage
1 parent f95b6da commit 58f9f6b

3 files changed

Lines changed: 27 additions & 17 deletions

File tree

src/composables/useCourses.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ref, reactive } from "vue";
2+
import { apiFetch } from "../utils/api";
23

34
export function useCourses() {
45
const courses = ref([]);
@@ -27,7 +28,7 @@ export function useCourses() {
2728

2829
const fetchDepartments = async () => {
2930
try {
30-
const response = await fetch("/api/departments/");
31+
const response = await apiFetch("/api/departments/");
3132
if (!response.ok) throw new Error("Failed to fetch departments");
3233
departments.value = await response.json();
3334
} catch (e) {
@@ -49,7 +50,7 @@ export function useCourses() {
4950
params.append("page", pagination.current_page);
5051

5152
try {
52-
const response = await fetch(`/api/courses/?${params.toString()}`);
53+
const response = await apiFetch(`/api/courses/?${params.toString()}`);
5354
if (!response.ok) {
5455
const errorData = await response
5556
.json()
@@ -59,11 +60,11 @@ export function useCourses() {
5960
);
6061
}
6162
const data = await response.json();
62-
courses.value = data.courses;
63-
pagination.current_page = data.pagination.current_page;
64-
pagination.total_pages = data.pagination.total_pages;
65-
pagination.total_courses = data.pagination.total_courses;
66-
pagination.limit = data.pagination.limit;
63+
// DRF 标准分页格式: { count, next, previous, results }
64+
courses.value = data.results || [];
65+
const totalCount = data.count || 0;
66+
pagination.total_courses = totalCount;
67+
pagination.total_pages = Math.ceil(totalCount / pagination.limit) || 1;
6768
} catch (e) {
6869
console.error("useCourses: Error fetching courses:", e);
6970
error.value = e.message;

src/composables/useReviews.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ref } from "vue";
22
import { getCookie } from "../utils/cookies";
3+
import { apiFetch } from "../utils/api";
34

45
export function useReviews() {
56
const loading = ref(false);
@@ -8,7 +9,9 @@ export function useReviews() {
89
const fetchUserReview = async (courseId) => {
910
if (!courseId) return null;
1011
try {
11-
const response = await fetch(`/api/course/${courseId}/my-review/`);
12+
const response = await apiFetch(
13+
`/api/courses/${courseId}/reviews/?author=me`,
14+
);
1215
if (response.ok) {
1316
const data = await response.json();
1417
return Array.isArray(data) ? data[0] : data;
@@ -29,7 +32,7 @@ export function useReviews() {
2932

3033
const submitReview = async (courseId, newReview) => {
3134
try {
32-
const response = await fetch(`/api/course/${courseId}/`, {
35+
const response = await apiFetch(`/api/courses/${courseId}/reviews/`, {
3336
method: "POST",
3437
headers: {
3538
"Content-Type": "application/json",
@@ -48,17 +51,17 @@ export function useReviews() {
4851
}
4952
};
5053

51-
const deleteReview = async (courseId) => {
54+
const deleteReview = async (reviewId) => {
5255
try {
53-
const response = await fetch(`/api/course/${courseId}/review/`, {
56+
const response = await apiFetch(`/api/reviews/${reviewId}/`, {
5457
method: "DELETE",
5558
headers: { "X-CSRFToken": getCookie("csrftoken") },
5659
});
57-
if (!response.ok) {
60+
if (!response.ok && response.status !== 204) {
5861
const errorData = await response.json().catch(() => null);
5962
throw new Error(errorData?.detail || "Failed to delete review");
6063
}
61-
return await response.json();
64+
return true;
6265
} catch (e) {
6366
console.error("useReviews: deleteReview error", e);
6467
throw e;
@@ -67,7 +70,7 @@ export function useReviews() {
6770

6871
const vote = async (courseId, value, forLayup) => {
6972
try {
70-
const response = await fetch(`/api/course/${courseId}/vote/`, {
73+
const response = await apiFetch(`/api/courses/${courseId}/vote/`, {
7174
method: "POST",
7275
headers: {
7376
"Content-Type": "application/json",
@@ -85,7 +88,7 @@ export function useReviews() {
8588

8689
const voteOnReview = async (reviewId, isKudos) => {
8790
try {
88-
const response = await fetch(`/api/review/${reviewId}/vote/`, {
91+
const response = await apiFetch(`/api/reviews/${reviewId}/vote/`, {
8992
method: "POST",
9093
headers: {
9194
"Content-Type": "application/json",

src/utils/api.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
// Lightweight API utilities used across components
22
// checkAuthentication: returns a boolean indicating auth status
3+
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || "";
4+
5+
export async function apiFetch(path, options = {}) {
6+
return fetch(`${API_BASE_URL}${path}`, options);
7+
}
8+
39
export async function checkAuthentication() {
410
try {
5-
const response = await fetch("/api/user/status/");
11+
const response = await apiFetch("/api/user/status/");
612
if (response.ok) {
713
const data = await response.json();
814
return !!data.isAuthenticated;
@@ -14,4 +20,4 @@ export async function checkAuthentication() {
1420
}
1521
}
1622

17-
export default { checkAuthentication };
23+
export default { apiFetch, checkAuthentication };

0 commit comments

Comments
 (0)