some progress
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
|
||||
use App\Models\Book;
|
||||
use App\Models\Category;
|
||||
use App\Models\Summary;
|
||||
use App\Models\Author;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -42,11 +43,13 @@ class IngestController extends Controller
|
||||
// Basic validation (minimal to satisfy requirement)
|
||||
$title = $item['title'] ?? null;
|
||||
$author = $item['author'] ?? null;
|
||||
if (!$title || !$author) {
|
||||
$authorsArr = $item['authors'] ?? null; // optional array of names
|
||||
$hasAuthorInfo = $author || (is_array($authorsArr) && count($authorsArr) > 0);
|
||||
if (!$title || !$hasAuthorInfo) {
|
||||
$results[] = [
|
||||
'index' => $index,
|
||||
'status' => 'error',
|
||||
'message' => 'title and author are required.',
|
||||
'message' => 'title and author/authors are required.',
|
||||
];
|
||||
continue;
|
||||
}
|
||||
@@ -95,19 +98,22 @@ class IngestController extends Controller
|
||||
$bookSlug = Str::slug($item['title']);
|
||||
}
|
||||
|
||||
// Determine a primary author name for backward-compatible matching
|
||||
$primaryAuthorName = $item['author'] ?? (is_array($item['authors'] ?? null) && count($item['authors']) > 0 ? ($item['authors'][0] ?? null) : null);
|
||||
|
||||
$bookQuery = Book::query();
|
||||
if ($bookSlug) {
|
||||
$bookQuery->where('slug', $bookSlug);
|
||||
} else {
|
||||
// Fallback: match by title+author
|
||||
$bookQuery->where('title', $item['title'])->where('author', $item['author']);
|
||||
// Fallback: match by title+author (string). If authors[] provided, use first one.
|
||||
$bookQuery->where('title', $item['title'])->where('author', $primaryAuthorName);
|
||||
}
|
||||
|
||||
$book = $bookQuery->first();
|
||||
$bookData = [
|
||||
'title' => $item['title'] ?? ($book->title ?? null),
|
||||
'slug' => $bookSlug ?? ($book->slug ?? null),
|
||||
'author' => $item['author'] ?? ($book->author ?? null),
|
||||
'author' => $primaryAuthorName ?? ($book->author ?? null),
|
||||
'description' => $item['description'] ?? ($book->description ?? null),
|
||||
'publication_date' => $item['publication_date'] ?? ($book->publication_date ?? null),
|
||||
'cover_image' => $item['cover_image'] ?? ($book->cover_image ?? null),
|
||||
@@ -126,6 +132,41 @@ class IngestController extends Controller
|
||||
$bookStatus = 'created';
|
||||
}
|
||||
|
||||
// Handle authors upsert and pivot sync
|
||||
$names = [];
|
||||
if (!empty($item['author'])) {
|
||||
$names[] = $item['author'];
|
||||
}
|
||||
if (!empty($item['authors']) && is_array($item['authors'])) {
|
||||
$names = array_merge($names, $item['authors']);
|
||||
}
|
||||
$names = array_values(array_unique(array_filter(array_map(function ($n) { return is_string($n) ? trim($n) : null; }, $names))));
|
||||
if (count($names) > 0) {
|
||||
$authorIds = [];
|
||||
foreach ($names as $name) {
|
||||
$slug = Str::slug($name);
|
||||
$author = Author::where('slug', $slug)->first();
|
||||
if (!$author) {
|
||||
$author = Author::where('name', $name)->first();
|
||||
}
|
||||
if ($author) {
|
||||
// ensure slug and name are up to date
|
||||
$author->fill([
|
||||
'name' => $name,
|
||||
'slug' => $author->slug ?: $slug,
|
||||
])->save();
|
||||
} else {
|
||||
$author = Author::create([
|
||||
'name' => $name,
|
||||
'slug' => $slug,
|
||||
]);
|
||||
}
|
||||
$authorIds[] = $author->id;
|
||||
}
|
||||
// Replace current authors with provided set
|
||||
$book->authors()->sync($authorIds);
|
||||
}
|
||||
|
||||
// Handle summary upsert
|
||||
$summaryData = $item['summary'] ?? null;
|
||||
$summaryResult = null;
|
||||
|
||||
Reference in New Issue
Block a user