-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd2244d
commit 10a6bb5
Showing
32 changed files
with
846 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Actions\Projects; | ||
|
||
use App\Models\Project; | ||
use App\Models\User; | ||
use Illuminate\Support\Facades\Validator; | ||
|
||
class CreateProject | ||
{ | ||
public function create(User $user, array $input): Project | ||
{ | ||
$this->validate($user, $input); | ||
|
||
$project = new Project([ | ||
'user_id' => $user->id, | ||
'name' => $input['name'], | ||
]); | ||
|
||
$project->save(); | ||
|
||
return $project; | ||
} | ||
|
||
private function validate(User $user, array $input): void | ||
{ | ||
Validator::make($input, [ | ||
'name' => [ | ||
'required', | ||
'string', | ||
'max:255', | ||
'unique:projects,name,NULL,id,user_id,'.$user->id, | ||
], | ||
])->validate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Actions\Projects; | ||
|
||
use App\Models\Project; | ||
use App\Models\User; | ||
use Illuminate\Validation\ValidationException; | ||
|
||
class DeleteProject | ||
{ | ||
public function delete(User $user, int $projectId): void | ||
{ | ||
/** @var Project $project */ | ||
$project = $user->projects()->findOrFail($projectId); | ||
|
||
if ($user->projects()->count() === 1) { | ||
throw ValidationException::withMessages([ | ||
'project' => __('Cannot delete the last project.'), | ||
]); | ||
} | ||
|
||
if ($user->current_project_id == $project->id) { | ||
/** @var Project $randomProject */ | ||
$randomProject = $user->projects()->where('id', '!=', $project->id)->first(); | ||
$user->current_project_id = $randomProject->id; | ||
$user->save(); | ||
} | ||
|
||
$project->delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Actions\Projects; | ||
|
||
use App\Models\Project; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Validation\Rule; | ||
|
||
class UpdateProject | ||
{ | ||
public function update(Project $project, array $input): Project | ||
{ | ||
$this->validate($project, $input); | ||
|
||
$project->name = $input['name']; | ||
|
||
$project->save(); | ||
|
||
return $project; | ||
} | ||
|
||
private function validate(Project $project, array $input): void | ||
{ | ||
Validator::make($input, [ | ||
'name' => [ | ||
'required', | ||
'string', | ||
'max:255', | ||
Rule::unique('projects')->ignore($project->id), | ||
], | ||
])->validate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Project; | ||
use App\Models\User; | ||
use Illuminate\Contracts\View\View; | ||
|
||
class ProjectController extends Controller | ||
{ | ||
public function index(): View | ||
{ | ||
return view('projects.index'); | ||
} | ||
|
||
public function switch($projectId) | ||
{ | ||
/** @var User $user */ | ||
$user = auth()->user(); | ||
|
||
/** @var Project $project */ | ||
$project = $user->projects()->findOrFail($projectId); | ||
|
||
$user->current_project_id = $project->id; | ||
$user->save(); | ||
|
||
return redirect()->route('servers'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App\Http\Livewire\Projects; | ||
|
||
use App\Traits\HasToast; | ||
use App\Traits\RefreshComponentOnBroadcast; | ||
use Illuminate\Contracts\View\View; | ||
use Livewire\Component; | ||
|
||
class CreateProject extends Component | ||
{ | ||
use HasToast; | ||
use RefreshComponentOnBroadcast; | ||
|
||
public bool $open = false; | ||
|
||
public array $inputs = []; | ||
|
||
public function create(): void | ||
{ | ||
app(\App\Actions\Projects\CreateProject::class) | ||
->create(auth()->user(), $this->inputs); | ||
|
||
$this->emitTo(ProjectsList::class, '$refresh'); | ||
|
||
$this->dispatchBrowserEvent('created', true); | ||
} | ||
|
||
public function render(): View | ||
{ | ||
if (request()->query('create')) { | ||
$this->open = true; | ||
} | ||
|
||
return view('livewire.projects.create-project'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App\Http\Livewire\Projects; | ||
|
||
use App\Actions\Projects\UpdateProject; | ||
use App\Models\Project; | ||
use App\Traits\RefreshComponentOnBroadcast; | ||
use Illuminate\Contracts\View\View; | ||
use Livewire\Component; | ||
|
||
class EditProject extends Component | ||
{ | ||
use RefreshComponentOnBroadcast; | ||
|
||
public Project $project; | ||
|
||
public array $inputs = []; | ||
|
||
public function save(): void | ||
{ | ||
app(UpdateProject::class)->update($this->project, $this->inputs); | ||
|
||
$this->redirect(route('projects')); | ||
} | ||
|
||
public function mount(): void | ||
{ | ||
$this->inputs = [ | ||
'name' => $this->project->name, | ||
]; | ||
} | ||
|
||
public function render(): View | ||
{ | ||
return view('livewire.projects.edit-project'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace App\Http\Livewire\Projects; | ||
|
||
use App\Actions\Projects\DeleteProject; | ||
use App\Traits\HasToast; | ||
use App\Traits\RefreshComponentOnBroadcast; | ||
use Illuminate\Contracts\View\View; | ||
use Illuminate\Validation\ValidationException; | ||
use Livewire\Component; | ||
|
||
class ProjectsList extends Component | ||
{ | ||
use HasToast; | ||
use RefreshComponentOnBroadcast; | ||
|
||
protected $listeners = [ | ||
'$refresh', | ||
]; | ||
|
||
public int $deleteId; | ||
|
||
public function delete(): void | ||
{ | ||
try { | ||
app(DeleteProject::class)->delete(auth()->user(), $this->deleteId); | ||
|
||
$this->redirect(route('projects')); | ||
|
||
return; | ||
} catch (ValidationException $e) { | ||
$this->toast()->error($e->getMessage()); | ||
} | ||
} | ||
|
||
public function render(): View | ||
{ | ||
return view('livewire.projects.projects-list', [ | ||
'projects' => auth()->user()->projects()->orderByDesc('id')->get(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
|
||
/** | ||
* @property int $id | ||
* @property int $user_id | ||
* @property string $name | ||
* @property Carbon $created_at | ||
* @property Carbon $updated_at | ||
* @property User $user | ||
* @property Collection<Server> $servers | ||
* @property Collection<NotificationChannel> $notificationChannels | ||
*/ | ||
class Project extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $fillable = [ | ||
'user_id', | ||
'name', | ||
]; | ||
|
||
public static function boot(): void | ||
{ | ||
parent::boot(); | ||
|
||
static::deleting(function (Project $project) { | ||
$project->servers()->each(function (Server $server) { | ||
$server->delete(); | ||
}); | ||
}); | ||
} | ||
|
||
public function user(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
|
||
public function servers(): HasMany | ||
{ | ||
return $this->hasMany(Server::class); | ||
} | ||
|
||
public function notificationChannels(): HasMany | ||
{ | ||
return $this->hasMany(NotificationChannel::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.