3 Commits 8eb4f77483 ... b11db23779

Author SHA1 Message Date
  axkuhta b11db23779 Author selector when adding a book 1 year ago
  axkuhta 7b72a3b39e Fix author pointer on books 1 year ago
  axkuhta 537f3d8ac8 Author/book deletion 1 year ago

+ 5 - 0
app/Http/Controllers/AuthorController.php

@@ -42,4 +42,9 @@ class AuthorController extends Controller {
 
 		return view("success");
 	}
+
+	function drop(Author $author) {
+		$author->delete();
+		return view("success");
+	}
 }

+ 12 - 4
app/Http/Controllers/BookController.php

@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
 
 // Фасад DB здесь не используется; здесь используется модель
 use App\Models\Book;
+use App\Models\Author;
 
 class BookController extends Controller {
 	function index() {
@@ -18,7 +19,7 @@ class BookController extends Controller {
 	}
 
 	function add() {
-		return view("add_book_form");
+		return view("add_book_form", ["authors" => Author::all()]);
 	}
 
 	function view(Book $book) {
@@ -36,9 +37,11 @@ class BookController extends Controller {
 			"annotation" => "nullable",
 			"pagecount" => "nullable|numeric",
 			"year" => "nullable|numeric",
-			"isbn" => "nullable"
+			"isbn" => "nullable",
+			"author" => "required|exists:authors,id"
 		], [
-			"name" => "Книга должна иметь название."
+			"name" => "Книга должна иметь название.",
+			"author" => "Книга должна иметь автора."
 		]);
 
 		$arr = $request;
@@ -49,9 +52,14 @@ class BookController extends Controller {
 		$book->pagecount = $arr->pagecount;
 		$book->year = $arr->year;
 		$book->isbn = $arr->isbn;
-		$book->authors = 0;
+		$book->author_id = $arr->author;
 		$book->save();
 
 		return view("success");
 	}
+
+	function drop(Book $book) {
+		$book->delete();
+		return view("success");
+	}
 }

+ 1 - 1
database/migrations/2023_11_02_093534_create_books_table.php

@@ -18,7 +18,7 @@ return new class extends Migration
             $table->string("isbn")->nullable();
             $table->year("year")->nullable();
             $table->smallInteger("pagecount")->unsigned()->nullable();
-            $table->foreignId("authors");
+            $table->foreignId("author_id")->constrained();
             $table->timestamps();
         });
     }

+ 10 - 0
resources/views/add_book_form.blade.php

@@ -55,6 +55,16 @@
 		</label>
 	</div>
 
+	<div>
+		<label>
+			<div>Автор:</div>
+			@include("include.author_selector")
+			@error("author")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
 	<input type="submit">
 </form>
 @endsection

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

@@ -7,6 +7,6 @@
 <div>{{$author->description ?? "N/A"}}</div>
 </p>
 <p>
-<a href="/author/{{ $author->id }}/edit">Редактировать</a>
+<a href="/author/{{ $author->id }}/edit">Редактировать</a> | <a href="/author/{{ $author->id }}/delete">Удалить</a>
 </p>
 @endsection

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

@@ -12,6 +12,6 @@
 <div>Год: {{$book->year ?? "N/A"}}</div>
 </p>
 <p>
-<a href="/book/{{ $book->id }}/edit">Редактировать</a>
+<a href="/book/{{ $book->id }}/edit">Редактировать</a> | <a href="/book/{{ $book->id }}/delete">Удалить</a>
 </p>
 @endsection

+ 8 - 0
resources/views/include/author_selector.blade.php

@@ -0,0 +1,8 @@
+<div class="author-selector">
+@foreach ($authors as $author)
+	<label>
+		<input type="radio" name="author" value="{{ $author->id }}">
+		<span>{{ $author->name }}</span>
+	</label>
+@endforeach
+</div>

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

@@ -61,17 +61,26 @@
 				}
 			}
 
-			label {
+			form > div > label {
 				border-left: 2px solid #D0D0D0;
 				display: block;
 				padding-left: 0.3rem;
 				margin: .3rem 0;
 			}
 
-			label:focus-within {
+			form > div > label:focus-within {
 				border-color: #00FF60;
 			}
 
+			.author-selector {
+				font-size: 22px;
+			}
+
+			.author-selector label {
+				display: block;
+				padding: .2rem 0rem;
+			}
+
 			input, textarea {
 				font-size: inherit;
 				border: 1px solid #D0D0D0;

+ 2 - 0
routes/web.php

@@ -22,6 +22,7 @@ use App\Http\Controllers;
 Route::get('/books', [Controllers\BookController::class, 'index']);
 Route::get('/book/add', [Controllers\BookController::class, 'add']);
 Route::get('/book/{book}', [Controllers\BookController::class, 'view']);
+Route::get('/book/{book}/delete', [Controllers\BookController::class, 'drop']);
 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']);
@@ -30,6 +31,7 @@ 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/{author}', [Controllers\AuthorController::class, 'view']);
+Route::get('/author/{author}/delete', [Controllers\AuthorController::class, 'drop']);
 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']);