Support creation of spaces
ADD
app/Actions/CreateSpaceAction.php
+<?php
+
+namespace App\Actions;
+
+use App\Models\Space;
+
+class CreateSpaceAction
+{
+ public function execute(string $name, int $currencyId, int $creatorUserId): Space
+ {
+ $space = Space::create([
+ 'name' => $name,
+ 'currency_id' => $currencyId
+ ]);
+
+ $space->users()->attach($creatorUserId, ['role' => 'admin']);
+
+ return $space;
+ }
+}

MODIFY
app/Http/Controllers/SpaceController.php
app/Http/Controllers/SpaceController.php
namespace App\Http\Controllers;
+use App\Actions\CreateSpaceAction;
use App\Actions\CreateSpaceInviteAction;
use App\Actions\StoreSpaceInSessionAction;
use App\Exceptions\SpaceInviteAlreadyExistsException;
use App\Exceptions\SpaceInviteInviteeAlreadyPresentException;
use App\Mail\InvitedToSpace;
+use App\Models\Currency;
use App\Models\Space;
use App\Models\SpaceInvite;
use App\Models\User;
@@ -16,6 +18,25 @@ use Illuminate\Support\Facades\Mail;
class SpaceController extends Controller
{
+ public function create()
+ {
+ $currencies = Currency::all();
+
+ return view('spaces.create', ['currencies' => $currencies]);
+ }
+
+ public function store(Request $request)
+ {
+ $request->validate([
+ 'name' => 'required|max:255',
+ 'currency_id' => 'required|exists:currencies,id'
+ ]);
+
+ (new CreateSpaceAction())->execute($request->name, $request->currency_id, Auth::id());
+
+ return redirect()->route('settings.spaces.index');
+ }
+
public function show($id)
{
$space = Space::find($id);

MODIFY
resources/views/settings/spaces/index.blade.php
resources/views/settings/spaces/index.blade.php
@endsection
@section('settings_body')
+ <a class="button mb-2" href="/spaces/create">{{ __('actions.create') }}</a>
<div class="box">
<ul class="box__section">
@foreach ($spaces as $space)

ADD
resources/views/spaces/create.blade.php
+@extends('layout')
+
+@section('title', __('actions.create') . ' ' . __('models.space'))
+
+@section('body')
+ <div class="wrapper my-3">
+ <h2 class="mb-3">{{ __('actions.create') }} {{ __('models.space') }}</h2>
+ <div class="box mt-3">
+ <form method="POST" action="{{ route('spaces.store') }}" autocomplete="off">
+ {{ csrf_field() }}
+ <div class="box__section">
+ <div class="input input--small">
+ <label>{{ __('fields.name') }}</label>
+ <input type="text" name="name" />
+ @include('partials.validation_error', ['payload' => 'name'])
+ </div>
+ <div class="input input--small mb-0">
+ <label>{{ __('fields.currency') }}</label>
+ <select name="currency_id">
+ @foreach ($currencies as $currency)
+ <option value="{{ $currency->id }}">{{ $currency->name }}</option>
+ @endforeach
+ </select>
+ @include('partials.validation_error', ['payload' => 'currency_id'])
+ </div>
+ </div>
+ <div class="box__section box__section--highlight row row--right">
+ <div class="row__column row__column--compact row__column--middle">
+ <a href="{{ route('settings.spaces.index') }}">{{ __('actions.cancel') }}</a>
+ </div>
+ <div class="row__column row__column--compact ml-2">
+ <button class="button">{{ __('actions.create') }}</button>
+ </div>
+ </div>
+ </form>
+ </div>
+ </div>
+@endsection

MODIFY
routes/web.php
routes/web.php
@@ -122,6 +122,8 @@ Route::group(['middleware' => ['auth']], function () {
});
Route::name('spaces.')->group(function () {
+ Route::get('/spaces/create', [SpaceController::class, 'create'])->name('create');
+ Route::post('/spaces', [SpaceController::class, 'store'])->name('store');
Route::get('/spaces/{space}', [SpaceController::class, 'show'])->name('show');
Route::get('/spaces/{space}/edit', [SpaceController::class, 'edit'])->name('edit');
Route::post('/spaces/{space}/update', [SpaceController::class, 'update'])->name('update');

Support creation of spaces