diff --git a/src/api/applicationApi.ts b/src/api/applicationApi.ts index a53bc455..f090e205 100644 --- a/src/api/applicationApi.ts +++ b/src/api/applicationApi.ts @@ -1,7 +1,13 @@ +import api from './api'; +import { AxiosError } from 'axios'; import type { NoticeInfo } from './alertApi'; import type { ShopInfo } from './shopApi'; import type { UserProfileItem } from './userApi'; +interface ErrorMessage { + message: string; +} + export interface LinkInfo { rel: string; description: string; @@ -63,3 +69,95 @@ export interface ApplicationNoticeResponse { export interface ApplicationRequest { status: 'accepted' | 'rejected' | 'canceled'; } + +// GET /shops/{shop_id}/notices/{notice_id}/applications - 가게의 특정 공고의 지원 목록 조회 +export const getNoticeApplications = async ( + shopId: string, + noticeId: string, + query?: { offset?: number; limit?: number }, +): Promise => { + try { + const newQuery = new URLSearchParams({ + offset: String(query?.offset ?? ''), + limit: String(query?.limit ?? ''), + }); + const response = await api.get( + `/shops/${shopId}/notices/${noticeId}/applications?${newQuery}`, + ); + return response.data; + } catch (error) { + const axiosError = error as AxiosError; // 에러 타입 명시 + if (axiosError.response) { + throw new Error(axiosError.response.data.message); + } else { + throw new Error('서버에 연결할 수 없습니다. 인터넷 연결을 확인해주세요.'); + } + } +}; + +// POST /shops/{shop_id}/notices/{notice_id}/applications - 가게의 특정 공고 지원 등록 +export const postNoticeApplications = async ( + shopId: string, + noticeId: string, +): Promise => { + try { + const response = await api.post( + `/shops/${shopId}/notices/${noticeId}/applications`, + ); + return response.data; + } catch (error) { + const axiosError = error as AxiosError; // 에러 타입 명시 + if (axiosError.response) { + throw new Error(axiosError.response.data.message); + } else { + throw new Error('서버에 연결할 수 없습니다. 인터넷 연결을 확인해주세요.'); + } + } +}; + +// PUT /shops/{shop_id}/notices/{notice_id}/applications/{application_id} - 가게의 특정 공고 지원 승인, 거절 또는 취소 +export const putNoticeApplications = async ( + shopId: string, + noticeId: string, + applicationId: string, + body: ApplicationRequest, +): Promise => { + try { + const response = await api.put( + `/shops/${shopId}/notices/${noticeId}/applications/${applicationId}`, + body, + ); + return response.data; + } catch (error) { + const axiosError = error as AxiosError; // 에러 타입 명시 + if (axiosError.response) { + throw new Error(axiosError.response.data.message); + } else { + throw new Error('서버에 연결할 수 없습니다. 인터넷 연결을 확인해주세요.'); + } + } +}; + +// GET /users/{user_id}/applications - 유저의 지원 목록 조회 +export const getUserApplications = async ( + userId: string, + query?: { offset?: number; limit?: number }, +): Promise => { + try { + const newQuery = new URLSearchParams({ + offset: String(query?.offset ?? ''), + limit: String(query?.limit ?? ''), + }); + const response = await api.get( + `/users/${userId}/applications?${newQuery}`, + ); + return response.data; + } catch (error) { + const axiosError = error as AxiosError; // 에러 타입 명시 + if (axiosError.response) { + throw new Error(axiosError.response.data.message); + } else { + throw new Error('서버에 연결할 수 없습니다. 인터넷 연결을 확인해주세요.'); + } + } +};