opnform/app/Http/Controllers/WorkspaceController.php

84 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2022-09-20 19:59:52 +00:00
<?php
namespace App\Http\Controllers;
use App\Http\Requests\Workspace\CustomDomainRequest;
use App\Http\Resources\WorkspaceResource;
2022-09-20 19:59:52 +00:00
use App\Models\Workspace;
use App\Service\WorkspaceHelper;
2024-02-23 10:54:12 +00:00
use Illuminate\Http\Request;
2022-09-20 19:59:52 +00:00
use Illuminate\Support\Facades\Auth;
class WorkspaceController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$this->authorize('viewAny', Workspace::class);
2024-02-23 10:54:12 +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();
}
public function saveCustomDomain(CustomDomainRequest $request)
{
$request->workspace->custom_domains = $request->customDomains;
$request->workspace->save();
2024-02-23 10:54:12 +00:00
return new WorkspaceResource($request->workspace);
}
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();
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return $this->success([
'message' => 'Workspace deleted.',
2024-02-23 10:54:12 +00:00
'workspace_id' => $id,
2022-09-20 19:59:52 +00:00
]);
}
public function create(Request $request)
{
$user = $request->user();
$this->validate($request, [
2024-02-23 10:54:12 +00:00
'name' => 'required',
2022-09-20 19:59:52 +00:00
]);
// Create workspace
$workspace = Workspace::create([
'name' => $request->name,
'icon' => ($request->emoji) ? $request->emoji : '',
]);
// Add relation with user
$user->workspaces()->sync([
$workspace->id => [
2024-02-23 10:54:12 +00:00
'role' => 'admin',
],
2022-09-20 19:59:52 +00:00
], false);
return $this->success([
'message' => 'Workspace created.',
2024-01-22 13:40:17 +00:00
'workspace_id' => $workspace->id,
2024-02-23 10:54:12 +00:00
'workspace' => new WorkspaceResource($workspace),
2022-09-20 19:59:52 +00:00
]);
}
}