Skip to content

Commit

Permalink
feat: shelves crud & per user
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardotglobal committed Jul 31, 2024
1 parent 9691165 commit af2ed23
Show file tree
Hide file tree
Showing 24 changed files with 505 additions and 264 deletions.
71 changes: 11 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,17 @@
<p align="center"><a href="https://laravel.com" target="_blank"><img src="https://raw.githubusercontent.com/laravel/art/master/logo-lockup/5%20SVG/2%20CMYK/1%20Full%20Color/laravel-logolockup-cmyk-red.svg" width="400" alt="Laravel Logo"></a></p>
# calibuh
Blazingly fast 🔥, lightweight 🎈and minimal alternative to calibre-web. Written in PHP using Laravel and Blade and almost fully server-rendered.
Work in Progress.

<p align="center">
<a href="https://github.com/laravel/framework/actions"><img src="https://github.com/laravel/framework/workflows/tests/badge.svg" alt="Build Status"></a>
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/dt/laravel/framework" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/v/laravel/framework" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/l/laravel/framework" alt="License"></a>
</p>
## Inspiration
Calibre-Web is a great project. However, I've found it to be excruciatingly slow. I've tried to optimize it, but it's just too much of a hassle to work with. So I decided to write my own version of it.

## About Laravel
## The name
According to the creator of calibre, Kovid, the name is pronounced as "cali-bur" instead of "ka-libre", which I did not know until recently and I found quite interesting.

Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:
## Installation

