Skip to content

Commit

Permalink
feat: stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardotglobal committed Jul 19, 2024
1 parent 48cfbb1 commit c6c231f
Show file tree
Hide file tree
Showing 25 changed files with 1,086 additions and 250 deletions.
3 changes: 1 addition & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ root = true
charset = utf-8
end_of_line = lf
indent_size = 4
ij_continuation_indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
Expand All @@ -14,5 +15,3 @@ trim_trailing_whitespace = false
[*.{yml,yaml}]
indent_size = 2

[docker-compose.yml]
indent_size = 4
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ yarn-error.log
!/storage/framework/testing
!/storage/framework/views
bun.lockb
data
public/content
40 changes: 40 additions & 0 deletions app/Http/Views/SeriesViewProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Views;

use App\Http\Resources\BookResource;
use App\Interfaces\ViewProvider;
use App\Models\Book;
use App\Models\Series;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class SeriesViewProvider extends ViewProvider
{
protected ?string $view = "books";
private Series $series;

public function __construct(int $id)
{
$series = Series::find($id);

if ($series === null) {
throw new \RuntimeException("Shelf with id $id not found");
}

$this->series = $series;
}

public function toArray(Request $request): array
{
$books = Book::whereHas("serieses", function (Builder $query) {
$query->where("series.id", "=", $this->series->id);
})
->get();

return [
"books" => BookResource::collection($books)->toArray($request),
"title" => "Books in " . $this->series->name,
];
}
}
39 changes: 39 additions & 0 deletions app/Http/Views/SeriesesViewProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Http\Views;

use App\Interfaces\ViewProvider;
use App\Models\Book;
use App\Models\Series;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;

class SeriesesViewProvider extends ViewProvider
{
protected ?string $view = "serieses";

public function toArray(Request $request): array
{
return [
"serieses" => Series::all()
->map(function (Series $series) {
$book = Book::whereHas("serieses", function (Builder $query) use ($series) {
$query->where("series.id", "=", $series->id);
})
->first();

return [
"authors" => $book->authors
->map(fn($author) => $author->name)
->implode(", "),

"name" => $series->name,
"id" => $series->id,
"cover" => "/content/$book->path/cover.jpg",
];
})
->toArray()
];
}
}
40 changes: 40 additions & 0 deletions app/Http/Views/ShelfViewProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Views;

use App\Http\Resources\BookResource;
use App\Interfaces\ViewProvider;
use App\Models\Book;
use App\Models\Shelf;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class ShelfViewProvider extends ViewProvider
{
protected ?string $view = "books";
private Shelf $shelf;

public function __construct(int $id)
{
$shelf = Shelf::find($id);

if ($shelf === null) {
throw new \RuntimeException("Shelf with id $id not found");
}

$this->shelf = $shelf;
}

public function toArray(Request $request): array
{
$books = Book::whereHas("shelves", function (Builder $query) {
$query->where("calibuh_shelves.id", "=", $this->shelf->id);
})
->get();

return [
"books" => BookResource::collection($books)->toArray($request),
"title" => "Books in shelf " . $this->shelf->name,
];
}
}
36 changes: 36 additions & 0 deletions app/Http/Views/ShelvesViewProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Http\Views;

use App\Http\Resources\BookResource;
use App\Interfaces\ViewProvider;
use App\Models\Book;
use App\Models\Shelf;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class ShelvesViewProvider extends ViewProvider
{
protected ?string $view = "shelves";

public function toArray(Request $request): array
{
return [
"shelves" => Shelf::all()
->map(function (Shelf $shelf) use ($request) {
$books = Book::whereHas("shelves", function (Builder $query) use ($shelf) {
$query->where("calibuh_shelves.id", "=", $shelf->id);
})
->get();

return [
"books" => BookResource::collection($books)->toArray($request),
"name" => $shelf->name,
"id" => $shelf->id,
"cover" => "/content/" . $books->first()->path ."/cover.jpg",
];
})
->toArray()
];
}
}
36 changes: 36 additions & 0 deletions app/Interfaces/ViewProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Interfaces;

use Illuminate\Contracts\Support\Responsable;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;


