Files
GNX-WEB/frontEnd/lib/api/caseStudyService.ts
Iliyan Angelov 136f75a859 update
2025-11-24 08:18:18 +02:00

365 lines
8.4 KiB
TypeScript

import { API_CONFIG } from '../config/api';
// Types for Case Study API
export interface CaseStudyCategory {
id: number;
name: string;
slug: string;
description?: string;
display_order: number;
case_studies_count?: number;
}
export interface Client {
id: number;
name: string;
slug: string;
logo?: string;
description?: string;
website?: string;
}
export interface CaseStudyImage {
id: number;
image: string;
caption?: string;
display_order: number;
}
export interface CaseStudyProcess {
id: number;
title: string;
description: string;
step_number: number;
}
export interface CaseStudy {
id: number;
title: string;
slug: string;
subtitle?: string;
description?: string;
excerpt: string;
thumbnail?: string;
featured_image?: string;
poster_image?: string;
project_image?: string;
project_overview?: string;
site_map_content?: string;
category?: CaseStudyCategory;
category_name?: string;
category_slug?: string;
client?: Client;
client_name?: string;
gallery_images?: CaseStudyImage[];
process_steps?: CaseStudyProcess[];
meta_description?: string;
meta_keywords?: string;
published: boolean;
featured: boolean;
views_count: number;
display_order: number;
published_at: string;
created_at: string;
updated_at: string;
related_case_studies?: CaseStudy[];
}
export interface CaseStudyListResponse {
count: number;
next: string | null;
previous: string | null;
results: CaseStudy[];
}
// Helper function to build query string
const buildQueryString = (params: Record<string, any>): string => {
const searchParams = new URLSearchParams();
Object.entries(params).forEach(([key, value]) => {
if (value !== undefined && value !== null && value !== '') {
searchParams.append(key, value.toString());
}
});
return searchParams.toString();
};
// Case Study API functions
export const caseStudyService = {
// Get all case studies with optional filtering
getCaseStudies: async (params?: {
category?: string;
client?: string;
search?: string;
featured?: boolean;
ordering?: string;
page?: number;
page_size?: number;
}): Promise<CaseStudyListResponse> => {
try {
const queryString = params ? buildQueryString(params) : '';
const url = queryString
? `${API_CONFIG.BASE_URL}/api/case-studies/?${queryString}`
: `${API_CONFIG.BASE_URL}/api/case-studies/`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
throw error;
}
},
// Get a single case study by slug
getCaseStudyBySlug: async (slug: string): Promise<CaseStudy> => {
try {
const response = await fetch(
`${API_CONFIG.BASE_URL}/api/case-studies/${slug}/`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
throw error;
}
},
// Get featured case studies
getFeaturedCaseStudies: async (): Promise<CaseStudy[]> => {
try {
const response = await fetch(
`${API_CONFIG.BASE_URL}/api/case-studies/featured/`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
throw error;
}
},
// Get latest case studies
getLatestCaseStudies: async (limit: number = 6): Promise<CaseStudy[]> => {
try {
const response = await fetch(
`${API_CONFIG.BASE_URL}/api/case-studies/latest/?limit=${limit}`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
throw error;
}
},
// Get popular case studies
getPopularCaseStudies: async (limit: number = 6): Promise<CaseStudy[]> => {
try {
const response = await fetch(
`${API_CONFIG.BASE_URL}/api/case-studies/popular/?limit=${limit}`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
throw error;
}
},
// Get related case studies for a specific case study
getRelatedCaseStudies: async (slug: string): Promise<CaseStudy[]> => {
try {
const response = await fetch(
`${API_CONFIG.BASE_URL}/api/case-studies/${slug}/related/`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
throw error;
}
},
// Get all categories
getCategories: async (): Promise<CaseStudyCategory[]> => {
try {
const response = await fetch(
`${API_CONFIG.BASE_URL}/api/case-studies/categories/`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
throw error;
}
},
// Get categories with case studies
getCategoriesWithCaseStudies: async (): Promise<CaseStudyCategory[]> => {
try {
const response = await fetch(
`${API_CONFIG.BASE_URL}/api/case-studies/categories/with_case_studies/`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
throw error;
}
},
// Get all clients
getClients: async (): Promise<Client[]> => {
try {
const response = await fetch(
`${API_CONFIG.BASE_URL}/api/case-studies/clients/`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
throw error;
}
},
// Get a client by slug
getClientBySlug: async (slug: string): Promise<Client> => {
try {
const response = await fetch(
`${API_CONFIG.BASE_URL}/api/case-studies/clients/${slug}/`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
throw error;
}
},
// Get case studies for a specific client
getClientCaseStudies: async (slug: string): Promise<CaseStudyListResponse> => {
try {
const response = await fetch(
`${API_CONFIG.BASE_URL}/api/case-studies/clients/${slug}/case_studies/`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
}
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
throw error;
}
},
};
export default caseStudyService;