Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JIRA-002 Implemented Book reordering #2

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 50 additions & 57 deletions app/Http/Controllers/BookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,39 @@

namespace App\Http\Controllers;

use App\Http\Requests\MoveBookRequest;
use App\Http\Requests\StoreBookRequest;
use App\Http\Requests\UpdateBookRequest;
use App\Models\Book;
use App\Models\Publisher;
use App\Models\Writer;
use Illuminate\Http\Request;
use App\Services\BookService;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;

class BookController extends Controller
{
public function index()

public function __construct(private BookService $bookService)
{
$books = Book::all();
}

/**
* @return View
*/
public function index(): View
{
$books = $this->bookService->getBooksSorted();

return view('books.index', compact('books'));
}

/**
* Show the form for creating a new book.
*
* @return \Illuminate\View\View
* @return View
*/
public function create()
public function create(): View
{
$writers = Writer::all();
$publishers = Publisher::all();
Expand All @@ -31,43 +45,23 @@ public function create()
/**
* Store a newly created book in the database.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
* @param StoreBookRequest $request
* @return RedirectResponse
*/
public function store(Request $request)
public function store(StoreBookRequest $request): RedirectResponse
{
$request->validate([
'title' => 'required|string|max:255',
'ISBN' => 'required|string|max:255',
'publication_year' => 'required|integer|min:1900|max:' . date('Y'),
'price' => 'required|numeric|min:0',
'genre' => 'required|string|max:255',
'subgenre' => 'required|string|max:255',
'writer_id' => 'required|exists:writers,id',
'publisher_id' => 'required|exists:publishers,id',
]);

Book::create([
'title' => $request->input('title'),
'ISBN' => $request->input('ISBN'),
'publication_year' => $request->input('publication_year'),
'price' => $request->input('price'),
'genre' => $request->input('genre'),
'subgenre' => $request->input('subgenre'),
'writer_id' => $request->input('writer_id'),
'publisher_id' => $request->input('publisher_id'),
]);
$this->bookService->createBook($request);

return redirect()->route('books.index');
}

/**
* Show the form for editing the specified book.
*
* @param \App\Models\Book $book
* @return \Illuminate\View\View
* @param Book $book
* @return View
*/
public function edit(Book $book)
public function edit(Book $book): View
{
$writers = Writer::all();
$publishers = Publisher::all();
Expand All @@ -78,33 +72,32 @@ public function edit(Book $book)
/**
* Update the specified book in the database.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Book $book
* @return \Illuminate\Http\RedirectResponse
* @param UpdateBookRequest $request
* @param Book $book
* @return RedirectResponse
*/
public function update(Request $request, Book $book)
public function update(UpdateBookRequest $request, Book $book): RedirectResponse
{
$request->validate([
'title' => 'required|string|max:255',
'ISBN' => 'required|string|max:255',
'publication_year' => 'required|integer|min:1900|max:' . date('Y'),
'price' => 'required|numeric|min:0',
'genre' => 'required|string|max:255',
'subgenre' => 'required|string|max:255',
'writer_id' => 'required|exists:writers,id',
'publisher_id' => 'required|exists:publishers,id',
]);

$book->update([
'title' => $request->input('title'),
'ISBN' => $request->input('ISBN'),
'publication_year' => $request->input('publication_year'),
'price' => $request->input('price'),
'genre' => $request->input('genre'),
'subgenre' => $request->input('subgenre'),
'writer_id' => $request->input('writer_id'),
'publisher_id' => $request->input('publisher_id'),
]);
$this->bookService->updateBook($request, $book);

return redirect()->route('books.index');
}

/**
* Reorder the books.
*
* @param MoveBookRequest $request
* @param Book $book
*
* @return RedirectResponse
*/
public function move(MoveBookRequest $request, Book $book): RedirectResponse
{
if ($book->stock_amount === 0) {
return back();
}

$this->bookService->moveAndReorderBooks($request, $book);

return redirect()->route('books.index');
}
Expand Down
18 changes: 10 additions & 8 deletions app/Http/Controllers/PublisherController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace App\Http\Controllers;

use App\Models\Publisher;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class PublisherController extends Controller
{
Expand All @@ -16,7 +18,7 @@ public function index()
/**
* Show the form for creating a new publisher.
*
* @return \Illuminate\View\View
* @return View
*/
public function create()
{
Expand All @@ -26,8 +28,8 @@ public function create()
/**
* Store a newly created publisher in the database.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
* @param Request $request
* @return RedirectResponse
*/
public function store(Request $request)
{
Expand All @@ -47,8 +49,8 @@ public function store(Request $request)
/**
* Show the form for editing the specified publisher.
*
* @param \App\Models\Publisher $publisher
* @return \Illuminate\View\View
* @param Publisher $publisher
* @return View
*/
public function edit(Publisher $publisher)
{
Expand All @@ -58,9 +60,9 @@ public function edit(Publisher $publisher)
/**
* Update the specified publisher in the database.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Publisher $publisher
* @return \Illuminate\Http\RedirectResponse
* @param Request $request
* @param Publisher $publisher
* @return RedirectResponse
*/
public function update(Request $request, Publisher $publisher)
{
Expand Down
18 changes: 10 additions & 8 deletions app/Http/Controllers/WriterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace App\Http\Controllers;

use App\Models\Writer;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class WriterController extends Controller
{
Expand All @@ -16,7 +18,7 @@ public function index()
/**
* Show the form for creating a new writer.
*
* @return \Illuminate\View\View
* @return View
*/
public function create()
{
Expand All @@ -26,8 +28,8 @@ public function create()
/**
* Store a newly created writer in the database.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
* @param Request $request
* @return RedirectResponse
*/
public function store(Request $request)
{
Expand All @@ -47,8 +49,8 @@ public function store(Request $request)
/**
* Show the form for editing the specified writer.
*
* @param \App\Models\Writer $writer
* @return \Illuminate\View\View
* @param Writer $writer
* @return View
*/
public function edit(Writer $writer)
{
Expand All @@ -58,9 +60,9 @@ public function edit(Writer $writer)
/**
* Update the specified writer in the database.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Writer $writer
* @return \Illuminate\Http\RedirectResponse
* @param Request $request
* @param Writer $writer
* @return RedirectResponse
*/
public function update(Request $request, Writer $writer)
{
Expand Down
35 changes: 35 additions & 0 deletions app/Http/Requests/MoveBookRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

/**
* @property string $up
* @property string $down
*/
class MoveBookRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'up' => 'nullable|integer|min:0|prohibits:down',
'down' => 'nullable|integer|min:0|prohibits:up',
];
}
}
50 changes: 50 additions & 0 deletions app/Http/Requests/StoreBookRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

/**
* @property string $title
* @property string $ISBN
* @property int $publication_year
* @property float $price
* @property string $genre
* @property string $subgenre
* @property int $sort_order
* @property int $stock_amount
* @property int $writer_id
* @property int $publisher_id
*/
class StoreBookRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'title' => 'required|string|max:255',
'ISBN' => 'required|string|max:255',
'publication_year' => 'required|integer|min:1900|max:' . date('Y'),
'price' => 'required|numeric|min:0',
'genre' => 'required|string|max:255',
'stock_amount' => 'required|integer|min:0',
'subgenre' => 'required|string|max:255',
'writer_id' => 'required|exists:writers,id',
'publisher_id' => 'required|exists:publishers,id',
];
}
}
50 changes: 50 additions & 0 deletions app/Http/Requests/UpdateBookRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

/**
* @property string $title
* @property string $ISBN
* @property int $publication_year
* @property float $price
* @property string $genre
* @property string $subgenre
* @property int $sort_order
* @property int $stock_amount
* @property int $writer_id
* @property int $publisher_id
*/
class UpdateBookRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'title' => 'required|string|max:255',
'ISBN' => 'required|string|max:255',
'publication_year' => 'required|integer|min:1900|max:' . date('Y'),
'price' => 'required|numeric|min:0',
'genre' => 'required|string|max:255',
'subgenre' => 'required|string|max:255',
'stock_amount' => 'required|integer|min:0',
'writer_id' => 'required|exists:writers,id',
'publisher_id' => 'required|exists:publishers,id',
];
}
}
Loading