Skip to content

Commit

Permalink
feat(time): add fetch function getLectureList / getMajorList & add en…
Browse files Browse the repository at this point in the history
…dpoint constant (#97)
  • Loading branch information
SWARVY committed Aug 17, 2024
1 parent 7ef2e26 commit b216950
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
6 changes: 6 additions & 0 deletions apps/time/src/widgets/time-table/api/endpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const TIMETABLE_ENDPOINT = {
getLectureList: 'api/v1/lecture/retrieve',
getMajorList: 'api/v1/lecture/retrieve/major',
};

export default TIMETABLE_ENDPOINT;
75 changes: 75 additions & 0 deletions apps/time/src/widgets/time-table/api/getLectureList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { DayKor } from '@/shared/types';
import type {
DayCampus,
DayPeriod,
Grade,
LectureKey,
NightCampus,
NightPeriod,
} from '@/widgets/time-table';
import { TIMETABLE_ENDPOINT } from '@/widgets/time-table';

export interface GetLectureListParams {
campus: (DayCampus | NightCampus)[];
type: LectureKey[];
grade: Grade[];
day: DayKor[];
time: (DayPeriod | NightPeriod)[];
major: string[];
lectureName: string;
cursor: number;
limit: number;
}

export interface GetLectureListReponseValue {
id: number;
campus: string;
category: string;
code: string;
credit: 0;
grade: 0;
groupName: string;
isExceeded: true;
major: string;
name: string;
professor: string;
room: string;
year: 0;
semester: string;
time: string;
type: LectureKey;
}

export interface GetLectureListResponse {
success: boolean;
data: {
values: GetLectureListReponseValue[];
};
hasPrevious: boolean;
hasNext: boolean;
}

export async function getLectureList({
...params
}: GetLectureListParams): Promise<GetLectureListResponse> {
const apiURL = new URL(
`${process.env.NEXT_PUBLIC_SERVER_URL}/${TIMETABLE_ENDPOINT.getLectureList}`,
);

Object.entries(params).forEach(([key, value]) => {
if (value) {
apiURL.searchParams.set(
key,
Array.isArray(value) ? value.join(',') : String(value),
);
}
});

const res = await fetch(apiURL);

if (!res.ok) {
throw new Error('강의 정보 불러오기에 실패했습니다.');
}

return res.json();
}
28 changes: 28 additions & 0 deletions apps/time/src/widgets/time-table/api/getMajorList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { TIMETABLE_ENDPOINT } from '@/widgets/time-table';

export interface GetMajorListParams {
major: string;
}

export interface GetMajorListResponse {
success: boolean;
data: string[];
}

export async function getMajorList({
major,
}: GetMajorListParams): Promise<GetMajorListResponse> {
const apiURL = new URL(
`${process.env.NEXT_PUBLIC_SERVER_URL}/${TIMETABLE_ENDPOINT.getMajorList}`,
);

apiURL.searchParams.set('major', major);

const res = await fetch(apiURL);

if (!res.ok) {
throw new Error('전공 리스트 불러오기에 실패했습니다.');
}

return res.json();
}

0 comments on commit b216950

Please sign in to comment.