Category management

This commit is contained in:
bad 2021-12-16 11:51:47 +01:00
parent e21fa78f15
commit 39feadab32
7 changed files with 99 additions and 25 deletions

View file

@ -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}}
{"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}}

View file

@ -0,0 +1,64 @@
<?php
namespace App\Http\Controllers;
use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class CategoryController extends Controller
{
public function index()
{
$categories = Category::query()->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();
}
}

View file

@ -1,19 +0,0 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class order extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
return parent::toArray($request);
}
}

View file

@ -7,7 +7,8 @@ use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory, HasUUID;
use HasFactory;
use HasUUID;
public $primaryKey = "uuid";
protected $fillable = ['name'];

View file

@ -24,8 +24,8 @@ class CreateCategoriesTable extends Migration
$table->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();
});
}

View file

@ -0,0 +1,27 @@
@push('head')
<link rel="stylesheet" href="{{ mix("css/user.css") }}">
@endpush
@extends('layouts.app')
@section('title', "Sklep")
@section('main')
<form action="{{route("category.store")}}" method="post">
<input type="text" name="name" placeholder="nazwa" >
<button type="submit">Dodaj</button>
@csrf
</form>
@foreach($categories as $category)
<form action="{{route("category.edit", $category)}}" method="post">
<input type="text" name="name" value="{{ $category->name }}">
<button type="submit">Zatwierdź</button>
@csrf
</form>
<form action="{{route("category.destroy", $category)}}" method="post">
<button type="submit">Usuń</button>
@csrf
@method("DELETE")
</form>
@endforeach
@endsection()

View file

@ -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");