Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mythos committed Feb 7, 2022
2 parents 6ee382d + 09ae6da commit 772dc45
Show file tree
Hide file tree
Showing 34 changed files with 767 additions and 584 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

return (new PhpCsFixer\Config)
->setRules([
'no_unused_imports' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => [
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 2.1.0 (2022-02-07)

- Added date format to user profile
- Improve updating series and volumes data Manga Passion API
- ISBN and Publish Date of a volume are now optional
- Reordering volumes makes sure that no gaps are left
- When setting a series as subscribed, all volumes with status "New" will be set to "Ordered"

## 2.0.3 (2022-02-05)

- Applied fixes from 2.0.2 to update button
Expand Down
108 changes: 108 additions & 0 deletions app/Helpers/MangaPassionApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace App\Helpers;

use DateTime;
use Illuminate\Support\Facades\Http;
use Nicebooks\Isbn\Isbn;

class MangaPassionApi
{
public static function loadSeries($title)
{
$response = Http::get('https://api.manga-passion.de/editions?order[titleLength]=asc&order[title]=asc&title=' . urlencode($title));
if (!$response->successful()) {
return null;
}
$response = $response->json();
if (count($response) == 0) {
return null;
}
$result = $response[0];
$series = [];
$series['mangapassion_id'] = $result['id'];

$series['name'] = $result['title'];
if ($result['status'] == 1 || $result['status'] == 2) {
$series['status'] = $result['status'];
} else {
$series['status'] = 0;
}
$series['image_url'] = $result['cover'];

if ($result['status'] == 2) {
$series['total'] = $result['numVolumes'];
} elseif (!empty($result['sources'])) {
$sourceId = $result['sources'][0]['id'];
$sourceResponse = Http::get('https://api.manga-passion.de/sources/' . $sourceId);
if ($sourceResponse->successful()) {
$source = $sourceResponse->json();
if (!empty($source)) {
if (!empty($source['volumes'])) {
$series['total'] = $source['volumes'];
}
}
}
}

$defaultPrice = MangaPassionApi::getDefaultPrice($series['mangapassion_id']);
$series['default_price'] = $defaultPrice;

if (!empty($result['publishers'])) {
$series['publisher'] = $result['publishers'][0]['name'];
}

return $series;
}

public static function loadVolumes($mangaPassionId)
{
$result = [];
$url = 'https://api.manga-passion.de/editions/' . $mangaPassionId . '/volumes?itemsPerPage=500&order[number]=asc';
$response = Http::get($url);
if ($response->successful()) {
$responseBody = $response->json();

foreach ($responseBody as $responseItem) {
$number = !empty($responseItem['number']) ? $responseItem['number'] : 1;
$publish_date = null;
if ($responseItem['status'] < 2) {
$publish_date = !empty($responseItem['date']) ? new DateTime($responseItem['date']) : null;
}
$isbn = !empty($responseItem['isbn13']) ? (string) Isbn::of($responseItem['isbn13'])->to13() : null;
$price = !empty($responseItem['price']) ? floatval($responseItem['price']) / 100.0 : 0;

$result[] = [
'number' => $number,
'isbn' => $isbn,
'publish_date' => $publish_date,
'price' => $price,
];
}

return collect($result);
}

return null;
}

public static function getDefaultPrice($mangaPassionId): ?float
{
$volumesResponse = Http::get('https://api.manga-passion.de/editions/' . $mangaPassionId . '/volumes?itemsPerPage=1&order[number]=asc');
if (!$volumesResponse->successful()) {
return null;
}
$volumesResult = $volumesResponse->json();
if (count($volumesResult) > 0) {
foreach ($volumesResult as $volumeResult) {
if (empty($volumeResult['price'])) {
continue;
}

return !empty($volumeResult['price']) ? floatval($volumeResult['price']) / 100.0 : 0;
}
}

return null;
}
}
2 changes: 0 additions & 2 deletions app/Http/Livewire/Overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace App\Http\Livewire;

use App\Models\Volume;
use Exception;
use Illuminate\Support\Facades\DB;
use Livewire\Component;

Expand Down
1 change: 1 addition & 0 deletions app/Http/Livewire/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Profile extends Component
'user.name' => 'required',
'user.email' => 'required|email',
'user.format_isbns_enabled' => 'boolean',
'user.date_format' => 'required',
];

public function mount(): void
Expand Down
129 changes: 43 additions & 86 deletions app/Http/Livewire/Series/CreateSeries.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

namespace App\Http\Livewire\Series;

use App\Helpers\MangaPassionApi;
use App\Models\Category;
use App\Models\Publisher;
use App\Models\Series;
use App\Models\Volume;
use DateTime;
use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image as FacadesImage;
use Intervention\Image\Image;
use Livewire\Component;
use Nicebooks\Isbn\Isbn;
use Storage;

class CreateSeries extends Component
Expand Down Expand Up @@ -95,68 +93,30 @@ public function fetchdata(): void
{
$this->validateOnly('series.name');
$this->series->mangapassion_id = null;
$response = Http::get('https://api.manga-passion.de/editions?order[titleLength]=asc&order[title]=asc&title=' . urlencode($this->series->name));
if ($response->successful()) {
$response = $response->json();
if (count($response) == 0) {
return;
}
$result = $response[0];
if (!empty($result['id'])) {
$this->series->mangapassion_id = $result['id'];
}
if (!empty($result['title'])) {
$this->series->name = $result['title'];
}
if (!empty($result['status'])) {
if ($result['status'] == 1) {
$this->series->status = 1;
} elseif ($result['status'] == 2) {
$this->series->status = 2;
} else {
$this->series->status = 0;
}
}
if (!empty($result['cover'])) {
$this->image_url = $result['cover'];
}

if (!empty($result['status']) && $result['status'] == 2) {
$this->series->total = $result['numVolumes'];
} elseif (!empty($result['sources'])) {
$sourceId = $result['sources'][0]['id'];
$sourceResponse = Http::get('https://api.manga-passion.de/sources/' . $sourceId);
if ($sourceResponse->successful()) {
$source = $sourceResponse->json();
if (!empty($source)) {
if (!empty($source['volumes'])) {
$this->series->total = $source['volumes'];
}
}
}
}

$volumesResponse = Http::get('https://api.manga-passion.de/editions/' . $this->series->mangapassion_id . '/volumes?itemsPerPage=1&order[number]=asc');
if ($volumesResponse->successful()) {
$volumesResult = $volumesResponse->json();
if (count($volumesResult) > 0) {
foreach ($volumesResult as $volumeResult) {
if (empty($volumeResult['price'])) {
continue;
}
$this->series->default_price = !empty($volumeResult['price']) ? floatval($volumeResult['price']) / 100.0 : 0;
}
}
}

if (!empty($result['publishers'])) {
$publisherName = $result['publishers'][0]['name'];
$publisher = Publisher::whereName($publisherName)->firstOrCreate([
'name' => $publisherName,
]);
$this->publishers = Publisher::orderBy('name')->get();
$this->series->publisher_id = $publisher->id;
}
$series = MangaPassionApi::loadSeries($this->series->name);

if (empty($series)) {
toastr()->livewire()->addWarning(__('No entry with the title :name has been found', ['name' => $this->series->name]));

return;
}

$this->series->mangapassion_id = $series['mangapassion_id'];
$this->series->name = $series['name'];
$this->series->status = $series['status'];
$this->series->total = $series['total'];
$this->series->default_price = $series['default_price'];
$this->image_url = $series['image_url'];

$publisher = Publisher::whereName($series['publisher'])->first();
if (!empty($publisher)) {
$this->series->publisher_id = $publisher->id;
} else {
$publisher = new Publisher(['name' => $series['publisher']]);
$publisher->save();

$this->series->publisher_id = $publisher->id;
$this->publishers = Publisher::orderBy('name')->get();
}
}

Expand Down Expand Up @@ -186,27 +146,24 @@ private function createVolumes(): void
if (empty($this->series->mangapassion_id)) {
return;
}
$seriesId = $this->series->id;
$volumesResponse = Http::get('https://api.manga-passion.de/editions/' . $this->series->mangapassion_id . '/volumes?itemsPerPage=500&order[number]=asc');
if ($volumesResponse->successful()) {
$volumesResult = $volumesResponse->json();
if (count($volumesResult) > 0) {
foreach ($volumesResult as $volumeResult) {
if (empty($volumeResult['isbn13'])) {
continue;
}
$publish_date = !empty($volumeResult['date']) ? new DateTime($volumeResult['date']) : null;
$volume = new Volume([
'series_id' => $seriesId,
'isbn' => Isbn::of($volumeResult['isbn13'])->to13(),
'number' => $volumeResult['number'] ?? 1,
'publish_date' => !empty($publish_date) ? $publish_date->format('Y-m-d') : null,
'price' => !empty($volumeResult['price']) ? floatval($volumeResult['price']) / 100.0 : 0,
'status' => $this->series->subscription_active,
]);
$volume->save();
}
}

$volumesResult = MangaPassionApi::loadVolumes($this->series->mangapassion_id);

foreach ($volumesResult as $newVolume) {
$number = $newVolume['number'];
$isbn = $newVolume['isbn'];
$publish_date = $newVolume['publish_date'];
$price = $newVolume['price'];

$volume = new Volume([
'series_id' => $this->series->id,
'isbn' => $isbn,
'number' => $number,
'publish_date' => !empty($publish_date) ? $publish_date->format('Y-m-d') : null,
'price' => $price,
'status' => $this->series->subscription_active,
]);
$volume->save();
}
}
}
16 changes: 12 additions & 4 deletions app/Http/Livewire/Series/EditSeries.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Livewire\Series;

use App\Http\Traits\LivewireDelete;
use App\Models\Category;
use App\Models\Publisher;
use App\Models\Series;
Expand Down Expand Up @@ -67,7 +66,7 @@ public function render()
return view('livewire.series.edit-series')->extends('layouts.app')->section('content');
}

public function save(): void
public function save()
{
$this->validate();
if (!empty($this->series->default_price)) {
Expand All @@ -78,8 +77,10 @@ public function save(): void
$this->series->save();
$this->storeImages($image);
$this->updatePrices();
toastr()->livewire()->addSuccess(__(':name has been updated', ['name' => $this->series->name]));
$this->reset(['image_url']);
$this->updateStatuses();
toastr()->addSuccess(__(':name has been updated', ['name' => $this->series->name]));

return redirect()->route('series.show', [$this->category, $this->series]);
} catch (Exception $exception) {
Log::error($exception);
toastr()->livewire()->addError(__(':name could not be updated', ['name' => $this->series->name]));
Expand Down Expand Up @@ -131,4 +132,11 @@ private function updatePrices(): void
Volume::whereSeriesId($this->series->id)->whereNull('price')->update(['price' => $this->series->default_price]);
}
}

private function updateStatuses(): void
{
if ($this->series->subscription_active) {
Volume::whereSeriesId($this->series->id)->where('status', '=', '0')->update(['status' => 1]);
}
}
}
Loading

0 comments on commit 772dc45

Please sign in to comment.