requests for auth/register
This commit is contained in:
parent
9a3f05ef60
commit
bc293f6370
9 changed files with 348 additions and 63 deletions
117
SurveyFrontend/src/api/SurveyApi.ts
Normal file
117
SurveyFrontend/src/api/SurveyApi.ts
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
import {BASE_URL, createRequestConfig, handleResponse} from "./BaseApi.ts";
|
||||
|
||||
export interface ISurvey {
|
||||
id: number;
|
||||
title: string;
|
||||
description: string;
|
||||
createdBy: number;
|
||||
}
|
||||
|
||||
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) {
|
||||
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) {
|
||||
console.error("Error receiving surveys:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* postNewSurvey - добавление нового опроса
|
||||
* @param survey
|
||||
*/
|
||||
export const postNewSurvey = async (survey: INewSurvey): Promise<INewSurvey> => {
|
||||
const token = localStorage.getItem("token");
|
||||
if (!token) {
|
||||
throw new Error("Токен отсутствует");
|
||||
}
|
||||
|
||||
try{
|
||||
const response = await fetch(`${BASE_URL}/surveys`, {
|
||||
...createRequestConfig('POST'),
|
||||
body: JSON.stringify(survey)
|
||||
})
|
||||
|
||||
// return await handleResponse(response);
|
||||
|
||||
if (response.status === 201) {
|
||||
return await handleResponse(response);
|
||||
}
|
||||
throw new Error(`Ожидался код 201, получен ${response.status}`);
|
||||
} catch (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){
|
||||
console.error(`Error finding the survey by id: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Запрос на удаление опроса
|
||||
* @param surveyId - ID выбранного опроса
|
||||
*/
|
||||
export const deleteSurvey = async (surveyId: string) => {
|
||||
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){
|
||||
console.error(`Error deleting a survey: ${error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue