All files / lib/api/services/evenementSports lieuService.ts

100% Statements 27/27
100% Branches 5/5
100% Functions 12/12
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 1026x                                       6x 6x             5x   5x 3x 3x 3x 3x   3x 3x       5x               2x               2x                     2x                     2x                   3x       6x 1x 1x 1x 1x 1x 1x    
import { fetchApi } from '@/lib/api/core/fetchWrappers';
import { Lieu } from '@/types/sportEvenement/lieu';
 
// Types pour les requêtes
export interface CreateLieuRequest {
  nom: string;
}
 
export interface UpdateLieuRequest {
  id: number;
  nom: string;
}
 
export interface LieuFilters {
  nom?: string;
  page?: number;
  limit?: number;
}
 
// Service pour la gestion des lieux
export class LieuService {
  private static readonly BASE_PATH = '/lieu';
 
  /**
   * Récupère tous les lieux
   * Route Django: path('lieu/', LieuListView.as_view(), name='lieu-list')
   */
  static async getAllLieux(filters?: LieuFilters): Promise<Lieu[]> {
    let url = `${this.BASE_PATH}/`;
    
    if (filters) {
      const params = new URLSearchParams();
      if (filters.nom) params.append('search', filters.nom);
      if (filters.page) params.append('page', filters.page.toString());
      if (filters.limit) params.append('limit', filters.limit.toString());
      
      if (params.toString()) {
        url += `?${params.toString()}`;
      }
    }
    
    return fetchApi<Lieu[]>(url);
  }
 
  /**
   * Récupère un lieu par son ID
   * Route Django: path('lieu/<int:pk>/', LieuDetailView.as_view(), name='lieu-detail')
   */
  static async getLieuById(id: number): Promise<Lieu> {
    return fetchApi<Lieu>(`${this.BASE_PATH}/${id}/`);
  }
 
  /**
   * Crée un nouveau lieu
   * Route Django: path('lieu/create/', LieuCreateView.as_view(), name='lieu-create')
   */
  static async createLieu(data: CreateLieuRequest): Promise<Lieu> {
    return fetchApi<Lieu>(`${this.BASE_PATH}/create/`, {
      method: 'POST',
      body: JSON.stringify(data),
    });
  }
 
  /**
   * Met à jour un lieu existant
   * Route Django: path('lieu/update/<int:pk>/', LieuUpdateView.as_view(), name='lieu-update')
   */
  static async updateLieu(data: UpdateLieuRequest): Promise<Lieu> {
    return fetchApi<Lieu>(`${this.BASE_PATH}/update/${data.id}/`, {
      method: 'PUT',
      body: JSON.stringify(data),
    });
  }
 
  /**
   * Supprime un lieu
   * Route Django: path('lieu/delete/<int:pk>/', LieuDeleteView.as_view(), name='lieu-delete')
   */
  static async deleteLieu(id: number): Promise<void> {
    return fetchApi<void>(`${this.BASE_PATH}/delete/${id}/`, {
      method: 'DELETE',
    });
  }
 
  /**
   * Recherche des lieux par nom (si implémentée côté backend)
   * Note: Cette route n'existe pas dans les routes Django fournies
   */
  static async searchLieux(query: string): Promise<Lieu[]> {
    return fetchApi<Lieu[]>(`${this.BASE_PATH}/?search=${encodeURIComponent(query)}`);
  }
}
 
export const lieuApi = {
  getAll: (filters?: LieuFilters) => LieuService.getAllLieux(filters),
  getById: (id: number) => LieuService.getLieuById(id),
  create: (data: CreateLieuRequest) => LieuService.createLieu(data),
  update: (data: UpdateLieuRequest) => LieuService.updateLieu(data),
  delete: (id: number) => LieuService.deleteLieu(id),
  search: (query: string) => LieuService.searchLieux(query),
};