Browse Source

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

3.0
Romano Schoonheim 1 year ago
parent
commit
dfa0b6505d
2 changed files with 49 additions and 1 deletions
  1. +6
    -1
      app/Domain/Room/Repositories/RoomRepository.php
  2. +43
    -0
      app/Http/Controllers/RoomController.php

+ 6
- 1
app/Domain/Room/Repositories/RoomRepository.php 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();
}
}

+ 43
- 0
app/Http/Controllers/RoomController.php 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()
);
}
}

Loading…
Cancel
Save