This commit is contained in:
LivelyPuer
2025-05-06 20:44:13 +04:00
parent bd75372ea2
commit e3a1b6f03c
32 changed files with 3643 additions and 2907 deletions

View File

@@ -0,0 +1,82 @@
const API_URL = 'http://localhost:3000';
export default class MovieService {
static async getMovies() {
try {
const response = await fetch(`${API_URL}/movies`);
if (!response.ok) {
throw new Error('Failed to fetch movies');
}
return await response.json();
} catch (error) {
console.error('Error fetching movies:', error);
return [];
}
}
static async getMovieById(id) {
try {
const response = await fetch(`${API_URL}/movies/${id}`);
if (!response.ok) {
throw new Error('Failed to fetch movie');
}
return await response.json();
} catch (error) {
console.error('Error fetching movie:', error);
return null;
}
}
static async addMovie(movie) {
try {
const response = await fetch(`${API_URL}/movies`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(movie)
});
if (!response.ok) {
throw new Error('Failed to add movie');
}
return await response.json();
} catch (error) {
console.error('Error adding movie:', error);
throw error;
}
}
static async updateMovie(id, movie) {
try {
const response = await fetch(`${API_URL}/movies/${id}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(movie)
});
if (!response.ok) {
throw new Error('Failed to update movie');
}
return await response.json();
} catch (error) {
console.error('Error updating movie:', error);
throw error;
}
}
static async deleteMovie(id) {
try {
const response = await fetch(`${API_URL}/movies/${id}`, {
method: 'DELETE'
});
if (!response.ok) {
throw new Error('Failed to delete movie');
}
return true;
} catch (error) {
console.error('Error deleting movie:', error);
throw error;
}
}
}