lab5
This commit is contained in:
82
src/services/MovieService.js
Normal file
82
src/services/MovieService.js
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user