A ton of progress, search start
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@ -4,15 +4,13 @@ namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class BookController extends Controller
|
||||
{
|
||||
class BookController extends Controller {
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
public function index() {
|
||||
//
|
||||
}
|
||||
|
||||
@ -21,64 +19,58 @@ class BookController extends Controller
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
public function create() {
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
public function store(Request $request) {
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
public function show($id) {
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
public function edit($id) {
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
public function update(Request $request, $id) {
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
public function destroy($id) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Location;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class LocationController extends Controller {
|
||||
@ -37,10 +38,16 @@ class LocationController extends Controller {
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return mixed
|
||||
*/
|
||||
public function show($id) {
|
||||
//
|
||||
$location = Location::find($id);
|
||||
|
||||
if (!$location) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
return view('location', [ 'location' => $location ]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2,12 +2,73 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Author;
|
||||
use App\Models\Book;
|
||||
use App\Models\Location;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class MainController extends Controller {
|
||||
public function index() {
|
||||
return view('welcome');
|
||||
$rows = Book::paginate(15);
|
||||
|
||||
return view('index', [ 'rows' => $rows ]);
|
||||
}
|
||||
|
||||
public function add() {
|
||||
return view('add');
|
||||
public function search(Request $request) {
|
||||
$this->validate($request, [
|
||||
'query' => [ 'required' ]
|
||||
]);
|
||||
|
||||
$rows = Book::where('name', 'LIKE', '%' . $request->get('query') . '%')->paginate(15);
|
||||
|
||||
return view('index', [ 'rows' => $rows ]);
|
||||
}
|
||||
|
||||
public function add(Request $request) {
|
||||
return view('add', [
|
||||
'old' => array_filter($request->old('books', []), function($item) {
|
||||
return !empty($item['barcode']);
|
||||
})
|
||||
]);
|
||||
}
|
||||
|
||||
public function save(Request $request) {
|
||||
$this->validate($request, [
|
||||
'location' => [ 'required' ]
|
||||
]);
|
||||
|
||||
$input = array_filter($request->get('books'), function($item) {
|
||||
return !empty($item['barcode']) && !empty($item['name']);
|
||||
});
|
||||
|
||||
$this->getValidationFactory()->make($input, [
|
||||
'*.barcode' => [ 'required' ],
|
||||
'*.name' => [ 'required' ]
|
||||
]);
|
||||
|
||||
$location = Location::firstOrCreate([
|
||||
'name' => trim($request->get('location'))
|
||||
]);
|
||||
|
||||
foreach ($input as $item) {
|
||||
$book = Book::create([
|
||||
'location_id' => $location->id,
|
||||
'barcode' => $item['barcode'],
|
||||
'name' => $item['name']
|
||||
]);
|
||||
|
||||
$authors = [];
|
||||
|
||||
foreach (Arr::get($item, 'authors') as $author) {
|
||||
$authors[] = Author::firstOrCreate([
|
||||
'name' => $author
|
||||
]);
|
||||
}
|
||||
|
||||
$authors = array_map(function($author) { return $author->id; }, $authors);
|
||||
|
||||
$book->authors()->attach($authors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,9 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Author extends Authenticatable {
|
||||
class Author extends Model {
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
|
@ -2,9 +2,15 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use App\Services\Search\BookConfigurator;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use ScoutElastic\Searchable;
|
||||
|
||||
class Book extends Authenticatable {
|
||||
class Book extends Model {
|
||||
|
||||
use Searchable;
|
||||
|
||||
protected $indexConfigurator = BookConfigurator::class;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
@ -12,14 +18,28 @@ class Book extends Authenticatable {
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'location_id', 'barcode', 'name',
|
||||
];
|
||||
|
||||
public function author() {
|
||||
return $this->hasMany(Author::class);
|
||||
public function authors() {
|
||||
return $this->belongsToMany(Author::class, 'book_authors');
|
||||
}
|
||||
|
||||
public function location() {
|
||||
return $this->belongsTo(Location::class);
|
||||
}
|
||||
|
||||
protected $mapping = [
|
||||
'properties' => [
|
||||
'name' => [
|
||||
'type' => 'text',
|
||||
// Also you can configure multi-fields, more details you can find here https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html
|
||||
'fields' => [
|
||||
'raw' => [
|
||||
'type' => 'keyword',
|
||||
]
|
||||
]
|
||||
],
|
||||
]
|
||||
];
|
||||
}
|
||||
|
@ -2,9 +2,9 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Location extends Authenticatable {
|
||||
class Location extends Model {
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
|
22
app/Services/Search/BookConfigurator.php
Normal file
22
app/Services/Search/BookConfigurator.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
namespace App\Services\Search;
|
||||
|
||||
use ScoutElastic\IndexConfigurator;
|
||||
|
||||
class BookConfigurator extends IndexConfigurator
|
||||
{
|
||||
// It's not obligatory to determine name. By default it'll be a snaked class name without `IndexConfigurator` part.
|
||||
protected $name = 'books';
|
||||
|
||||
// You can specify any settings you want, for example, analyzers.
|
||||
protected $settings = [
|
||||
'analysis' => [
|
||||
'analyzer' => [
|
||||
'es_std' => [
|
||||
'type' => 'standard',
|
||||
'stopwords' => '_spanish_'
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
Reference in New Issue
Block a user