2025-10-18 14:10:21 +03:00
|
|
|
import { apiClient } from '@/services/apiClient.js'
|
2025-10-26 21:32:35 +03:00
|
|
|
import { useAuthStore } from '@/stores/authStore.js'
|
|
|
|
|
import { IMAGE_STORAGE_URL } from '@/main.js'
|
2025-10-18 14:10:21 +03:00
|
|
|
|
|
|
|
|
export const useMerchImagesApi = () => {
|
2025-10-26 21:32:35 +03:00
|
|
|
const authStore = useAuthStore()
|
|
|
|
|
|
2025-10-18 14:10:21 +03:00
|
|
|
const uploadImage = async (uuid, file) => {
|
|
|
|
|
const formData = new FormData()
|
|
|
|
|
formData.append('file', file)
|
|
|
|
|
formData.append('imageType', 'all')
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await apiClient.post(`/merch/images/${uuid}`, formData)
|
|
|
|
|
|
|
|
|
|
if (response.status !== 200) {
|
|
|
|
|
throw new Error(`Upload failed: ${response.status}`)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-26 21:32:35 +03:00
|
|
|
const { fullImage, thumbnail } = response.data
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
fullImage,
|
|
|
|
|
thumbnail,
|
|
|
|
|
}
|
2025-10-18 14:10:21 +03:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Upload failed:', error)
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-26 21:32:35 +03:00
|
|
|
const getImageUrl = (merchUuid, type = 'full') => {
|
|
|
|
|
const userUuid = authStore.userUuid;
|
|
|
|
|
if (!userUuid) throw new Error('userUuid not found in store');
|
2025-10-18 14:10:21 +03:00
|
|
|
|
2025-10-26 21:32:35 +03:00
|
|
|
const allowedTypes = ['full', 'thumbnail'];
|
|
|
|
|
if (!allowedTypes.includes(type)) {
|
|
|
|
|
throw new Error(`Invalid type: ${type}. Allowed values: ${allowedTypes.join(', ')}`);
|
2025-10-18 14:10:21 +03:00
|
|
|
}
|
2025-10-26 21:32:35 +03:00
|
|
|
|
|
|
|
|
return `${IMAGE_STORAGE_URL}/merchImages/${userUuid}/${merchUuid}?type=${type}`;
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-18 14:10:21 +03:00
|
|
|
|
|
|
|
|
const deleteImage = async (uuid) => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await apiClient.delete(`/merch/images/${uuid}`)
|
2025-10-19 19:37:37 +03:00
|
|
|
|
2025-10-18 14:10:21 +03:00
|
|
|
if (response.status !== 200) {
|
|
|
|
|
throw new Error(`Delete failed: ${response.status}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Delete image failed:', error)
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
uploadImage,
|
|
|
|
|
getImageUrl,
|
|
|
|
|
deleteImage,
|
|
|
|
|
}
|
|
|
|
|
}
|