- [Simple, fast routing engine](https://laravel.com/docs/routing).
- [Powerful dependency injection container](https://laravel.com/docs/container).
- Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage.
- Expressive, intuitive [database ORM](https://laravel.com/docs/eloquent).
- Database agnostic [schema migrations](https://laravel.com/docs/migrations).
- [Robust background job processing](https://laravel.com/docs/queues).
- [Real-time event broadcasting](https://laravel.com/docs/broadcasting).
See the excellent [Laravel Deployment documentation.](https://laravel.com/docs/11.x/deployment).
Also run `npm install` and `npm run build` with your favourite node package manager.

Laravel is accessible, powerful, and provides tools required for large, robust applications.
Make sure to symlink or copy your Calibre library to the `public/content` directory (not *inside* the `public/content` directory!).

## Learning Laravel

Laravel has the most extensive and thorough [documentation](https://laravel.com/docs) and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework.

You may also try the [Laravel Bootcamp](https://bootcamp.laravel.com), where you will be guided through building a modern Laravel application from scratch.

If you don"t feel like reading, [Laracasts](https://laracasts.com) can help. Laracasts contains thousands of video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library.

## Laravel Sponsors

We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the [Laravel Partners program](https://partners.laravel.com).

### Premium Partners

- **[Vehikl](https://vehikl.com/)**
- **[Tighten Co.](https://tighten.co)**
- **[WebReinvent](https://webreinvent.com/)**
- **[Kirschbaum Development Group](https://kirschbaumdevelopment.com)**
- **[64 Robots](https://64robots.com)**
- **[Curotec](https://www.curotec.com/services/technologies/laravel/)**
- **[Cyber-Duck](https://cyber-duck.co.uk)**
- **[DevSquad](https://devsquad.com/hire-laravel-developers)**
- **[Jump24](https://jump24.co.uk)**
- **[Redberry](https://redberry.international/laravel/)**
- **[Active Logic](https://activelogic.com)**
- **[byte5](https://byte5.de)**
- **[OP.GG](https://op.gg)**

## Contributing

Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).

## Code of Conduct

In order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct).

## Security Vulnerabilities

If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via [[email protected]](mailto:[email protected]). All security vulnerabilities will be promptly addressed.

## License

The Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
4 changes: 2 additions & 2 deletions app/Http/Resources/BookResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function transform($record): object
{
return (object) [
"authors" => $record->authors
->map(fn($author) => $author->name)
->map(fn($author) => str_replace("|", "", $author->name))
->implode(", "),

"formats" => $record->formats
Expand All @@ -27,12 +27,12 @@ public static function transform($record): object

"tags" => $record->tags
->map(fn($tag) => "#" . $tag->name)
->take(5)
->implode(", "),

"series" => $record->serieses->first() !== null ? "Book $record->series_index of " . $record->serieses->first()->name : null,
"title" => $record->title,
"cover" => "/content/$record->path/cover.jpg",
"id" => $record->id,
];
}
}
26 changes: 26 additions & 0 deletions app/Livewire/AddToShelf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Livewire;

use App\Models\Book;
use App\Models\Shelf;
use Closure;
use Illuminate\View\View;
use LivewireUI\Modal\ModalComponent;

class AddToShelf extends ModalComponent
{
public Book $book;
public array $shelves;

public function render(): View|Closure|string
{
return view("livewire.add-to-shelf");
}

public function save(): void
{
Shelf::findMany($this->shelves)->each(fn(Shelf $shelf) => $shelf->books()->attach($this->book->id));
$this->closeModal();
}
}
7 changes: 7 additions & 0 deletions app/Livewire/EditUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

namespace App\Livewire;

use App\Actions\Fortify\PasswordValidationRules;
use App\Actions\Fortify\UpdateUserPassword;
use App\Models\User;
use Closure;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;
use Illuminate\View\View;
use Livewire\Attributes\Validate;
use LivewireUI\Modal\ModalComponent;
use Throwable;

class EditUser extends ModalComponent
{
public User $user;

#[Validate("required|string")]
public string $newPassword = "";

public function render(): View|Closure|string
Expand All @@ -24,6 +30,7 @@ public function render(): View|Closure|string
*/
public function save(): void
{
$this->validate(["newPassword" => "required|string"]);
$this->user->password = Hash::make($this->newPassword);

$this->user->saveOrFail();
Expand Down
33 changes: 33 additions & 0 deletions app/Livewire/MakeShelf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Livewire;

use App\Models\Shelf;
use Closure;
use Illuminate\View\View;
use Livewire\Attributes\Validate;
use LivewireUI\Modal\ModalComponent;
use Throwable;

class MakeShelf extends ModalComponent
{
#[Validate("required|string|max:255")]
public string $name = "";

public function render(): View|Closure|string
{
return view("livewire.make-shelf");
}

/**
* @throws Throwable
*/
public function create(): void
{
$this->validate();
Shelf::create(["name" => $this->name]);

$this->closeModal();
$this->js("window.location.reload()");
}
}
30 changes: 30 additions & 0 deletions app/Livewire/RemoveFromShelf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Livewire;

use App\Models\Book;
use App\Models\Shelf;
use Closure;
use Illuminate\Support\Facades\Route;
use Illuminate\View\View;
use LivewireUI\Modal\ModalComponent;

class RemoveFromShelf extends ModalComponent
{
public Book $book;
public Shelf $shelf;

public function render(): View|Closure|string
{
return view("livewire.remove-from-shelf");
}

public function confirm(): void
{
$this->shelf->books()->detach($this->book->id);
$this->closeModal();

// TODO: fix this shit
redirect(Route::current()->uri() . "#{$this->shelf->id}");
}
}
18 changes: 17 additions & 1 deletion app/Models/Shelf.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
*
*
*
* @property int $id
* @property \Illuminate\Support\Carbon|null $created_at
Expand All @@ -28,8 +29,23 @@ class Shelf extends Model
{
protected $table = "calibuh_shelves";

/**
* The attributes that are protected.
*
* @var array
*/
protected $guarded = [];

public function books(): BelongsToMany
{
return $this->belongsToMany(Book::class, "calibuh_shelves_books", "shelf_id", "book_id");
}

/**
* Get the user who owns this shelf or null (public)
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
26 changes: 26 additions & 0 deletions app/View/Components/ThemeToggle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class ThemeToggle extends Component
{
/**
* Create a new component instance.
*/
public function __construct()
{
//
}

/**
* Get the view / contents that represent the component.
*/
public function render(): View|Closure|string
{
return view('components.theme-toggle');
}
}
1 change: 1 addition & 0 deletions app/View/Pages/BooksPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ protected function _render(Request $request): View|Closure|string
return view("books", [
"title" => "All books",
"books" => BookResource::collection(Book::all()),
"edit" => true,
]);
}
}
2 changes: 1 addition & 1 deletion app/View/Pages/ShelfPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function _render(Request $request): View|Closure|string

return view("books", [
"books" => BookResource::collection($books),
"title" => "Books in " . $shelf->name,
"title" => "Books in shelf " . $shelf->name,
]);
}
}
6 changes: 4 additions & 2 deletions app/View/Pages/ShelvesPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ class ShelvesPage extends PageComponent
protected function _render(Request $request): View|Closure|string
{
return view("shelves", [
"shelves" => Shelf::all()
"shelves" => Shelf::whereNull("user_id")
->orWhereHas("user", fn(Builder $query) => $query->where("calibuh_shelves.user_id", "=", $request->user()->id))
->orderBy("id", direction: "desc")
->get()
->map(function (Shelf $shelf) {
$books = Book::whereHas("shelves", fn(Builder $query) => $query->where("calibuh_shelves.id", "=", $shelf->id))->get();

return (object)[
"books" => BookResource::collection($books),
"name" => $shelf->name,
"id" => $shelf->id,
"cover" => "/content/" . $books->first()->path . "/cover.jpg",
];
})
]);
Expand Down
Loading

0 comments on commit af2ed23

Please sign in to comment.