Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
VampireAotD committed Aug 9, 2024
1 parent 9f4da8a commit ac47622
Show file tree
Hide file tree
Showing 36 changed files with 85 additions and 246 deletions.
2 changes: 1 addition & 1 deletion src/app/Console/Commands/Lists/Anime/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function handle(AnimeService $animeService, UserRepositoryInterface $user
}

$animeList = $animeService->all([
new ColumnFilter(['id', 'title', 'status', 'rating', 'episodes']),
new ColumnFilter(['id', 'title', 'type', 'status', 'rating', 'episodes', 'year']),
new RelationFilter([
'urls:anime_id,url',
'synonyms:anime_id,name',
Expand Down
15 changes: 13 additions & 2 deletions src/app/Http/Controllers/Telegram/TelegramController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use App\Services\TelegramUserService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Throwable;

class TelegramController extends Controller
{
Expand All @@ -29,7 +31,16 @@ public function assign(AssignRequest $request): RedirectResponse
$request->get('username'),
);

$this->telegramUserService->createAndAttach($dto, $request->user());
try {
$this->telegramUserService->createAndAttach($request->user(), $dto);
} catch (Throwable $e) {
Log::error('Failed to assign telegram user', [
'exception_trace' => $e->getTraceAsString(),
'exception_message' => $e->getMessage(),
]);

return back()->withErrors(['message' => $e->getMessage()]);
}

return back();
}
Expand All @@ -40,7 +51,7 @@ public function assign(AssignRequest $request): RedirectResponse
*/
public function detach(Request $request): RedirectResponse
{
if (!$request->user()?->telegramUser()?->update(['user_id' => null])) {
if (!$request->user()?->telegramUser()?->delete()) {
return back()->withErrors(['message' => 'Could not revoke Telegram account']);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RedirectIfHasAssignedUserMiddleware
public function handle(Request $request, Closure $next): Response
{
if ($request->user()->telegramUser) {
return back();
return back()->withErrors(['message' => 'You already have assigned Telegram account']);
}

return $next($request);
Expand Down
4 changes: 3 additions & 1 deletion src/app/Models/Anime.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\SoftDeletes;

#[ObservedBy(AnimeObserver::class)]
/**
* @mixin IdeHelperAnime
*/
#[ObservedBy(AnimeObserver::class)]
class Anime extends Model
{
use HasUuids;
use HasFactory;
use Filterable;
use SoftDeletes;

protected $fillable = [
'title',
Expand Down
2 changes: 2 additions & 0 deletions src/app/Models/AnimeSynonym.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -13,6 +14,7 @@
*/
class AnimeSynonym extends Model
{
use HasUuids;
use HasFactory;

protected $fillable = ['anime_id', 'name'];
Expand Down
2 changes: 2 additions & 0 deletions src/app/Models/AnimeUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -14,6 +15,7 @@
*/
class AnimeUrl extends Model
{
use HasUuids;
use HasFactory;

protected $fillable = ['anime_id', 'url'];
Expand Down
2 changes: 2 additions & 0 deletions src/app/Models/Genre.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
* @mixin IdeHelperGenre
Expand All @@ -19,6 +20,7 @@ class Genre extends Model
use HasUuids;
use HasFactory;
use Filterable;
use SoftDeletes;

protected $fillable = ['name'];

Expand Down
6 changes: 4 additions & 2 deletions src/app/Models/TelegramUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
* @mixin IdeHelperTelegramUser
*/
class TelegramUser extends Model
{
use HasFactory;
use HasUuids;
use HasFactory;
use SoftDeletes;

protected $fillable = [
'telegram_id',
'user_id',
'telegram_id',
'first_name',
'last_name',
'username',
Expand Down
3 changes: 0 additions & 3 deletions src/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ protected function casts(): array
];
}

/**
* @return HasOne
*/
public function telegramUser(): HasOne
{
return $this->hasOne(TelegramUser::class);
Expand Down
2 changes: 2 additions & 0 deletions src/app/Models/VoiceActing.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
* @mixin IdeHelperVoiceActing
Expand All @@ -18,6 +19,7 @@ class VoiceActing extends Model
use HasUuids;
use HasFactory;
use Filterable;
use SoftDeletes;

protected $table = 'voice_acting';

Expand Down
2 changes: 2 additions & 0 deletions src/app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use App\Repositories\VoiceActing\VoiceActingRepositoryInterface;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\ClientBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -54,6 +55,7 @@ public function register(): void
public function boot(): void
{
$this->enforceMorphAliases();
Model::shouldBeStrict(!$this->app->isProduction());
}

private function setUpElasticsearchClient(): void
Expand Down
8 changes: 0 additions & 8 deletions src/app/Repositories/TelegramUser/TelegramUserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,11 @@ public function upsert(array $data): TelegramUser
return $this->query->updateOrCreate(['telegram_id' => $data['telegram_id']], $data);
}

/**
* @param int $telegramId
* @return TelegramUser|null
*/
public function findByTelegramId(int $telegramId): ?TelegramUser
{
return $this->query->where('telegram_id', $telegramId)->first();
}

/**
* @param string $username
* @return TelegramUser|null
*/
public function findByUsername(string $username): ?TelegramUser
{
return $this->query->where('username', $username)->first();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,9 @@

interface TelegramUserRepositoryInterface
{
/**
* @param array $data
* @return TelegramUser
*/
public function upsert(array $data): TelegramUser;

/**
* @param int $telegramId
* @return TelegramUser|null
*/
public function findByTelegramId(int $telegramId): ?TelegramUser;

/**
* @param string $username
* @return TelegramUser|null
*/
public function findByUsername(string $username): ?TelegramUser;
}
14 changes: 11 additions & 3 deletions src/app/Services/TelegramUserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ public function upsert(CreateUserDTO $dto): TelegramUser
/**
* @throws Throwable
*/
public function createAndAttach(CreateUserDTO $dto, User $user): TelegramUser
public function createAndAttach(User $user, CreateUserDTO $dto): TelegramUser
{
return DB::transaction(function () use ($dto, $user) {
$telegramUser = $this->upsert($dto);
return DB::transaction(function () use ($dto, $user): TelegramUser {
/** @var TelegramUser $telegramUser */
$telegramUser = TelegramUser::withTrashed()->updateOrCreate(
['telegram_id' => $dto->telegramId],
$dto->toArray()
);

if ($telegramUser->trashed()) {
$telegramUser->restore();
}

$telegramUser->user()->associate($user)->save();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,22 @@
return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create('telegram_users', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->bigInteger('telegram_id')->unique();
$table->string('nickname');
$table->string('username');
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
$table->string('username')->nullable();
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,19 @@
return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create('voice_acting', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name')->unique();
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,20 @@
return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create('animes', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('title');
$table->string('title')->unique();
$table->string('url');
$table->foreignUuid('favourite_voice_acting')
->nullable()
->constrained('voice_acting')
->nullOnDelete();
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Expand Down

This file was deleted.

Loading

0 comments on commit ac47622

Please sign in to comment.