2022-09-20 19:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2023-11-29 13:53:08 +00:00
|
|
|
use App\Http\Requests\Workspace\CustomDomainRequest;
|
2024-01-22 13:28:22 +00:00
|
|
|
use App\Http\Resources\WorkspaceResource;
|
2022-09-20 19:59:52 +00:00
|
|
|
use App\Models\Workspace;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Service\WorkspaceHelper;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
class WorkspaceController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$this->authorize('viewAny', Workspace::class);
|
2024-01-22 13:28:22 +00:00
|
|
|
return WorkspaceResource::collection(Auth::user()->workspaces);
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function listUsers(Request $request, $workspaceId)
|
|
|
|
{
|
|
|
|
$workspace = Workspace::findOrFail($workspaceId);
|
|
|
|
$this->authorize('view', $workspace);
|
|
|
|
|
|
|
|
return (new WorkspaceHelper($workspace))->getAllUsers();
|
|
|
|
}
|
|
|
|
|
2023-11-29 13:53:08 +00:00
|
|
|
public function saveCustomDomain(CustomDomainRequest $request)
|
|
|
|
{
|
|
|
|
$request->workspace->custom_domains = $request->customDomains;
|
|
|
|
$request->workspace->save();
|
2024-01-22 13:28:22 +00:00
|
|
|
return new WorkspaceResource($request->workspace);
|
2023-11-29 13:53:08 +00:00
|
|
|
}
|
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
public function delete($id)
|
|
|
|
{
|
|
|
|
$workspace = Workspace::findOrFail($id);
|
|
|
|
$this->authorize('delete', $workspace);
|
|
|
|
|
|
|
|
$id = $workspace->id;
|
|
|
|
$workspace->delete();
|
|
|
|
return $this->success([
|
|
|
|
'message' => 'Workspace deleted.',
|
|
|
|
'workspace_id' => $id
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create(Request $request)
|
|
|
|
{
|
|
|
|
$user = $request->user();
|
|
|
|
|
|
|
|
$this->validate($request, [
|
|
|
|
'name' => 'required'
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Create workspace
|
|
|
|
$workspace = Workspace::create([
|
|
|
|
'name' => $request->name,
|
|
|
|
'icon' => ($request->emoji) ? $request->emoji : '',
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Add relation with user
|
|
|
|
$user->workspaces()->sync([
|
|
|
|
$workspace->id => [
|
|
|
|
'role' => 'admin'
|
|
|
|
]
|
|
|
|
], false);
|
|
|
|
|
|
|
|
return $this->success([
|
|
|
|
'message' => 'Workspace created.',
|
|
|
|
'workspace_id' => $workspace->id
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|