abstract class ViewProvider implements Responsable
{
protected ?string $view = null;

/**
* Transform the resource collection into an array.
*
* @return array<string, mixed>
*/
public abstract function toArray(Request $request): array;

/**
* Create an HTTP response that represents the object.
*
* @param Request $request
*/
public function toResponse($request): View|Factory|Application
{
if ($this->view === null) {
throw new \LogicException('$view should be set when extending ViewProvider');
}

return view($this->view, $this->toArray($request));
}
}
38 changes: 38 additions & 0 deletions app/Models/Author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
*
*
* @property int|null $id the author's name
* @property string $name the name of the author in a sortable manner
* @property string|null $sort
* @property string $link ??
* @method static \Illuminate\Database\Eloquent\Builder|Author newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Author newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Author query()
* @method static \Illuminate\Database\Eloquent\Builder|Author whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Author whereLink($value)
* @method static \Illuminate\Database\Eloquent\Builder|Author whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|Author whereSort($value)
* @mixin \Eloquent
*/
class Author extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = "authors";

/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
}
115 changes: 95 additions & 20 deletions app/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,120 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
*
*
* @property int|null $id
* @property string|null $timestamp
* @property string|null $author_sort
* @property string $title
* @property string|null $sort
* @property int $series_index
* @property string $path
* @property int $flags
* @property string|null $uuid
* @property string $last_modified
* @property-read Collection<int, \App\Models\Author> $authors
* @property-read int|null $authors_count
* @property-read Collection<int, \App\Models\BookFormat> $formats
* @property-read int|null $formats_count
* @property-read Collection<int, \App\Models\Series> $serieses
* @property-read int|null $serieses_count
* @property-read Collection<int, \App\Models\BookTag> $tags
* @property-read int|null $tags_count
* @method static \Illuminate\Database\Eloquent\Builder|Book newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Book newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Book query()
* @method static \Illuminate\Database\Eloquent\Builder|Book whereAuthorSort($value)
* @method static \Illuminate\Database\Eloquent\Builder|Book whereFlags($value)
* @method static \Illuminate\Database\Eloquent\Builder|Book whereHasCover($value)
* @method static \Illuminate\Database\Eloquent\Builder|Book whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Book whereIsbn($value)
* @method static \Illuminate\Database\Eloquent\Builder|Book whereLastModified($value)
* @method static \Illuminate\Database\Eloquent\Builder|Book whereLccn($value)
* @method static \Illuminate\Database\Eloquent\Builder|Book wherePath($value)
* @method static \Illuminate\Database\Eloquent\Builder|Book wherePubdate($value)
* @method static \Illuminate\Database\Eloquent\Builder|Book whereSeriesIndex($value)
* @method static \Illuminate\Database\Eloquent\Builder|Book whereSort($value)
* @method static \Illuminate\Database\Eloquent\Builder|Book whereTimestamp($value)
* @method static \Illuminate\Database\Eloquent\Builder|Book whereTitle($value)
* @method static \Illuminate\Database\Eloquent\Builder|Book whereUuid($value)
* @property string|null $pubdate
* @property string|null $isbn
* @property string|null $lccn
* @property bool|null $has_cover
* @property-read Collection<int, \App\Models\Shelf> $shelves
* @property-read int|null $shelves_count
* @mixin \Eloquent
*/
class Book extends Model
{
use HasFactory, Notifiable;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = "books";

/**
* The attributes that are mass assignable.
* Indicates if the model should be timestamped.
*
* @var array<int, string>
* @var bool
*/
protected $fillable = [
"name",
"email",
"password",
];
public $timestamps = false;

/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
"password",
"remember_token",
"timestamp",
"flags",
];

/**
* Get the attributes that should be cast.
*
* @return array<string, string>
* Get the authors of the book.
*/
public function authors(): BelongsToMany
{
return $this->belongsToMany(Author::class, "books_authors_link", "book", "author");
}

/**
* Get the tags of the book.
*/
public function tags(): BelongsToMany
{
return $this->belongsToMany(BookTag::class, "books_tags_link", "book", "tag");
}

/**
* Get the different formats of the book.
*/
public function formats(): HasMany
{
return $this->hasMany(BookFormat::class, "book");
}

/**
* Get the serieses of the book.
*/
public function serieses(): BelongsToMany
{
return $this->belongsToMany(Series::class, "books_series_link", "book", "series");
}

/**
* Get shelves the book is in
*/
protected function casts(): array
public function shelves(): BelongsToMany
{
return [
"email_verified_at" => "datetime",
"password" => "hashed",
];
return $this->belongsToMany(Shelf::class, "calibuh_shelves_books", "book_id", "shelf_id");
}
}
Loading

0 comments on commit c6c231f

Please sign in to comment.