From 39feadab324b0bd542163a0a44994e53f8e152fe Mon Sep 17 00:00:00 2001 From: bad Date: Thu, 16 Dec 2021 11:51:47 +0100 Subject: [PATCH] Category management --- .php-cs-fixer.cache | 2 +- app/Http/Controllers/CategoryController.php | 64 +++++++++++++++++++ app/Http/Resources/order.php | 19 ------ app/Models/Category.php | 3 +- ...1_12_08_203110_create_categories_table.php | 4 +- resources/views/category.blade.php | 27 ++++++++ routes/web.php | 5 +- 7 files changed, 99 insertions(+), 25 deletions(-) create mode 100644 app/Http/Controllers/CategoryController.php delete mode 100644 app/Http/Resources/order.php create mode 100644 resources/views/category.blade.php diff --git a/.php-cs-fixer.cache b/.php-cs-fixer.cache index a3394f2..9e80abc 100644 --- a/.php-cs-fixer.cache +++ b/.php-cs-fixer.cache @@ -1 +1 @@ -{"php":"8.0.13","version":"3.0.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":{"elements":["const","method","property"]},"blank_line_after_namespace":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"\/run\/user\/1000\/neoformat\/product.blade.php":1493321553,"\/tmp\/neoformat\/user.blade.php":2134171036,"\/tmp\/neoformat\/edit.blade.php":2692964585,"\/tmp\/neoformat\/ProductController.php":4228240463,"\/tmp\/neoformat\/DatabaseSeeder.php":3199248297}} \ No newline at end of file +{"php":"8.0.13","version":"3.0.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":{"elements":["const","method","property"]},"blank_line_after_namespace":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"\/run\/user\/1000\/neoformat\/product.blade.php":1493321553,"\/tmp\/neoformat\/user.blade.php":2134171036,"\/tmp\/neoformat\/edit.blade.php":2692964585,"\/tmp\/neoformat\/ProductController.php":4228240463,"\/tmp\/neoformat\/DatabaseSeeder.php":3199248297,"\/tmp\/neoformat\/category.blade.php":2623876730,"\/tmp\/neoformat\/CategoryController.php":4271583155}} \ No newline at end of file diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php new file mode 100644 index 0000000..245493f --- /dev/null +++ b/app/Http/Controllers/CategoryController.php @@ -0,0 +1,64 @@ +get(); + return view("category", ["categories"=>$categories]); + } + + /** + * Store a newly created resource in storage. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\Response + */ + public function store(Request $request) + { + $validatedData = $request->validate([ + // The server should make sure to serve SVG files with the correct CSP to prevent XSS + 'name' => 'required|string|unique:categories', + ]); + /* + Once the image is validated , create the name on the image + */ + $cat = new Category(); + $cat->name = $validatedData["name"]; + + $cat->save(); + return back(); + } + + public function edit(Category $cat, Request $request) + { + $validatedData = $request->validate([ + // The server should make sure to serve SVG files with the correct CSP to prevent XSS + 'name' => 'required|string|unique:categories', + ]); + /* + Once the image is validated , create the name on the image + */ + $cat->name = $validatedData["name"]; + $cat->save(); + return back(); + } + + /** + * Remove the specified resource from storage. + * + * @param \App\Models\Image $image + * @return \Illuminate\Http\Response + */ + public function destroy($id) + { + Category::query()->findOrFail($id)->delete(); + return back(); + } +} diff --git a/app/Http/Resources/order.php b/app/Http/Resources/order.php deleted file mode 100644 index 6222be1..0000000 --- a/app/Http/Resources/order.php +++ /dev/null @@ -1,19 +0,0 @@ -id(); $table->timestamps(); - $table->foreignUuid("product_uuid")->references("uuid")->on("products"); - $table->foreignUuid("category_uuid")->references("uuid")->on("categories"); + $table->foreignUuid("product_uuid")->references("uuid")->on("products")->cascadeOnDelete()->cascadeOnUpdate(); + $table->foreignUuid("category_uuid")->references("uuid")->on("categories")->cascadeOnDelete()->cascadeOnUpdate(); }); } diff --git a/resources/views/category.blade.php b/resources/views/category.blade.php new file mode 100644 index 0000000..0d3fd31 --- /dev/null +++ b/resources/views/category.blade.php @@ -0,0 +1,27 @@ +@push('head') + +@endpush + +@extends('layouts.app') + +@section('title', "Sklep") + +@section('main') +
+ + + @csrf +
+ @foreach($categories as $category) +
+ + + @csrf +
+
+ + @csrf + @method("DELETE") +
+ @endforeach +@endsection() diff --git a/routes/web.php b/routes/web.php index e1bd81c..7067126 100644 --- a/routes/web.php +++ b/routes/web.php @@ -4,14 +4,14 @@ use App\Http\Controllers\ImageController; use App\Http\Controllers\LoginController; use App\Http\Controllers\MainPageController; use App\Http\Controllers\CartController; +use App\Http\Controllers\CategoryController; use App\Http\Controllers\OrderController; use App\Http\Controllers\ProductController; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Route; /* -|-------------------------------------------------------------------------- -| Web Routes +|-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These @@ -44,3 +44,4 @@ Route::resource("order", OrderController::class)->middleware("auth"); Route::post('/order/create', [OrderController::class, "create"])->name("order.create"); Route::resource("image", ImageController::class)->only(["store", "destroy", "create"])->middleware("auth.admin"); +Route::resource("category", CategoryController::class)->only(["index","destroy","edit", "store"])->middleware("auth.admin");