153 lines
No EOL
4.4 KiB
TypeScript
153 lines
No EOL
4.4 KiB
TypeScript
import {BASE_URL, createRequestConfig, handleResponse, handleUnauthorizedError} from "./BaseApi.ts";
|
|
|
|
export interface ISurvey {
|
|
id: number;
|
|
title: string;
|
|
description: string;
|
|
createdBy: number;
|
|
createdAt: string;
|
|
}
|
|
|
|
export interface INewSurvey{
|
|
title: string;
|
|
description: string;
|
|
}
|
|
|
|
/**
|
|
* getMySurveys - запрос на получение моих опросов
|
|
*/
|
|
export const getMySurveys = async (): Promise<ISurvey[]> => {
|
|
const token = localStorage.getItem("token");
|
|
if (!token) {
|
|
throw new Error("Токен отсутствует");
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`${BASE_URL}/surveys/my`, {
|
|
...createRequestConfig('GET'),
|
|
});
|
|
return await handleResponse(response);
|
|
} catch (error) {
|
|
handleUnauthorizedError(error);
|
|
console.error("Error receiving surveys:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* getAllSurvey - запрос на получение всех опросов
|
|
*/
|
|
export const getAllSurveys = async (): Promise<ISurvey[]> => {
|
|
try{
|
|
const response = await fetch(`${BASE_URL}/surveys`, {
|
|
...createRequestConfig('GET'),
|
|
})
|
|
return await handleResponse(response);
|
|
} catch (error) {
|
|
handleUnauthorizedError(error);
|
|
console.error("Error receiving surveys:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* postNewSurvey - добавление нового опроса
|
|
* @param survey
|
|
*/
|
|
export const postNewSurvey = async (survey: INewSurvey): Promise<ISurvey> => {
|
|
const token = localStorage.getItem("token");
|
|
if (!token) {
|
|
throw new Error("Токен отсутствует");
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`${BASE_URL}/surveys`, {
|
|
...createRequestConfig('POST'),
|
|
body: JSON.stringify(survey),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Ошибка: ${response.status}`);
|
|
}
|
|
|
|
const data = await handleResponse(response);
|
|
if (!data.id) {
|
|
throw new Error("Сервер не вернул ID опроса");
|
|
}
|
|
return data;
|
|
} catch (error) {
|
|
handleUnauthorizedError(error);
|
|
console.error(`Error when adding a new survey: ${error}`);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Запрос на получение опроса по заданному ID
|
|
* @param surveyId - ID опроса
|
|
*/
|
|
export const getSurveyById = async (surveyId: number): Promise<ISurvey> => {
|
|
try{
|
|
const response = await fetch(`${BASE_URL}/surveys/${surveyId}`, {
|
|
...createRequestConfig('GET'),
|
|
})
|
|
return await handleResponse(response);
|
|
} catch (error){
|
|
handleUnauthorizedError(error);
|
|
console.error(`Error finding the survey by id: ${error}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Запрос на удаление опроса
|
|
* @param surveyId - ID выбранного опроса
|
|
*/
|
|
export const deleteSurvey = async (surveyId: number) => {
|
|
const token = localStorage.getItem("token");
|
|
if (!token) {
|
|
throw new Error("Токен отсутствует");
|
|
}
|
|
|
|
try{
|
|
const response = await fetch(`${BASE_URL}/surveys/${surveyId}`, {
|
|
...createRequestConfig('DELETE'),
|
|
})
|
|
const responseData = await handleResponse(response);
|
|
if (response.ok && !responseData){
|
|
return {success: true};
|
|
}
|
|
return responseData;
|
|
} catch (error){
|
|
handleUnauthorizedError(error);
|
|
console.error(`Error deleting a survey: ${error}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Запрос на изменение опроса целиком
|
|
* @param surveyId
|
|
* @param survey
|
|
*/
|
|
export const updateSurvey = async (surveyId: number, survey: Partial<INewSurvey>): Promise<ISurvey> => {
|
|
const token = localStorage.getItem("token");
|
|
if (!token) {
|
|
throw new Error('Токен отсутствует');
|
|
}
|
|
try{
|
|
const response = await fetch(`${BASE_URL}/surveys/${surveyId}`, {
|
|
...createRequestConfig('PUT'),
|
|
body: JSON.stringify({
|
|
title: survey.title,
|
|
description: survey.description,
|
|
})
|
|
})
|
|
return await handleResponse(response);
|
|
}
|
|
catch (error){
|
|
handleUnauthorizedError(error);
|
|
console.error(`Error updating survey: ${error}`);
|
|
throw error;
|
|
}
|
|
} |