Updated project for admin panel
This commit is contained in:
63
app/Http/Controllers/Dashboard/ProjectController.php
Normal file
63
app/Http/Controllers/Dashboard/ProjectController.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Dashboard;
|
||||
|
||||
use App\Http\Requests\ProjectRequest;
|
||||
use App\Models\Project;
|
||||
use App\Repository\Interfaces\ProjectRepository;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ProjectController
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
private ProjectRepository $projectRepository
|
||||
) {}
|
||||
|
||||
public function edit(Project $project): View
|
||||
{
|
||||
return view('dashboard.projects.edit', compact('project'));
|
||||
}
|
||||
|
||||
public function update(ProjectRequest $request, Project $project): RedirectResponse
|
||||
{
|
||||
$validated = $request->validated();
|
||||
if ($this->projectRepository->update($project, $validated)) {
|
||||
return back()->with('message', 'Zaktualizowano projekt!');
|
||||
}
|
||||
|
||||
return back()->withError(['message_error', 'Wystąpił błąd podczas aktualizacji!']);
|
||||
}
|
||||
|
||||
public function create(): View
|
||||
{
|
||||
return view('dashboard.projects.create');
|
||||
}
|
||||
|
||||
public function store(ProjectRequest $request): RedirectResponse
|
||||
{
|
||||
$validated = $request->validated();
|
||||
if ($project = $this->projectRepository->create($validated)) {
|
||||
return redirect()
|
||||
->route('admin.project.update', compact('project'))
|
||||
->with('message', 'Utworzono projekt!');
|
||||
}
|
||||
|
||||
return back()->withError(['message_error', 'Wystąpił błąd podczas tworzenia!']);
|
||||
}
|
||||
|
||||
public function delete(Project $project): View
|
||||
{
|
||||
return view('dashboard.projects.delete', compact('project'));
|
||||
}
|
||||
|
||||
public function destroy(Project $project): RedirectResponse
|
||||
{
|
||||
$title = $project->title;
|
||||
$project->delete();
|
||||
return redirect()->route('admin.home')->with('message', 'Usunięto projekt "'. $title .'"');
|
||||
}
|
||||
|
||||
}
|
37
app/Http/Requests/ProjectRequest.php
Normal file
37
app/Http/Requests/ProjectRequest.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ProjectRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'title' => 'required|string|min:3|max:255',
|
||||
'author' => 'required|string|min:3|max:50',
|
||||
'categories' => 'nullable|string',
|
||||
'release_date' => 'required|date:Y-m-d',
|
||||
'update_date' => 'nullable|date:Y-m-d',
|
||||
'project_url' => 'nullable|string',
|
||||
'project_version' => 'nullable|string',
|
||||
'description' => 'nullable|string',
|
||||
];
|
||||
}
|
||||
}
|
@@ -23,7 +23,8 @@ class ProjectResource extends JsonResource
|
||||
'categories' => $this->categories,
|
||||
'author' => $this->author,
|
||||
'images' => $this->images,
|
||||
'release_data' => $this->release_data,
|
||||
'release_date' => $this->release_date,
|
||||
'update_date' => $this->release_date,
|
||||
'project_url' => $this->project_url,
|
||||
'project_version' => $this->project_version,
|
||||
'description' => $this->description,
|
||||
|
@@ -25,6 +25,8 @@ class Project extends Model
|
||||
|
||||
// use HasFactory;
|
||||
|
||||
protected $dateFormat = 'Y-m-d';
|
||||
|
||||
protected $guarded = [];
|
||||
protected $casts = [
|
||||
'id' => 'integer',
|
||||
@@ -32,10 +34,34 @@ class Project extends Model
|
||||
'categories' => 'array',
|
||||
'author' => 'string',
|
||||
'images' => 'array',
|
||||
'release_data' => 'datetime:d-m-Y',
|
||||
'release_date' => 'datetime:Y-m-d',
|
||||
'update_date' => 'datetime:Y-m-d',
|
||||
'project_url' => 'string',
|
||||
'project_version' => 'string',
|
||||
'description' => 'string'
|
||||
];
|
||||
|
||||
public function getReleaseDateAttribute($value): String
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function setReleaseDateAttribute($value): void
|
||||
{
|
||||
$this->attributes['release_date'] = $value;
|
||||
}
|
||||
|
||||
public function getUpdateDateAttribute($value): String|null
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function setUpdateDateAttribute($value): void
|
||||
{
|
||||
if (!is_null($value))
|
||||
$this->attributes['update_date'] = $value;
|
||||
else
|
||||
$this->attributes['update_date'] = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -63,7 +63,7 @@ class ProjectRepository implements ProjectRepositoryInterface
|
||||
$toSave['author'] = $data['author'];
|
||||
|
||||
if (isset($data['release_date']))
|
||||
$toSave['release_date'] = Carbon::createFromFormat('Y-d-m', $data['release_date']);
|
||||
$toSave['release_date'] = $data['release_date'];
|
||||
|
||||
if (isset($data['project_url']))
|
||||
$toSave['project_url'] = $data['project_url'];
|
||||
@@ -76,12 +76,16 @@ class ProjectRepository implements ProjectRepositoryInterface
|
||||
|
||||
if (isset($data['categories']) && is_array($data['categories']))
|
||||
$toSave['categories'] = $data['categories'];
|
||||
else if (isset($data['categories']) && !empty($data['categories']))
|
||||
$toSave['categories'] = explode(',', str_replace(', ', ',', $data['categories']));
|
||||
|
||||
if (isset($data['images']) && is_array($data['images']))
|
||||
$toSave['images'] = $data['images'];
|
||||
|
||||
if (isset($data['update_date']) && !empty($data['update_date']))
|
||||
$toSave['update_date'] = Carbon::createFromFormat('Y-d-m', $data['update_date']);
|
||||
$toSave['update_date'] = $data['update_date'];
|
||||
else
|
||||
$toSave['update_date'] = null;
|
||||
|
||||
return $toSave;
|
||||
}
|
||||
|
Reference in New Issue
Block a user