3.0: room controller. Index rooms, create a new room.

This commit is contained in:
Romano Schoonheim 2021-01-07 10:49:52 -08:00
parent f8c1421bdd
commit dfa0b6505d
2 changed files with 49 additions and 1 deletions

View File

@ -2,11 +2,16 @@
namespace App\Domain\Room\Repositories;
use App\Models\Room;
/**
* Class RoomRepository
* @package App\Domain\Room\Repositories
*/
class RoomRepository
{
public function all()
{
return Room::all();
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace App\Http\Controllers;
use App\Domain\Room\Factories\RoomFactory;
use App\Domain\Room\Repositories\RoomRepository;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class RoomController extends Controller
{
private RoomRepository $roomRepository;
public function __construct(RoomRepository $roomRepository)
{
$this->roomRepository = $roomRepository;
}
public function index(): JsonResponse
{
$rooms = $this->roomRepository->all();
return response()->json(
$rooms->toArray()
);
}
public function store(RoomFactory $roomFactory): JsonResponse
{
$this->validate(request(), [
'name' => 'required|string|unique:rooms,name'
]);
$room = $roomFactory->create(
request()->post('name')
);
return response()->json(
$room->toArray()
);
}
}