Skip to content

Commit

Permalink
feat: add image uploader to category form and services
Browse files Browse the repository at this point in the history
  • Loading branch information
phojie committed Dec 30, 2022
1 parent 160daea commit 671029a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 1 deletion.
12 changes: 12 additions & 0 deletions app/Services/CategoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public function store(CategoryRequest $request): void

$productsIds = collect($request->products)->pluck('id')->toArray();
$category->products()->attach($productsIds);

// if has request image
if ($request->image) {
(new FileUploaderService())->uploadCategoryImageToMedia($category->id, $request->image);
}
});
} catch (\Exception $e) {
throw $e;
Expand All @@ -61,6 +66,13 @@ public function update(CategoryRequest $request, Category $category): void

$productsIds = collect($request->products)->pluck('id')->toArray();
$category->products()->sync($productsIds);

// if has request image
if ($request->image) {
(new FileUploaderService())->uploadCategoryImageToMedia($category->id, $request->image);
} else {
(new FileUploaderService())->deleteCategoryImageFromMedia($category->id);
}
});
} catch (\Exception $e) {
throw $e;
Expand Down
28 changes: 28 additions & 0 deletions app/Services/FileUploaderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Services;

use App\Models\Category;
use App\Models\Product;
use App\Models\TemporaryFile;
use App\Models\User;
Expand Down Expand Up @@ -44,6 +45,23 @@ public function uploadProductImageToMedia(string $id, string $image): void
}
}

public function uploadCategoryImageToMedia(string $id, string $image): void
{
try {
$temporaryFile = TemporaryFile::where('folder', $image)->first();
if($temporaryFile) {
$product = Category::findOrFail($id);
$product->addMedia(storage_path('app/public/tmp/'.$image.'/'.$temporaryFile->filename))
->toMediaCollection('image');

(new TemporaryFileService())->delete($temporaryFile->folder);
}

} catch (\Exception $e) {
throw $e;
}
}

public function deleteUserAvatarFromMedia(string $id): void
{
try {
Expand All @@ -63,4 +81,14 @@ public function deleteProductImageFromMedia(string $id): void
throw $e;
}
}

public function deleteCategoryImageFromMedia(string $id): void
{
try {
$product = Category::findOrFail($id);
$product->clearMediaCollection('image');
} catch (\Exception $e) {
throw $e;
}
}
}
2 changes: 1 addition & 1 deletion resources/js/Components/Form/FormProduct.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const processing = $computed(() => useProductStore().processing)
<JFileInput
v-model="form.image"
accepted-file-types="image/*"
label="Cover Image"
label="Product Image"
/>

<JSelect
Expand Down
6 changes: 6 additions & 0 deletions resources/js/components/Form/FormCategory.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const processing = $computed(() => useCategoryStore().processing)

<template>
<div class="pt-6 pb-5 space-y-6">
<JFileInput
v-model="form.image"
accepted-file-types="image/*"
label="Cover Image"
/>

<JTextField
id="name"
v-model="form.name"
Expand Down
2 changes: 2 additions & 0 deletions resources/js/store/category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const useCategoryStore = defineStore('category', () => {
id: '',
name: '',
description: '',
image: '',

products: [],
})
Expand Down Expand Up @@ -247,6 +248,7 @@ export const useCategoryStore = defineStore('category', () => {
form.id = ''
form.name = ''
form.description = ''
form.image = ''
form.status = 'active'
form.products = []

Expand Down
1 change: 1 addition & 0 deletions resources/js/types/category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface Category {
description: string
status?: string
slug?: string
image?: string
createdAt?: string
updatedAt?: string

Expand Down

0 comments on commit 671029a

Please sign in to comment.