174 lines
6.0 KiB
JavaScript
174 lines
6.0 KiB
JavaScript
const API_URL = 'http://localhost:3000';
|
|
|
|
class Model {
|
|
constructor() {
|
|
this.API_URL = API_URL;
|
|
}
|
|
|
|
async fetchAlbums() {
|
|
const response = await fetch(`${this.API_URL}/albums`);
|
|
if (!response.ok) throw new Error('Failed to fetch albums');
|
|
return response.json();
|
|
}
|
|
|
|
async fetchPhotos() {
|
|
const response = await fetch(`${this.API_URL}/photos`);
|
|
if (!response.ok) throw new Error('Failed to fetch photos');
|
|
return response.json();
|
|
}
|
|
|
|
async fetchFiles() {
|
|
const response = await fetch(`${this.API_URL}/files`);
|
|
if (!response.ok) throw new Error('Failed to fetch files');
|
|
return response.json();
|
|
}
|
|
|
|
async fetchBasket() {
|
|
const response = await fetch(`${this.API_URL}/basket`);
|
|
if (!response.ok) throw new Error('Failed to fetch basket');
|
|
return response.json();
|
|
}
|
|
|
|
async createAlbum(name, src, description, categoryId, visibility, owner, createdAt, tags) {
|
|
const response = await fetch(`${this.API_URL}/albums`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ name, src, description, categoryId, visibility, owner, createdAt, tags }),
|
|
});
|
|
if (!response.ok) throw new Error('Failed to create album');
|
|
return response.json();
|
|
}
|
|
|
|
async createPhoto(src, description, categoryId, photoTypeId, resolution, size, createdAt, tags) {
|
|
const response = await fetch(`${this.API_URL}/photos`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ src, description, categoryId, photoTypeId, resolution, size, createdAt, tags }),
|
|
});
|
|
if (!response.ok) throw new Error('Failed to create photo');
|
|
return response.json();
|
|
}
|
|
|
|
async createFile(src, name, description, categoryId, fileTypeId, size, format, createdAt, tags) {
|
|
const response = await fetch(`${this.API_URL}/files`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ src, name, description, categoryId, fileTypeId, size, format, createdAt, tags }),
|
|
});
|
|
if (!response.ok) throw new Error('Failed to create file');
|
|
return response.json();
|
|
}
|
|
|
|
async updateAlbum(id, name, description, categoryId, visibility, owner, createdAt, tags) {
|
|
const response = await fetch(`${this.API_URL}/albums/${id}`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ name, description, categoryId, visibility, owner, createdAt, tags }),
|
|
});
|
|
if (!response.ok) throw new Error('Failed to update album');
|
|
return response.json();
|
|
}
|
|
|
|
async updatePhoto(id, description, categoryId, photoTypeId, resolution, size, createdAt, tags) {
|
|
const response = await fetch(`${this.API_URL}/photos/${id}`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ description, categoryId, photoTypeId, resolution, size, createdAt, tags }),
|
|
});
|
|
if (!response.ok) throw new Error('Failed to update photo');
|
|
return response.json();
|
|
}
|
|
|
|
async updateFile(id, name, description, categoryId, fileTypeId, size, format, createdAt, tags) {
|
|
const response = await fetch(`${this.API_URL}/files/${id}`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ name, description, categoryId, fileTypeId, size, format, createdAt, tags }),
|
|
});
|
|
if (!response.ok) throw new Error('Failed to update file');
|
|
return response.json();
|
|
}
|
|
|
|
async moveToBasket(item, type) {
|
|
const basketItem = { ...item, type };
|
|
delete basketItem.id;
|
|
console.log(`Saving to basket:`, basketItem);
|
|
const response = await fetch(`${this.API_URL}/basket`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(basketItem),
|
|
});
|
|
if (!response.ok) throw new Error(`Failed to move to basket: ${response.statusText}`);
|
|
return response.json();
|
|
}
|
|
|
|
async restoreFromBasket(id, type) {
|
|
const basketItem = await this.getBasketItem(id);
|
|
console.log(`Restoring from basket:`, basketItem);
|
|
const response = await fetch(`${this.API_URL}/${type}s`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
name: basketItem.name,
|
|
src: basketItem.src,
|
|
description: basketItem.description,
|
|
categoryId: basketItem.categoryId,
|
|
visibility: basketItem.visibility,
|
|
owner: basketItem.owner,
|
|
createdAt: basketItem.createdAt,
|
|
tags: basketItem.tags,
|
|
photoTypeId: basketItem.photoTypeId,
|
|
fileTypeId: basketItem.fileTypeId,
|
|
resolution: basketItem.resolution,
|
|
size: basketItem.size,
|
|
format: basketItem.format
|
|
}),
|
|
});
|
|
if (!response.ok) throw new Error('Failed to restore item');
|
|
await this.deleteFromBasket(id);
|
|
return response.json();
|
|
}
|
|
|
|
async deleteFromBasket(id) {
|
|
const response = await fetch(`${this.API_URL}/basket/${id}`, {
|
|
method: 'DELETE',
|
|
});
|
|
if (!response.ok) throw new Error('Failed to delete from basket');
|
|
}
|
|
|
|
async clearBasket() {
|
|
const basket = await this.fetchBasket();
|
|
await Promise.all(
|
|
basket.map(item => fetch(`${this.API_URL}/basket/${item.id}`, { method: 'DELETE' }))
|
|
);
|
|
}
|
|
|
|
async getBasketItem(id) {
|
|
const response = await fetch(`${this.API_URL}/basket/${id}`);
|
|
if (!response.ok) throw new Error('Failed to fetch basket item');
|
|
return response.json();
|
|
}
|
|
|
|
async deleteAlbum(id) {
|
|
const response = await fetch(`${this.API_URL}/albums/${id}`, {
|
|
method: 'DELETE',
|
|
});
|
|
if (!response.ok) throw new Error('Failed to delete album');
|
|
}
|
|
|
|
async deletePhoto(id) {
|
|
const response = await fetch(`${this.API_URL}/photos/${id}`, {
|
|
method: 'DELETE',
|
|
});
|
|
if (!response.ok) throw new Error('Failed to delete photo');
|
|
}
|
|
|
|
async deleteFile(id) {
|
|
const response = await fetch(`${this.API_URL}/files/${id}`, {
|
|
method: 'DELETE',
|
|
});
|
|
if (!response.ok) throw new Error('Failed to delete file');
|
|
}
|
|
}
|
|
|
|
export default Model; |