1
0

6 Commits dd2dff261f ... 8eb4f77483

Autor SHA1 Mensagem Data
  axkuhta 8eb4f77483 Author editing há 1 ano atrás
  axkuhta 9f1fd9ea13 Book editing há 1 ano atrás
  axkuhta 31596aa8be Individual author pages há 1 ano atrás
  axkuhta 815aa806cb Add book/{id} page há 1 ano atrás
  axkuhta 30df864bf1 Link individual book pages from /books há 1 ano atrás
  axkuhta 1b79ce8a7b Fancier tables há 1 ano atrás

+ 8 - 3
app/Http/Controllers/AuthorController.php

@@ -16,12 +16,17 @@ class AuthorController extends Controller {
 		return view("add_author_form");
 	}
 
-	function view($id) {
-		return ["id" => $id];
+	function view(Author $author) {
+		return view("author", ["author" => $author]);
+	}
+
+	function edit(Author $author) {
+		return view("edit_author_form", ["author" => $author]);
 	}
 
 	function store(Request $request) {
 		$request->validate([
+			"id" => "nullable|exists:authors",
 			"name" => "required",
 			"description" => "nullable"
 		], [
@@ -30,7 +35,7 @@ class AuthorController extends Controller {
 
 		$arr = $request;
 
-		$author = new Author;
+		$author = Author::find($arr->id) ?? new Author;
 		$author->name = $arr->name;
 		$author->description = $arr->description;
 		$author->save();

+ 8 - 3
app/Http/Controllers/BookController.php

@@ -21,12 +21,17 @@ class BookController extends Controller {
 		return view("add_book_form");
 	}
 
-	function view($id) {
-		return ["id" => $id];
+	function view(Book $book) {
+		return view("book", ["book" => $book]);
+	}
+
+	function edit(Book $book) {
+		return view("edit_book_form", ["book" => $book]);
 	}
 
 	function store(Request $request) {
 		$request->validate([
+			"id" => "nullable|exists:books",
 			"name" => "required",
 			"annotation" => "nullable",
 			"pagecount" => "nullable|numeric",
@@ -38,7 +43,7 @@ class BookController extends Controller {
 
 		$arr = $request;
 
-		$book = new Book;
+		$book = Book::find($arr->id) ?? new Book;
 		$book->name = $arr->name;
 		$book->annotation = $arr->annotation;
 		$book->pagecount = $arr->pagecount;

+ 12 - 0
resources/views/author.blade.php

@@ -0,0 +1,12 @@
+@extends("layouts.app")
+
+@section("content")
+<h1>{{$author->name}}</h1>
+<p>
+<div>Описание автора:</div>
+<div>{{$author->description ?? "N/A"}}</div>
+</p>
+<p>
+<a href="/author/{{ $author->id }}/edit">Редактировать</a>
+</p>
+@endsection

+ 1 - 1
resources/views/authors.blade.php

@@ -9,7 +9,7 @@
 </tr>
 @foreach ($rows as $row)
 	<tr>
-		<td>{{$row->name}}</td>
+		<td><a href="/author/{{$row->id}}">{{$row->name}}</a></td>
 		<td>{{$row->description}}</td>
 	</tr>
 @endforeach

+ 17 - 0
resources/views/book.blade.php

@@ -0,0 +1,17 @@
+@extends("layouts.app")
+
+@section("content")
+<h1>{{$book->name}}</h1>
+<p>
+<div>Аннотация:</div>
+<div>{{$book->annotation ?? "N/A"}}</div>
+</p>
+<p>
+<div>ISBN: {{$book->isbn ?? "N/A"}}</div>
+<div>Количество страниц: {{$book->pagecount ?? "N/A"}}</div>
+<div>Год: {{$book->year ?? "N/A"}}</div>
+</p>
+<p>
+<a href="/book/{{ $book->id }}/edit">Редактировать</a>
+</p>
+@endsection

+ 1 - 1
resources/views/books.blade.php

@@ -12,7 +12,7 @@
 </tr>
 @foreach ($rows as $row)
 	<tr>
-		<td>{{$row->name}}</td>
+		<td><a href="/book/{{$row->id}}">{{$row->name}}</a></td>
 		<td>{{$row->annotation}}</td>
 		<td>{{$row->isbn}}</td>
 		<td>{{$row->year}}</td>

+ 32 - 0
resources/views/edit_author_form.blade.php

@@ -0,0 +1,32 @@
+@extends("layouts.app")
+
+@section("content")
+<h1>Редактирование автора</h1>
+<form method="POST" action="">
+	@csrf
+
+	<input type="hidden" name="id" value="{{ $author->id }}">
+
+	<div>
+		<label>
+			<div>Имя:</div>
+			<input type="text" name="name" placeholder="Имя автора..." value="{{ old("name") ?? $author->name }}">
+			@error("name")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
+	<div>
+		<label>
+			<div>Описание:</div>
+			<textarea name="description" placeholder="Описание...">{{ old("description") ?? $author->description }}</textarea>
+			@error("description")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
+	<input type="submit">
+</form>
+@endsection

+ 62 - 0
resources/views/edit_book_form.blade.php

@@ -0,0 +1,62 @@
+@extends("layouts.app")
+
+@section("content")
+<h1>Редактирование книги</h1>
+<form method="POST" action="">
+	@csrf
+
+	<input type="hidden" name="id" value="{{ $book->id }}">
+
+	<div>
+		<label>
+			<div>Название:</div>
+			<input type="text" name="name" placeholder="Название книги..." value="{{ old("name") ?? $book->name }}">
+			@error("name")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
+	<div>
+		<label>
+			<div>Аннотация:</div>
+			<textarea name="annotation" placeholder="Аннотация...">{{ old("annotation") ?? $book->annotation }}</textarea>
+			@error("annotation")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
+	<div>
+		<label>
+			<div>Количество страниц:</div>
+			<input type="number" name="pagecount" placeholder="Количество страниц..." value="{{ old("pagecount") ?? $book->pagecount }}">
+			@error("pagecount")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
+	<div>
+		<label>
+			<div>Год:</div>
+			<input type="year" name="year" placeholder="Год..." value="{{ old("year") ?? $book->year }}">
+			@error("year")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
+	<div>
+		<label>
+			<div>ISBN:</div>
+			<input type="text" name="isbn" placeholder="ISBN..." value="{{ old("ISBN") ?? $book->isbn }}">
+			@error("isbn")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
+	<input type="submit">
+</form>
+@endsection

+ 9 - 2
resources/views/layouts/app.blade.php

@@ -90,8 +90,15 @@
 			}
 
 			td, th {
-				border: 1px solid #D0D0D0;
-				padding: .2rem .5rem;
+				padding: .2rem 1rem;
+			}
+
+			th {
+				border-bottom: 1px solid #D0D0D0;
+			}
+
+			td {
+				border-top: 1px solid #707070;
 			}
 		</style>
 	</head>

+ 6 - 2
routes/web.php

@@ -21,11 +21,15 @@ use App\Http\Controllers;
 // Книги
 Route::get('/books', [Controllers\BookController::class, 'index']);
 Route::get('/book/add', [Controllers\BookController::class, 'add']);
-Route::get('/book/{id}', [Controllers\BookController::class, 'view']);
+Route::get('/book/{book}', [Controllers\BookController::class, 'view']);
+Route::get('/book/{book}/edit', [Controllers\BookController::class, 'edit']);
+Route::post('/book/{book}/edit', [Controllers\BookController::class, 'store']);
 Route::post('/book/add', [Controllers\BookController::class, 'store']);
 
 // Авторы
 Route::get('/authors', [Controllers\AuthorController::class, 'index']);
 Route::get('/author/add', [Controllers\AuthorController::class, 'add']);
-Route::get('/author/{id}', [Controllers\AuthorController::class, 'view']);
+Route::get('/author/{author}', [Controllers\AuthorController::class, 'view']);
+Route::get('/author/{author}/edit', [Controllers\AuthorController::class, 'edit']);
+Route::post('/author/{author}/edit', [Controllers\AuthorController::class, 'store']);
 Route::post('/author/add', [Controllers\AuthorController::class, 'store']);