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;
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Foundation\Auth\ConfirmsPasswords;
|
||||
|
||||
class ConfirmPasswordController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Confirm Password Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling password confirmations and
|
||||
| uses a simple trait to include the behavior. You're free to explore
|
||||
| this trait and override any functions that require customization.
|
||||
|
|
||||
*/
|
||||
|
||||
use ConfirmsPasswords;
|
||||
|
||||
/**
|
||||
* Where to redirect users when the intended url fails.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = '/home';
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
|
||||
|
||||
class ForgotPasswordController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Password Reset Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling password reset emails and
|
||||
| includes a trait which assists in sending these notifications from
|
||||
| your application to your users. Feel free to explore this trait.
|
||||
|
|
||||
*/
|
||||
|
||||
use SendsPasswordResetEmails;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
||||
|
||||
class LoginController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Login Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller handles authenticating users for the application and
|
||||
| redirecting them to your home screen. The controller uses a trait
|
||||
| to conveniently provide its functionality to your applications.
|
||||
|
|
||||
*/
|
||||
|
||||
use AuthenticatesUsers;
|
||||
|
||||
/**
|
||||
* Where to redirect users after login.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = '/home';
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('guest')->except('logout');
|
||||
$this->middleware('auth')->only('logout');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Auth\RegistersUsers;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class RegisterController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Register Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller handles the registration of new users as well as their
|
||||
| validation and creation. By default this controller uses a trait to
|
||||
| provide this functionality without requiring any additional code.
|
||||
|
|
||||
*/
|
||||
|
||||
use RegistersUsers;
|
||||
|
||||
/**
|
||||
* Where to redirect users after registration.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = '/home';
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('guest');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a validator for an incoming registration request.
|
||||
*
|
||||
* @param array $data
|
||||
* @return \Illuminate\Contracts\Validation\Validator
|
||||
*/
|
||||
protected function validator(array $data)
|
||||
{
|
||||
return Validator::make($data, [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
|
||||
'password' => ['required', 'string', 'min:8', 'confirmed'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new user instance after a valid registration.
|
||||
*
|
||||
* @param array $data
|
||||
* @return \App\Models\User
|
||||
*/
|
||||
protected function create(array $data)
|
||||
{
|
||||
return User::create([
|
||||
'name' => $data['name'],
|
||||
'email' => $data['email'],
|
||||
'password' => Hash::make($data['password']),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Foundation\Auth\ResetsPasswords;
|
||||
|
||||
class ResetPasswordController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Password Reset Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling password reset requests
|
||||
| and uses a simple trait to include this behavior. You're free to
|
||||
| explore this trait and override any methods you wish to tweak.
|
||||
|
|
||||
*/
|
||||
|
||||
use ResetsPasswords;
|
||||
|
||||
/**
|
||||
* Where to redirect users after resetting their password.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = '/home';
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Foundation\Auth\VerifiesEmails;
|
||||
|
||||
class VerificationController extends Controller
|
||||
{
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Email Verification Controller
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This controller is responsible for handling email verification for any
|
||||
| user that recently registered with the application. Emails may also
|
||||
| be re-sent if the user didn't receive the original email message.
|
||||
|
|
||||
*/
|
||||
|
||||
use VerifiesEmails;
|
||||
|
||||
/**
|
||||
* Where to redirect users after verification.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = '/home';
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
$this->middleware('signed')->only('verify');
|
||||
$this->middleware('throttle:6,1')->only('verify', 'resend');
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Book;
|
||||
use App\Models\Category;
|
||||
use App\Models\Author;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
@@ -12,10 +13,67 @@ class BookController extends Controller
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
public function index(Request $request)
|
||||
{
|
||||
$books = Book::with('category')->get();
|
||||
return view('books.index', compact('books'));
|
||||
$query = Book::query()->with(['category', 'authors', 'summary'])->orderByDesc('created_at');
|
||||
|
||||
// Text search
|
||||
if ($search = trim((string) $request->get('q'))) {
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('title', 'like', "%{$search}%")
|
||||
->orWhere('description', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
// Category filter (by id or slug)
|
||||
if ($cat = $request->get('category')) {
|
||||
if (is_numeric($cat)) {
|
||||
$query->where('category_id', (int) $cat);
|
||||
} else {
|
||||
$query->whereHas('category', function ($q) use ($cat) {
|
||||
$q->where('slug', $cat)->orWhere('name', 'like', "%{$cat}%");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Author filter (by id, slug, or name)
|
||||
if ($author = $request->get('author')) {
|
||||
$query->whereHas('authors', function ($q) use ($author) {
|
||||
if (is_numeric($author)) {
|
||||
$q->where('authors.id', (int) $author);
|
||||
} else {
|
||||
$q->where('authors.slug', $author)
|
||||
->orWhere('authors.name', 'like', "%{$author}%");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Publication date range
|
||||
if ($from = $request->get('date_from')) {
|
||||
$query->whereDate('publication_date', '>=', $from);
|
||||
}
|
||||
if ($to = $request->get('date_to')) {
|
||||
$query->whereDate('publication_date', '<=', $to);
|
||||
}
|
||||
|
||||
$books = $query->paginate(12)->withQueryString();
|
||||
|
||||
// For filter dropdowns
|
||||
$categories = Category::orderBy('name')->get();
|
||||
$authors = Author::orderBy('name')->get();
|
||||
|
||||
return view('books.index', [
|
||||
'books' => $books,
|
||||
'categories' => $categories,
|
||||
'authors' => $authors,
|
||||
'filters' => [
|
||||
'q' => $request->get('q'),
|
||||
'category' => $request->get('category'),
|
||||
'author' => $request->get('author'),
|
||||
'date_from' => $request->get('date_from'),
|
||||
'date_to' => $request->get('date_to'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,9 +99,18 @@ class BookController extends Controller
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
]);
|
||||
|
||||
Book::create([
|
||||
// Generate a unique slug for the title
|
||||
$baseSlug = Str::slug($request->title);
|
||||
$slug = $baseSlug;
|
||||
$i = 2;
|
||||
while (Book::where('slug', $slug)->exists()) {
|
||||
$slug = $baseSlug . '-' . $i;
|
||||
$i++;
|
||||
}
|
||||
|
||||
$book = Book::create([
|
||||
'title' => $request->title,
|
||||
'slug' => Str::slug($request->title),
|
||||
'slug' => $slug,
|
||||
'author' => $request->author,
|
||||
'description' => $request->description,
|
||||
'publication_date' => $request->publication_date,
|
||||
@@ -51,6 +118,22 @@ class BookController extends Controller
|
||||
'category_id' => $request->category_id,
|
||||
]);
|
||||
|
||||
// Normalize author: link to authors table via pivot
|
||||
if (!empty($request->author)) {
|
||||
$name = trim($request->author);
|
||||
$slug = Str::slug($name);
|
||||
$author = Author::where('slug', $slug)->first();
|
||||
if (!$author) {
|
||||
$author = Author::where('name', $name)->first();
|
||||
}
|
||||
if ($author) {
|
||||
$author->fill(['name' => $name, 'slug' => $author->slug ?: $slug])->save();
|
||||
} else {
|
||||
$author = Author::create(['name' => $name, 'slug' => $slug]);
|
||||
}
|
||||
$book->authors()->sync([$author->id]);
|
||||
}
|
||||
|
||||
return redirect()->route('books.index')
|
||||
->with('success', 'Book created successfully.');
|
||||
}
|
||||
@@ -60,7 +143,17 @@ class BookController extends Controller
|
||||
*/
|
||||
public function show(Book $book)
|
||||
{
|
||||
return view('books.show', compact('book'));
|
||||
$book->load(['summary', 'category', 'authors', 'reviews.user']);
|
||||
$avgRating = (float) round($book->reviews()->avg('rating') ?? 0, 1);
|
||||
$reviewsCount = (int) $book->reviews()->count();
|
||||
$userReview = auth()->check() ? $book->reviews()->where('user_id', auth()->id())->first() : null;
|
||||
|
||||
return view('books.show', [
|
||||
'book' => $book,
|
||||
'avgRating' => $avgRating,
|
||||
'reviewsCount' => $reviewsCount,
|
||||
'userReview' => $userReview,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,9 +179,30 @@ class BookController extends Controller
|
||||
'category_id' => 'required|exists:categories,id',
|
||||
]);
|
||||
|
||||
// If title changed, compute a unique slug (preserving current slug if still matching)
|
||||
$baseSlug = Str::slug($request->title);
|
||||
$slug = $baseSlug;
|
||||
if ($book->slug !== $baseSlug) {
|
||||
$i = 2;
|
||||
while (Book::where('slug', $slug)->where('id', '!=', $book->id)->exists()) {
|
||||
$slug = $baseSlug . '-' . $i;
|
||||
$i++;
|
||||
}
|
||||
} else {
|
||||
// Ensure no collision with other records even if same base
|
||||
if (Book::where('slug', $baseSlug)->where('id', '!=', $book->id)->exists()) {
|
||||
$i = 2;
|
||||
$slug = $baseSlug . '-' . $i;
|
||||
while (Book::where('slug', $slug)->where('id', '!=', $book->id)->exists()) {
|
||||
$i++;
|
||||
$slug = $baseSlug . '-' . $i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$book->update([
|
||||
'title' => $request->title,
|
||||
'slug' => Str::slug($request->title),
|
||||
'slug' => $slug,
|
||||
'author' => $request->author,
|
||||
'description' => $request->description,
|
||||
'publication_date' => $request->publication_date,
|
||||
@@ -96,6 +210,25 @@ class BookController extends Controller
|
||||
'category_id' => $request->category_id,
|
||||
]);
|
||||
|
||||
// Normalize author: sync pivot to the single provided author
|
||||
if (!empty($request->author)) {
|
||||
$name = trim($request->author);
|
||||
$slug = Str::slug($name);
|
||||
$author = Author::where('slug', $slug)->first();
|
||||
if (!$author) {
|
||||
$author = Author::where('name', $name)->first();
|
||||
}
|
||||
if ($author) {
|
||||
$author->fill(['name' => $name, 'slug' => $author->slug ?: $slug])->save();
|
||||
} else {
|
||||
$author = Author::create(['name' => $name, 'slug' => $slug]);
|
||||
}
|
||||
$book->authors()->sync([$author->id]);
|
||||
} else {
|
||||
// If author field is cleared, detach all author links
|
||||
$book->authors()->detach();
|
||||
}
|
||||
|
||||
return redirect()->route('books.index')
|
||||
->with('success', 'Book updated successfully.');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class ContactController extends Controller
|
||||
{
|
||||
/** Show the contact form */
|
||||
public function show()
|
||||
{
|
||||
return view('static.contact');
|
||||
}
|
||||
|
||||
/** Handle contact form submission and send email */
|
||||
public function send(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'email' => 'required|email',
|
||||
'message' => 'required|string|max:5000',
|
||||
]);
|
||||
|
||||
$to = 'mike@opentextsummaries.com';
|
||||
|
||||
$subject = 'Contact form message from ' . $data['name'];
|
||||
$body = "Name: {$data['name']}\nEmail: {$data['email']}\n\nMessage:\n{$data['message']}";
|
||||
|
||||
// Send using simple raw email (will go to log in local env per .env)
|
||||
Mail::raw($body, function ($message) use ($to, $subject, $data) {
|
||||
$message->to($to)
|
||||
->subject($subject)
|
||||
->replyTo($data['email'], $data['name']);
|
||||
});
|
||||
|
||||
return back()->with('success', 'Thank you! Your message has been sent.');
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,11 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
|
||||
class Controller extends BaseController
|
||||
{
|
||||
//
|
||||
use AuthorizesRequests, ValidatesRequests;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\SummaryReport;
|
||||
use App\Models\SummaryRequest;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the user dashboard with stats.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$user = $request->user();
|
||||
|
||||
// Aggregate counts
|
||||
$readlistCount = $user->readlistBooks()->count();
|
||||
$readlistWithSummaryCount = $user->readlistBooks()->whereHas('summary')->count();
|
||||
$reviewsCount = $user->reviews()->count();
|
||||
$reportsCount = SummaryReport::where('user_id', $user->id)->count();
|
||||
$requestsCount = SummaryRequest::where('user_id', $user->id)->count();
|
||||
|
||||
// Recent activity
|
||||
$latestReadlist = $user->readlistBooks()->with(['authors', 'summary'])->orderByPivot('created_at', 'desc')->limit(5)->get();
|
||||
$latestReviews = $user->reviews()->with('book')->latest()->limit(5)->get();
|
||||
$latestRequests = SummaryRequest::where('user_id', $user->id)->latest()->limit(5)->get();
|
||||
|
||||
return view('home', compact(
|
||||
'readlistCount',
|
||||
'readlistWithSummaryCount',
|
||||
'reviewsCount',
|
||||
'reportsCount',
|
||||
'requestsCount',
|
||||
'latestReadlist',
|
||||
'latestReviews',
|
||||
'latestRequests'
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Book;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class ReadlistController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the user's readlist.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$user = $request->user();
|
||||
$books = $user->readlistBooks()->with(['category', 'authors', 'summary'])->orderByPivot('created_at', 'desc')->paginate(12);
|
||||
|
||||
return view('readlist.index', [
|
||||
'books' => $books,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a book to the readlist.
|
||||
*/
|
||||
public function store(Request $request, Book $book)
|
||||
{
|
||||
$user = $request->user();
|
||||
$user->readlistBooks()->syncWithoutDetaching([$book->id]);
|
||||
|
||||
if ($request->wantsJson()) {
|
||||
return response()->json(['status' => 'added']);
|
||||
}
|
||||
|
||||
return back()->with('success', 'Book added to your readlist.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a book from the readlist.
|
||||
*/
|
||||
public function destroy(Request $request, Book $book)
|
||||
{
|
||||
$user = $request->user();
|
||||
$user->readlistBooks()->detach($book->id);
|
||||
|
||||
if ($request->wantsJson()) {
|
||||
return response()->json(['status' => 'removed']);
|
||||
}
|
||||
|
||||
return back()->with('success', 'Book removed from your readlist.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Book;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
|
||||
class SitemapController extends Controller
|
||||
{
|
||||
/**
|
||||
* Generate the XML sitemap.
|
||||
*/
|
||||
public function index(): Response
|
||||
{
|
||||
$urls = [];
|
||||
|
||||
// Static and index pages
|
||||
$urls[] = [URL::to('/'), now()];
|
||||
$urls[] = [URL::to('/about'), null];
|
||||
$urls[] = [URL::to('/privacy'), null];
|
||||
$urls[] = [URL::to('/terms'), null];
|
||||
$urls[] = [URL::to('/contact'), null];
|
||||
$urls[] = [URL::to('/books'), now()];
|
||||
$urls[] = [URL::to('/categories'), now()];
|
||||
|
||||
// Individual book pages
|
||||
Book::select(['slug', 'updated_at'])->orderBy('updated_at', 'desc')->chunk(500, function ($books) use (&$urls) {
|
||||
foreach ($books as $book) {
|
||||
$urls[] = [URL::to('/books/' . $book->slug), optional($book->updated_at)];
|
||||
}
|
||||
});
|
||||
|
||||
$xml = $this->buildXml($urls);
|
||||
|
||||
return response($xml, 200)->header('Content-Type', 'application/xml');
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a basic XML sitemap string.
|
||||
*
|
||||
* @param array<int, array{0:string,1:\DateTimeInterface|null}> $urls
|
||||
*/
|
||||
protected function buildXml(array $urls): string
|
||||
{
|
||||
$xmlHeader = '<?xml version="1.0" encoding="UTF-8"?>';
|
||||
$urlsetOpen = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
|
||||
$urlsetClose = '</urlset>';
|
||||
|
||||
$body = '';
|
||||
foreach ($urls as [$loc, $lastmod]) {
|
||||
$body .= "\n <url>\n";
|
||||
$body .= ' <loc>' . e($loc) . "</loc>\n";
|
||||
if ($lastmod) {
|
||||
$body .= ' <lastmod>' . $lastmod->format('c') . "</lastmod>\n";
|
||||
}
|
||||
$body .= " </url>";
|
||||
}
|
||||
|
||||
return $xmlHeader . "\n" . $urlsetOpen . $body . "\n" . $urlsetClose . "\n";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
class StaticPageController extends Controller
|
||||
{
|
||||
/** Show the About page. */
|
||||
public function about()
|
||||
{
|
||||
return view('static.about');
|
||||
}
|
||||
|
||||
/** Show the Privacy Policy page. */
|
||||
public function privacy()
|
||||
{
|
||||
return view('static.privacy');
|
||||
}
|
||||
|
||||
/** Show the Terms of Service page. */
|
||||
public function terms()
|
||||
{
|
||||
return view('static.terms');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Book;
|
||||
use App\Models\SummaryReport;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class SummaryReportController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created summary report in storage.
|
||||
*/
|
||||
public function store(Request $request, Book $book)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'reason' => 'required|string|max:100',
|
||||
'details' => 'nullable|string|max:5000',
|
||||
]);
|
||||
|
||||
// Try to prevent duplicate reports from the same user for the same book
|
||||
$report = SummaryReport::firstOrCreate(
|
||||
[
|
||||
'user_id' => $request->user()->id,
|
||||
'book_id' => $book->id,
|
||||
],
|
||||
[
|
||||
'summary_id' => optional($book->summary)->id,
|
||||
'reason' => $validated['reason'],
|
||||
'details' => $validated['details'] ?? null,
|
||||
'status' => 'open',
|
||||
]
|
||||
);
|
||||
|
||||
if (!$report->wasRecentlyCreated) {
|
||||
// If already exists, update reason/details in case user adds more context
|
||||
$report->update([
|
||||
'reason' => $validated['reason'],
|
||||
'details' => $validated['details'] ?? $report->details,
|
||||
'summary_id' => optional($book->summary)->id,
|
||||
]);
|
||||
}
|
||||
|
||||
return back()->with('success', 'Thanks! Your report has been submitted. We will review it soon.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\SummaryRequest;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class SummaryRequestController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
/** List the current user's requests */
|
||||
public function index(Request $request)
|
||||
{
|
||||
$requests = SummaryRequest::where('user_id', $request->user()->id)
|
||||
->orderByDesc('created_at')
|
||||
->paginate(12);
|
||||
|
||||
return view('requests.index', [
|
||||
'requests' => $requests,
|
||||
]);
|
||||
}
|
||||
|
||||
/** Show the create form */
|
||||
public function create(Request $request)
|
||||
{
|
||||
$prefill = [
|
||||
'gutenberg' => $request->query('gutenberg'),
|
||||
'title' => $request->query('title'),
|
||||
];
|
||||
return view('requests.create', $prefill);
|
||||
}
|
||||
|
||||
/** Store the request */
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'gutenberg' => 'required|string|max:255', // can be URL or ID
|
||||
'title' => 'nullable|string|max:255',
|
||||
'notes' => 'nullable|string|max:2000',
|
||||
]);
|
||||
|
||||
$gutenbergId = $this->parseGutenbergId($data['gutenberg']);
|
||||
if (!$gutenbergId) {
|
||||
return back()->withErrors(['gutenberg' => 'Please provide a valid Project Gutenberg URL or numeric ID.'])->withInput();
|
||||
}
|
||||
|
||||
$requestModel = SummaryRequest::firstOrCreate([
|
||||
'user_id' => $request->user()->id,
|
||||
'gutenberg_id' => $gutenbergId,
|
||||
], [
|
||||
'title' => $data['title'] ?? null,
|
||||
'notes' => $data['notes'] ?? null,
|
||||
'status' => 'pending',
|
||||
]);
|
||||
|
||||
// If already exists, update title/notes to latest provided
|
||||
if (!$requestModel->wasRecentlyCreated) {
|
||||
$requestModel->update([
|
||||
'title' => $data['title'] ?? $requestModel->title,
|
||||
'notes' => $data['notes'] ?? $requestModel->notes,
|
||||
]);
|
||||
}
|
||||
|
||||
return redirect()->route('requests.index')->with('success', 'Your summary request has been submitted. We will process it soon.');
|
||||
}
|
||||
|
||||
private function parseGutenbergId(string $input): ?int
|
||||
{
|
||||
$input = trim($input);
|
||||
// If pure number
|
||||
if (ctype_digit($input)) {
|
||||
return (int) $input;
|
||||
}
|
||||
|
||||
// Try to extract from URL
|
||||
$patterns = [
|
||||
'/gutenberg\.org\/(?:ebooks|etext)\/(\d+)/i', // https://www.gutenberg.org/ebooks/1342
|
||||
'/gutenberg\.org\/(?:files)\/(\d+)\//i', // https://www.gutenberg.org/files/1342/1342-h/1342-h.htm
|
||||
];
|
||||
foreach ($patterns as $pattern) {
|
||||
if (preg_match($pattern, $input, $m)) {
|
||||
return (int) $m[1];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Book;
|
||||
use App\Models\SummaryReview;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class SummaryReviewController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store or update a user's review for a book summary.
|
||||
*/
|
||||
public function store(Request $request, Book $book)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'rating' => 'required|integer|min:1|max:5',
|
||||
'comment' => 'nullable|string|max:2000',
|
||||
]);
|
||||
|
||||
$review = SummaryReview::updateOrCreate(
|
||||
[
|
||||
'user_id' => $request->user()->id,
|
||||
'book_id' => $book->id,
|
||||
],
|
||||
[
|
||||
'summary_id' => optional($book->summary)->id,
|
||||
'rating' => $data['rating'],
|
||||
'comment' => $data['comment'] ?? null,
|
||||
]
|
||||
);
|
||||
|
||||
return back()->with('success', $review->wasRecentlyCreated ? 'Thanks for your review!' : 'Your review has been updated.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified review from storage.
|
||||
*/
|
||||
public function destroy(Request $request, SummaryReview $review)
|
||||
{
|
||||
$this->authorizeAction($request, $review);
|
||||
$review->delete();
|
||||
return back()->with('success', 'Your review has been deleted.');
|
||||
}
|
||||
|
||||
protected function authorizeAction(Request $request, SummaryReview $review): void
|
||||
{
|
||||
abort_unless($review->user_id === $request->user()->id, 403, 'Unauthorized');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Services\GoatCounterService;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class GoatCounterMiddleware
|
||||
{
|
||||
protected GoatCounterService $goatCounter;
|
||||
|
||||
public function __construct(GoatCounterService $goatCounter)
|
||||
{
|
||||
$this->goatCounter = $goatCounter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*/
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
$response = $next($request);
|
||||
|
||||
// Only track "real" HTML page requests:
|
||||
if (
|
||||
$request->isMethod('GET') &&
|
||||
!$request->ajax() &&
|
||||
$this->isTrackable($request)
|
||||
) {
|
||||
$this->goatCounter->trackPageview(
|
||||
'/' . ltrim($request->path(), '/'),
|
||||
$request->route()?->getName() ?? ucfirst(str_replace('/', ' ', $request->path())),
|
||||
$request->headers->get('referer')
|
||||
);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
protected function isTrackable(Request $request): bool
|
||||
{
|
||||
$path = $request->path();
|
||||
|
||||
// Ignore file extensions (css, js, images, fonts, etc.)
|
||||
if (preg_match('/\.(css|js|png|jpe?g|gif|svg|ico|woff2?|ttf|map)$/i', $path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Optionally skip API routes
|
||||
if ($request->is('api/*')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Author extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'bio',
|
||||
];
|
||||
|
||||
/**
|
||||
* The books that belong to the author.
|
||||
*/
|
||||
public function books()
|
||||
{
|
||||
return $this->belongsToMany(Book::class, 'author_book')->withTimestamps();
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,14 @@ class Book extends Model
|
||||
'category_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* Use slug for route model binding and URL generation.
|
||||
*/
|
||||
public function getRouteKeyName(): string
|
||||
{
|
||||
return 'slug';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the category that owns the book.
|
||||
*/
|
||||
@@ -31,4 +39,36 @@ class Book extends Model
|
||||
{
|
||||
return $this->hasOne(Summary::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* The authors that belong to the book.
|
||||
*/
|
||||
public function authors()
|
||||
{
|
||||
return $this->belongsToMany(Author::class, 'author_book')->withTimestamps();
|
||||
}
|
||||
|
||||
/**
|
||||
* Users who saved this book to their readlist.
|
||||
*/
|
||||
public function savedByUsers()
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'readlist_items')->withTimestamps();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports submitted for this book's summary.
|
||||
*/
|
||||
public function reports()
|
||||
{
|
||||
return $this->hasMany(SummaryReport::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reviews left by users for this book's summary.
|
||||
*/
|
||||
public function reviews()
|
||||
{
|
||||
return $this->hasMany(SummaryReview::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class SummaryReport extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'book_id',
|
||||
'summary_id',
|
||||
'reason',
|
||||
'details',
|
||||
'status',
|
||||
];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function book()
|
||||
{
|
||||
return $this->belongsTo(Book::class);
|
||||
}
|
||||
|
||||
public function summary()
|
||||
{
|
||||
return $this->belongsTo(Summary::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class SummaryRequest extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'gutenberg_id',
|
||||
'title',
|
||||
'notes',
|
||||
'status',
|
||||
];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class SummaryReview extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'book_id',
|
||||
'summary_id',
|
||||
'rating',
|
||||
'comment',
|
||||
];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function book()
|
||||
{
|
||||
return $this->belongsTo(Book::class);
|
||||
}
|
||||
|
||||
public function summary()
|
||||
{
|
||||
return $this->belongsTo(Summary::class);
|
||||
}
|
||||
}
|
||||
@@ -45,4 +45,20 @@ class User extends Authenticatable
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Books saved by the user in their readlist.
|
||||
*/
|
||||
public function readlistBooks()
|
||||
{
|
||||
return $this->belongsToMany(Book::class, 'readlist_items')->withTimestamps();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reviews authored by the user.
|
||||
*/
|
||||
public function reviews()
|
||||
{
|
||||
return $this->hasMany(SummaryReview::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class GoatCounterService
|
||||
{
|
||||
protected string $endpoint;
|
||||
protected string $token;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->endpoint = 'http://gc.c0rrup7.net/api/v0/count';
|
||||
$this->token = '13nus05lo9d441ihqhk5d3w15s1hoi0h9pr013j1jwqyebgma8un'; // store in config/services.php
|
||||
}
|
||||
|
||||
/**
|
||||
* Track a single pageview
|
||||
*/
|
||||
public function trackPageview(string $path, string $title = null, string $referrer = null): array
|
||||
{
|
||||
return $this->sendHits([[
|
||||
'path' => $path,
|
||||
'title' => $title,
|
||||
'ref' => $referrer,
|
||||
]]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Track a custom event
|
||||
*/
|
||||
public function trackEvent(string $event, string $title = null, string $referrer = null): array
|
||||
{
|
||||
return $this->sendHits([[
|
||||
'event' => $event,
|
||||
'title' => $title,
|
||||
'ref' => $referrer,
|
||||
]]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send multiple hits in one request
|
||||
*/
|
||||
public function sendHits(array $hits): array
|
||||
{
|
||||
$response = Http::withToken($this->token)
|
||||
->acceptJson()
|
||||
->asJson()
|
||||
->post($this->endpoint, [
|
||||
'no_sessions' => true, // don’t start session cookies from backend
|
||||
'hits' => $hits,
|
||||
]);
|
||||
|
||||
return [
|
||||
'status' => $response->status(),
|
||||
'response' => $response->json(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user