Create the projects part.

This commit is contained in:
2025-01-22 16:37:24 +01:00
parent 0e712a3412
commit 83cea92630
13 changed files with 829 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Project;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
class ProjectController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(): JsonResponse
{
return response()->json(Project::with(['customer'])->get());
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): JsonResponse
{
$projectData = $request->validate([
'customer_id' => 'required|exists:customers,id',
'name' => 'required|string',
'project_number' => 'required|string',
'description' => 'nullable|string',
'start_date' => 'required|date',
'end_date' => 'required|date',
]);
$project = new Project($projectData);
$project->save();
return response()->json($project);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Project $project): JsonResponse
{
$projectData = $request->validate([
'customer_id' => 'required|exists:customers,id',
'name' => 'required|string',
'project_number' => 'required|string',
'description' => 'nullable|string',
'start_date' => 'required|date',
'end_date' => 'required|date',
]);
$project->update($projectData);
return response()->json($project);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Project $project)
{
//
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Http\Controllers;
use App\Models\Project;
use Illuminate\Contracts\View\View;
class ProjectController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(): View
{
return view('projects.index');
}
/**
* Show the form for creating a new resource.
*/
public function create(): View
{
return view('projects.create');
}
/**
* Display the specified resource.
*/
public function show(Project $project): View
{
return view('projects.show', ['project' => $project]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Project $project): View
{
return view('projects.edit', ['project' => $project]);
}
}

61
app/Models/Project.php Normal file
View File

@@ -0,0 +1,61 @@
<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class Project extends Model
{
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'customer_id',
'name',
'project_number',
'description',
'start_date',
'end_date',
];
/**
* The attributes that are appended with attribute getters.
*
* @var string[]
*/
protected $appends = [
'start',
'end',
];
/**
* Get the end_date attribute in local format.
*/
public function getStartAttribute(): string
{
return (is_null($this->start_date)) ? '' : Carbon::createFromFormat('Y-m-d', $this->start_date)->format('d.m.Y');
}
/**
* Get the end_date attribute in local format.
*/
public function getEndAttribute(): string
{
return (is_null($this->end_date)) ? '' : Carbon::createFromFormat('Y-m-d', $this->end_date)->format('d.m.Y');
}
/**
* Get the customer this project belongs to.
*/
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}
}