diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 8d31622..7ae3c93 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -37,12 +37,20 @@ class ProjectController extends Controller } /** - * TODO: Complete this method * Store a newly created project in storage. */ public function store(Request $request) { - // + $user = User::find(auth()->user()->id); + + $data = $request->validate([ + 'name' => 'required|unique:projects|max:255', + 'description' => 'nullable|max:255', + ]); + + $user->projects()->save(new Project($data)); + + return redirect()->route('project.index'); } /** @@ -51,7 +59,7 @@ class ProjectController extends Controller */ public function show(Project $project) { - // + return redirect()->route('project.index'); } /** @@ -68,7 +76,18 @@ class ProjectController extends Controller */ public function update(Request $request, Project $project) { - // + $user = User::find(auth()->user()->id); + $projects = $user->projects; + $project = $projects->find($project->id); + + $data = $request->validate([ + 'name' => 'required|unique:projects|max:255', + 'description' => 'nullable|max:255', + ]); + + $project->update($data); + + return back()->with('status', 'Project updated!'); } /** diff --git a/app/Http/Controllers/ProjectTodoController.php b/app/Http/Controllers/ProjectTodoController.php index c106068..a5c7234 100644 --- a/app/Http/Controllers/ProjectTodoController.php +++ b/app/Http/Controllers/ProjectTodoController.php @@ -77,6 +77,7 @@ class ProjectTodoController extends Controller $user = User::find(auth()->user()->id); $project = $user->projects->find($project_id); + // Add the Todo to the Project $project->todos()->save($todo); return redirect()->route('project.todo.index', $project_id) @@ -140,8 +141,7 @@ class ProjectTodoController extends Controller $todo->update($data); - return redirect()->route('project.todo.index', $project_id) - ->with('success', 'Todo updated successfully'); + return back()->with('success', 'Todo updated successfully'); } /** diff --git a/app/Http/Controllers/TodoController.php b/app/Http/Controllers/TodoController.php deleted file mode 100644 index ecc6025..0000000 --- a/app/Http/Controllers/TodoController.php +++ /dev/null @@ -1,181 +0,0 @@ -user()->id); - - $projects = $user->projects; - - $todos = collect(); - - foreach ($projects as $project) { - $todos = $todos->merge($project->todos); - } - - - return view('todo.index', [ - 'todos' => $todos->whereNull('completed_at')->values(), - 'completed' => $todos->whereNotNull('completed_at')->values(), - 'projects' => $projects, - ]); - } - - /** - * Show the form for creating a new resource. - */ - public function create(): Factory|View|Application - { - return view('todo.create'); - } - - /** - * Store a newly created resource in storage. - */ - public function store(StoreTodoRequest $request): RedirectResponse - { - $validatedData = $request->validate([ - 'title' => 'required|max:255', - 'description' => 'nullable|max:255', - 'due_start' => 'nullable|date', - // due_end is not required, but if it is provided, it must be after due_start - 'due_end' => 'nullable|after:due_start', - ]); - - $due_end = match (true) { - isset($validatedData['due_end']) => strtotime($validatedData['due_end']), - isset($validatedData['due_start']) => strtotime($validatedData['due_start']), - default => null, - }; - - $validatedData = array_merge($validatedData, [ - // due_end = due_start if due_end is not provided and due_start is provided or null - 'due_end' => $due_end, - 'user_id' => auth()->user()->id, - ]); - - // Modify all dates to unix timestamp - if (isset($validatedData['due_start'])) - $validatedData['due_start'] = strtotime($validatedData['due_start']); - if (isset($validatedData['due_end'])) - $validatedData['due_end'] = strtotime($validatedData['due_end']); - - $todo = new Todo($validatedData); - - $user = User::find(auth()->user()->id); - $project = $user->projects->first(); - $project->todos()->save($todo); - - // Set flash message - session()->flash('success', 'Todo created successfully.'); - - return redirect()->route('todo.index'); - } - - /** - * Display the specified resource. - */ - public function show(Todo $todo): Factory|View|Application - { - $user = User::find(auth()->user()->id); - $projects = $user->projects; - $todo = $projects->first() - ->todos - ->where('id', $todo->id) - ->first(); - - return view('todo.show', compact('todo')); - } - - /** - * Show the form for editing the specified resource. - */ - public function edit(Todo $todo): Factory|View|Application - { - $user = User::find(auth()->user()->id); - $projects = $user->projects; - $todo = $projects->first() - ->todos - ->where('id', $todo->id) - ->first(); - - return view('todo.edit', compact('todo')); - } - - /** - * Update the specified resource in storage. - * Can either update completed_at (Checkbox) or other fields - * - * @param UpdateTodoRequest $request UpdateTodoRequest - * @param Todo $todo Todo Object - * @return RedirectResponse Redirect to todo.index - */ - public function update(UpdateTodoRequest $request, Todo $todo): RedirectResponse - { - $data = $request->only(['title', 'description', 'due_start', 'due_end', 'completed_at']); - - if (Request::filled('completed_at')) { - $todo->completed_at = Request::input('completed_at') === 'on' ? strtotime(now($this->timezone)) : null; - $todo->save(); - return redirect()->route('todo.index')->with('success', 'Good job! Todo completed.'); - } else { - // If 'completed_at' is not provided, toggle its value (only if the request is empty) - if (empty($data)) - $todo->completed_at = $todo->completed_at ? null : strtotime(now($this->timezone)); - else - // Continue to update other fields - unset($data['completed_at']); - } - - if (Request::filled('due_start')) { - $data['due_start'] = strtotime(Request::input('due_start')); - } - - if (Request::filled('due_end')) { - $data['due_end'] = strtotime(Request::input('due_end')); - } elseif (isset($data['due_start'])) { - // If 'due_end' is not provided, set it to 'due_start' value - $data['due_end'] = strtotime(Request::input('due_start')); - } - - $todo->update($data); - - return redirect()->route('todo.index')->with('success', 'Todo updated successfully.'); - } - - - /** - * Remove the specified resource from storage. - */ - public function destroy(Todo $todo): RedirectResponse - { - $todo = Todo::where('user_id', auth()->user()->id) - ->where('id', $todo->id) - ->firstOrFail(); - $todo->delete(); - - // Set flash message - session()->flash('success', 'Todo deleted successfully.'); - - return redirect()->route('todo.index'); - } -} diff --git a/app/Models/Project.php b/app/Models/Project.php index 365e7ed..ff98aa5 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasManyThrough; +use Illuminate\Database\Eloquent\Relations\HasOneThrough; class Project extends Model { @@ -25,13 +26,13 @@ class Project extends Model * Relationship with Todo model (one to many) * @return BelongsToMany */ - public function todos(): HasManyThrough + public function todos(): BelongsToMany { - return $this->hasManyThrough(Todo::class, projectTodo::class, 'project_id', 'id', 'id', 'todo_id'); + return $this->belongsToMany(Todo::class, 'project_todo', 'project_id', 'todo_id'); } public function user(): BelongsTo { - return $this->belongsTo(projectUser::class, 'project_user', 'project_id', 'user_id'); + return $this->belongsTo(User::class); } } diff --git a/app/Models/Todo.php b/app/Models/Todo.php index 66c84cb..b856553 100644 --- a/app/Models/Todo.php +++ b/app/Models/Todo.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use App\Traits\UuidTrait; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\Relations\HasOneThrough; use Illuminate\Database\Eloquent\Relations\MorphTo; @@ -39,7 +40,7 @@ class Todo extends Model public function project(): HasOneThrough { - return $this->hasOneThrough(Project::class, ProjectTodo::class, 'todo_id', 'id', 'id', 'project_id'); + return $this->hasOneThrough(Project::class, projectTodo::class, 'todo_id', 'id', 'id', 'project_id'); } } diff --git a/app/Models/User.php b/app/Models/User.php index e73d05a..c983ac4 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -5,6 +5,8 @@ namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; use App\Traits\UuidTrait; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -45,13 +47,13 @@ class User extends Authenticatable 'password' => 'hashed', ]; - public function projects(): HasManyThrough + public function projects(): BelongsToMany { - return $this->hasManyThrough(Project::class, projectUser::class, 'user_id', 'id', 'id', 'project_id'); + return $this->belongsToMany(Project::class, 'project_user', 'user_id', 'project_id'); } - public function todos(): HasManyThrough - { - return $this->hasManyThrough(Todo::class, projectUser::class, 'user_id', 'id', 'id', 'project_id'); - } +// public function todos(): HasManyThrough +// { +// return $this->hasManyThrough(Todo::class, projectUser::class, 'user_id', 'id', 'id', 'project_id'); +// } } diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index 67159a8..0769b7b 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -17,7 +17,7 @@ class RouteServiceProvider extends ServiceProvider * * @var string */ - public const HOME = '/todo'; + public const HOME = '/project'; /** * Define your route model bindings, pattern filters, and other route configuration. diff --git a/resources/views/project/create.blade.php b/resources/views/project/create.blade.php new file mode 100644 index 0000000..0c19ce4 --- /dev/null +++ b/resources/views/project/create.blade.php @@ -0,0 +1,55 @@ + + +

+ {{ __('Create Project') }} +

+
+ + +
+
+ @csrf +
+
+
+ +
+ + + @error('name') +
+ {{ $message }} +
+ @enderror +
+ +
+ + + @error('description') +
+ {{ $message }} +
+ @enderror +
+ +
+ +
+ +
+
+
+
+
+ + +
+ + diff --git a/resources/views/project/edit.blade.php b/resources/views/project/edit.blade.php index b031a70..bd9a4a4 100644 --- a/resources/views/project/edit.blade.php +++ b/resources/views/project/edit.blade.php @@ -5,6 +5,19 @@ + + @if(session('success')) +
+ {{ session('success') }} +
+ @endif + + @if(session('error')) +
+ {{ session('error') }} +
+ @endif +
diff --git a/resources/views/project/index.blade.php b/resources/views/project/index.blade.php index 8bfff72..cee09b9 100644 --- a/resources/views/project/index.blade.php +++ b/resources/views/project/index.blade.php @@ -9,6 +9,18 @@ + + @if(session('success')) +
+ {{ session('success') }} +
+ @endif + + @if(session('error')) +
+ {{ session('error') }} +
+ @endif
@@ -83,12 +95,12 @@
@foreach ($todos as $todo) @if (!$todo->completed_at) - : +
- @csrf