From cb5a3f00ae7fdb8e7c2b753e4ee22093326e58cd Mon Sep 17 00:00:00 2001 From: Fahim Anzam Dip Date: Fri, 16 Jul 2021 21:13:21 +0600 Subject: [PATCH] Added: Product & Upload Module --- ...021_07_14_145047_create_products_table.php | 3 +- Modules/Product/Entities/Category.php | 2 +- Modules/Product/Entities/Product.php | 2 + .../Http/Controllers/ProductController.php | 67 ++++++- .../Http/Requests/ProductCreateRequest.php | 40 +++++ .../Http/Requests/ProductUpdateRequest.php | 41 +++++ .../categories/partials/actions.blade.php | 7 +- .../Resources/views/products/create.blade.php | 25 +-- .../Resources/views/products/edit.blade.php | 170 ++++++++++++++++++ .../views/products/partials/actions.blade.php | 7 +- .../Resources/views/products/show.blade.php | 83 +++++++++ Modules/Product/Routes/web.php | 10 +- ...2021_07_16_010005_create_uploads_table.php | 33 ++++ Modules/Upload/Entities/Upload.php | 17 ++ .../Http/Controllers/UploadController.php | 26 ++- Modules/Upload/Routes/web.php | 6 +- app/DataTables/ProductCategoriesDataTable.php | 4 +- app/DataTables/ProductDataTable.php | 41 ++--- app/Providers/AppServiceProvider.php | 3 +- config/sweetalert.php | 2 +- .../views/vendor/datatables/print.blade.php | 3 + routes/web.php | 2 +- 22 files changed, 536 insertions(+), 58 deletions(-) create mode 100644 Modules/Product/Http/Requests/ProductCreateRequest.php create mode 100644 Modules/Product/Http/Requests/ProductUpdateRequest.php create mode 100644 Modules/Upload/Database/Migrations/2021_07_16_010005_create_uploads_table.php create mode 100644 Modules/Upload/Entities/Upload.php diff --git a/Modules/Product/Database/Migrations/2021_07_14_145047_create_products_table.php b/Modules/Product/Database/Migrations/2021_07_14_145047_create_products_table.php index 460ff381..2fca1017 100644 --- a/Modules/Product/Database/Migrations/2021_07_14_145047_create_products_table.php +++ b/Modules/Product/Database/Migrations/2021_07_14_145047_create_products_table.php @@ -16,6 +16,7 @@ class CreateProductsTable extends Migration Schema::create('products', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('category_id'); + $table->string('product_image')->nullable(); $table->string('product_name'); $table->string('product_code')->unique()->nullable(); $table->string('product_barcode_symbology')->nullable(); @@ -24,7 +25,7 @@ class CreateProductsTable extends Migration $table->integer('product_price'); $table->integer('product_stock_alert'); $table->integer('product_order_tax')->nullable(); - $table->boolean('product_tax_type')->nullable(); + $table->tinyInteger('product_tax_type')->nullable(); $table->text('product_note')->nullable(); $table->foreign('category_id')->references('id')->on('categories')->restrictOnDelete(); $table->timestamps(); diff --git a/Modules/Product/Entities/Category.php b/Modules/Product/Entities/Category.php index 11ef3d43..04b1f839 100644 --- a/Modules/Product/Entities/Category.php +++ b/Modules/Product/Entities/Category.php @@ -12,6 +12,6 @@ class Category extends Model protected $guarded = []; public function products() { - return $this->hasMany(Product::class, 'id', 'category_id'); + return $this->hasMany(Product::class, 'category_id', 'id'); } } diff --git a/Modules/Product/Entities/Product.php b/Modules/Product/Entities/Product.php index 9be8e5ab..7368dc8e 100644 --- a/Modules/Product/Entities/Product.php +++ b/Modules/Product/Entities/Product.php @@ -13,6 +13,8 @@ class Product extends Model implements HasMedia protected $guarded = []; + protected $with = ['media']; + public function category() { return $this->belongsTo(Category::class, 'category_id', 'id'); } diff --git a/Modules/Product/Http/Controllers/ProductController.php b/Modules/Product/Http/Controllers/ProductController.php index ce362756..7e1b337a 100644 --- a/Modules/Product/Http/Controllers/ProductController.php +++ b/Modules/Product/Http/Controllers/ProductController.php @@ -6,6 +6,11 @@ use App\DataTables\ProductDataTable; use Illuminate\Contracts\Support\Renderable; use Illuminate\Http\Request; use Illuminate\Routing\Controller; +use Illuminate\Support\Facades\Storage; +use Modules\Product\Entities\Product; +use Modules\Product\Http\Requests\ProductCreateRequest; +use Modules\Product\Http\Requests\ProductUpdateRequest; +use Modules\Upload\Entities\Upload; class ProductController extends Controller { @@ -31,8 +36,32 @@ class ProductController extends Controller * @param Request $request * @return Renderable */ - public function store(Request $request) { - // + public function store(ProductCreateRequest $request) { + $product = Product::create($request->except('image')); + + if ($request->has('image')) { + $tempFile = Upload::where('folder', $request->image)->first(); + + if ($tempFile) { + $product->addMedia(Storage::path('public/temp/' . $request->image . '/' . $tempFile->filename))->toMediaCollection(); + + Storage::deleteDirectory('public/temp/' . $request->image); + $tempFile->delete(); + } + } + + toast('Product Created!', 'success'); + + return redirect()->route('products.index'); + } + + /** + * Show the details for the specified resource. + * @param int $id + * @return Renderable + */ + public function show(Product $product) { + return view('product::products.show', compact('product')); } /** @@ -40,8 +69,8 @@ class ProductController extends Controller * @param int $id * @return Renderable */ - public function edit($id) { - return view('product::products.edit'); + public function edit(Product $product) { + return view('product::products.edit', compact('product')); } /** @@ -50,8 +79,26 @@ class ProductController extends Controller * @param int $id * @return Renderable */ - public function update(Request $request, $id) { - // + public function update(ProductUpdateRequest $request, Product $product) { + $product->update($request->except('image')); + + if ($request->has('image')) { + $tempFile = Upload::where('folder', $request->image)->first(); + + $media = $product->getMedia(); + $media[0]->delete(); + + if ($tempFile) { + $product->addMedia(Storage::path('public/temp/' . $request->image . '/' . $tempFile->filename))->toMediaCollection(); + + Storage::deleteDirectory('public/temp/' . $request->image); + $tempFile->delete(); + } + } + + toast('Product Updated!', 'info'); + + return redirect()->route('products.index'); } /** @@ -59,7 +106,11 @@ class ProductController extends Controller * @param int $id * @return Renderable */ - public function destroy($id) { - // + public function destroy(Product $product) { + $product->delete(); + + toast('Product Deleted!', 'warning'); + + return redirect()->route('products.index'); } } diff --git a/Modules/Product/Http/Requests/ProductCreateRequest.php b/Modules/Product/Http/Requests/ProductCreateRequest.php new file mode 100644 index 00000000..5e2bf672 --- /dev/null +++ b/Modules/Product/Http/Requests/ProductCreateRequest.php @@ -0,0 +1,40 @@ + ['required', 'string', 'max:255'], + 'product_code' => ['required', 'string', 'max:255', 'unique:products,product_code'], + 'product_barcode_symbology' => ['required', 'string', 'max:255'], + 'product_quantity' => ['required', 'integer', 'min:1'], + 'product_cost' => ['required', 'integer'], + 'product_price' => ['required', 'integer'], + 'product_stock_alert' => ['required', 'integer', 'min:0'], + 'product_order_tax' => ['nullable', 'integer', 'min:1'], + 'product_tax_type' => ['nullable', 'integer'], + 'product_note' => ['nullable', 'string', 'max:1000'], + 'category_id' => ['required', 'integer'] + ]; + } + + /** + * Determine if the user is authorized to make this request. + * + * @return bool + */ + public function authorize() + { + return true; + } +} diff --git a/Modules/Product/Http/Requests/ProductUpdateRequest.php b/Modules/Product/Http/Requests/ProductUpdateRequest.php new file mode 100644 index 00000000..14dbcb73 --- /dev/null +++ b/Modules/Product/Http/Requests/ProductUpdateRequest.php @@ -0,0 +1,41 @@ + ['required', 'string', 'max:255'], + 'product_code' => ['required', 'string', 'max:255', 'unique:products,product_code,' . $this->product->id], + 'product_barcode_symbology' => ['required', 'string', 'max:255'], + 'product_quantity' => ['required', 'integer', 'min:1'], + 'product_cost' => ['required', 'integer'], + 'product_price' => ['required', 'integer'], + 'product_stock_alert' => ['required', 'integer', 'min:0'], + 'product_order_tax' => ['nullable', 'integer', 'min:1'], + 'product_tax_type' => ['nullable', 'integer'], + 'product_note' => ['nullable', 'string', 'max:1000'], + 'category_id' => ['required', 'integer'] + ]; + } + + /** + * Determine if the user is authorized to make this request. + * + * @return bool + */ + public function authorize() + { + return true; + } +} diff --git a/Modules/Product/Resources/views/categories/partials/actions.blade.php b/Modules/Product/Resources/views/categories/partials/actions.blade.php index 4001da4a..de219d01 100644 --- a/Modules/Product/Resources/views/categories/partials/actions.blade.php +++ b/Modules/Product/Resources/views/categories/partials/actions.blade.php @@ -1,7 +1,12 @@ - @@ -66,7 +59,7 @@
- @@ -102,8 +95,8 @@
- - + +
@@ -112,16 +105,16 @@
- +
- - - + +
@@ -135,7 +128,7 @@
- +
diff --git a/Modules/Product/Resources/views/products/edit.blade.php b/Modules/Product/Resources/views/products/edit.blade.php index e69de29b..542bba1f 100644 --- a/Modules/Product/Resources/views/products/edit.blade.php +++ b/Modules/Product/Resources/views/products/edit.blade.php @@ -0,0 +1,170 @@ +@extends('layouts.app') + +@section('title', 'Edit Product') + +@section('third_party_stylesheets') + + +@endsection + +@section('breadcrumb') + +@endsection + +@section('content') +
+ + @csrf + @method('patch') +
+
+ @include('utils.alerts') +
+ +
+
+
+
+
+
+
+
+ + +
+
+
+
+ + +
+
+
+ +
+
+
+ + +
+
+
+
+ + +
+
+
+ +
+
+
+ + +
+
+
+
+ + +
+
+
+ +
+
+
+ + +
+
+
+
+ + +
+
+
+ +
+
+
+ + +
+
+
+
+ + +
+
+
+
+
+
+ +
+
+
+
+ + Product Image + +
+
+
+
+
+ +
+@endsection + +@section('third_party_scripts') + + + + +@endsection + +@push('page_scripts') + +@endpush + diff --git a/Modules/Product/Resources/views/products/partials/actions.blade.php b/Modules/Product/Resources/views/products/partials/actions.blade.php index e2d22c5d..79100d30 100644 --- a/Modules/Product/Resources/views/products/partials/actions.blade.php +++ b/Modules/Product/Resources/views/products/partials/actions.blade.php @@ -4,7 +4,12 @@ -