searchHistoryService.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { SearchHistoryItem, SearchHistoryDetail } from '../types';
  2. const API_BASE = '/api';
  3. export const searchHistoryService = {
  4. // 検索履歴リストの取得
  5. async getHistories(page: number = 1, limit: number = 20): Promise<{
  6. histories: SearchHistoryItem[];
  7. total: number;
  8. page: number;
  9. limit: number;
  10. }> {
  11. const response = await fetch(`${API_BASE}/search-history?page=${page}&limit=${limit}`, {
  12. headers: {
  13. 'Authorization': `Bearer ${localStorage.getItem('authToken')}`,
  14. },
  15. });
  16. if (!response.ok) throw new Error('Failed to fetch search histories');
  17. return response.json();
  18. },
  19. // 検索履歴詳細の取得
  20. async getHistoryDetail(id: string): Promise<SearchHistoryDetail> {
  21. const response = await fetch(`${API_BASE}/search-history/${id}`, {
  22. headers: {
  23. 'Authorization': `Bearer ${localStorage.getItem('authToken')}`,
  24. },
  25. });
  26. if (!response.ok) throw new Error('Failed to fetch search history detail');
  27. return response.json();
  28. },
  29. // 検索履歴の作成
  30. async createHistory(title: string, selectedGroups?: string[]): Promise<{ id: string }> {
  31. const response = await fetch(`${API_BASE}/search-history`, {
  32. method: 'POST',
  33. headers: {
  34. 'Content-Type': 'application/json',
  35. 'Authorization': `Bearer ${localStorage.getItem('authToken')}`,
  36. },
  37. body: JSON.stringify({ title, selectedGroups }),
  38. });
  39. if (!response.ok) throw new Error('Failed to create search history');
  40. return response.json();
  41. },
  42. // 検索履歴の削除
  43. async deleteHistory(id: string): Promise<void> {
  44. const response = await fetch(`${API_BASE}/search-history/${id}`, {
  45. method: 'DELETE',
  46. headers: {
  47. 'Authorization': `Bearer ${localStorage.getItem('authToken')}`,
  48. },
  49. });
  50. if (!response.ok) throw new Error('Failed to delete search history');
  51. },
  52. };