From ac4762291c1d02073f9fc660d91a02b783f6ede3 Mon Sep 17 00:00:00 2001 From: Ilya Stepenko <38462532+VampireAotD@users.noreply.github.com> Date: Fri, 9 Aug 2024 17:17:05 +0300 Subject: [PATCH] Refactored code --- .../Commands/Lists/Anime/GenerateCommand.php | 2 +- .../Telegram/TelegramController.php | 15 ++++++- .../RedirectIfHasAssignedUserMiddleware.php | 2 +- src/app/Models/Anime.php | 4 +- src/app/Models/AnimeSynonym.php | 2 + src/app/Models/AnimeUrl.php | 2 + src/app/Models/Genre.php | 2 + src/app/Models/TelegramUser.php | 6 ++- src/app/Models/User.php | 3 -- src/app/Models/VoiceActing.php | 2 + src/app/Providers/AppServiceProvider.php | 2 + .../TelegramUser/TelegramUserRepository.php | 8 ---- .../TelegramUserRepositoryInterface.php | 12 ------ src/app/Services/TelegramUserService.php | 14 +++++-- ..._04_123816_create_telegram_users_table.php | 10 ++--- ...12_04_124746_create_voice_acting_table.php | 5 +-- .../2021_12_04_124796_create_animes_table.php | 10 +---- ...2_05_131321_alter_telegram_users_table.php | 40 ------------------- ..._voice_acting_column_from_animes_table.php | 36 ----------------- ...171703_create_anime_voice_acting_table.php | 9 ++--- .../2021_12_26_113712_create_images_table.php | 4 -- ...ange_type_of_id_column_in_users_table.php} | 4 -- ...174126_add_new_columns_to_animes_table.php | 6 +-- .../2021_12_26_174705_create_genres_table.php | 4 -- ...12_26_174721_create_anime_genres_table.php | 6 +-- ...73201_add_alias_column_to_images_table.php | 4 -- ..._06_123617_create_anime_synonyms_table.php | 9 ++--- ...2_12_06_123645_create_anime_urls_table.php | 9 ++--- ..._from_animes_table_to_anime_urls_table.php | 4 -- ...index_for_title_column_in_animes_table.php | 29 -------------- ...ndex_for_status_column_in_animes_table.php | 29 -------------- ...column_to_name_in_anime_synonyms_table.php | 2 + ...213212_add_new_columns_to_animes_table.php | 2 +- .../Lists/Anime/GenerateCommandTest.php | 2 +- .../Telegram/TelegramControllerTest.php | 28 +++++++------ src/vite.config.ts | 3 ++ 36 files changed, 85 insertions(+), 246 deletions(-) delete mode 100644 src/database/migrations/2021_12_05_131321_alter_telegram_users_table.php delete mode 100644 src/database/migrations/2021_12_24_171255_remove_favourite_voice_acting_column_from_animes_table.php rename src/database/migrations/{2021_12_26_121558_alter_users_table.php => 2021_12_26_121558_change_type_of_id_column_in_users_table.php} (92%) delete mode 100644 src/database/migrations/2023_12_23_152045_add_unique_index_for_title_column_in_animes_table.php delete mode 100644 src/database/migrations/2023_12_23_152543_add_index_for_status_column_in_animes_table.php diff --git a/src/app/Console/Commands/Lists/Anime/GenerateCommand.php b/src/app/Console/Commands/Lists/Anime/GenerateCommand.php index f503cb11..08b3e710 100644 --- a/src/app/Console/Commands/Lists/Anime/GenerateCommand.php +++ b/src/app/Console/Commands/Lists/Anime/GenerateCommand.php @@ -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', diff --git a/src/app/Http/Controllers/Telegram/TelegramController.php b/src/app/Http/Controllers/Telegram/TelegramController.php index 0ebf05d5..01ea55c6 100644 --- a/src/app/Http/Controllers/Telegram/TelegramController.php +++ b/src/app/Http/Controllers/Telegram/TelegramController.php @@ -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 { @@ -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(); } @@ -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']); } diff --git a/src/app/Http/Middleware/Telegram/RedirectIfHasAssignedUserMiddleware.php b/src/app/Http/Middleware/Telegram/RedirectIfHasAssignedUserMiddleware.php index 812611dc..1e31d67c 100644 --- a/src/app/Http/Middleware/Telegram/RedirectIfHasAssignedUserMiddleware.php +++ b/src/app/Http/Middleware/Telegram/RedirectIfHasAssignedUserMiddleware.php @@ -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); diff --git a/src/app/Models/Anime.php b/src/app/Models/Anime.php index 5771f224..5b83640d 100644 --- a/src/app/Models/Anime.php +++ b/src/app/Models/Anime.php @@ -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', diff --git a/src/app/Models/AnimeSynonym.php b/src/app/Models/AnimeSynonym.php index 17e117f0..0a6dc229 100644 --- a/src/app/Models/AnimeSynonym.php +++ b/src/app/Models/AnimeSynonym.php @@ -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; @@ -13,6 +14,7 @@ */ class AnimeSynonym extends Model { + use HasUuids; use HasFactory; protected $fillable = ['anime_id', 'name']; diff --git a/src/app/Models/AnimeUrl.php b/src/app/Models/AnimeUrl.php index 76011e9b..385c5dba 100644 --- a/src/app/Models/AnimeUrl.php +++ b/src/app/Models/AnimeUrl.php @@ -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; @@ -14,6 +15,7 @@ */ class AnimeUrl extends Model { + use HasUuids; use HasFactory; protected $fillable = ['anime_id', 'url']; diff --git a/src/app/Models/Genre.php b/src/app/Models/Genre.php index d1409e10..de0c5942 100644 --- a/src/app/Models/Genre.php +++ b/src/app/Models/Genre.php @@ -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 @@ -19,6 +20,7 @@ class Genre extends Model use HasUuids; use HasFactory; use Filterable; + use SoftDeletes; protected $fillable = ['name']; diff --git a/src/app/Models/TelegramUser.php b/src/app/Models/TelegramUser.php index cc0a67d3..08d9c375 100644 --- a/src/app/Models/TelegramUser.php +++ b/src/app/Models/TelegramUser.php @@ -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', diff --git a/src/app/Models/User.php b/src/app/Models/User.php index 9fbc58b2..0f8f1c2b 100644 --- a/src/app/Models/User.php +++ b/src/app/Models/User.php @@ -47,9 +47,6 @@ protected function casts(): array ]; } - /** - * @return HasOne - */ public function telegramUser(): HasOne { return $this->hasOne(TelegramUser::class); diff --git a/src/app/Models/VoiceActing.php b/src/app/Models/VoiceActing.php index 88d0a7b1..c3b3513e 100644 --- a/src/app/Models/VoiceActing.php +++ b/src/app/Models/VoiceActing.php @@ -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 @@ -18,6 +19,7 @@ class VoiceActing extends Model use HasUuids; use HasFactory; use Filterable; + use SoftDeletes; protected $table = 'voice_acting'; diff --git a/src/app/Providers/AppServiceProvider.php b/src/app/Providers/AppServiceProvider.php index 4b6e55e9..50cf1cd6 100644 --- a/src/app/Providers/AppServiceProvider.php +++ b/src/app/Providers/AppServiceProvider.php @@ -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; @@ -54,6 +55,7 @@ public function register(): void public function boot(): void { $this->enforceMorphAliases(); + Model::shouldBeStrict(!$this->app->isProduction()); } private function setUpElasticsearchClient(): void diff --git a/src/app/Repositories/TelegramUser/TelegramUserRepository.php b/src/app/Repositories/TelegramUser/TelegramUserRepository.php index df188ffc..7fdfb133 100644 --- a/src/app/Repositories/TelegramUser/TelegramUserRepository.php +++ b/src/app/Repositories/TelegramUser/TelegramUserRepository.php @@ -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(); diff --git a/src/app/Repositories/TelegramUser/TelegramUserRepositoryInterface.php b/src/app/Repositories/TelegramUser/TelegramUserRepositoryInterface.php index 1566c5dc..cecb17a6 100644 --- a/src/app/Repositories/TelegramUser/TelegramUserRepositoryInterface.php +++ b/src/app/Repositories/TelegramUser/TelegramUserRepositoryInterface.php @@ -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; } diff --git a/src/app/Services/TelegramUserService.php b/src/app/Services/TelegramUserService.php index ebac4dc2..a2b0ccf0 100644 --- a/src/app/Services/TelegramUserService.php +++ b/src/app/Services/TelegramUserService.php @@ -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(); diff --git a/src/database/migrations/2021_12_04_123816_create_telegram_users_table.php b/src/database/migrations/2021_12_04_123816_create_telegram_users_table.php index e50b943f..fc362b63 100644 --- a/src/database/migrations/2021_12_04_123816_create_telegram_users_table.php +++ b/src/database/migrations/2021_12_04_123816_create_telegram_users_table.php @@ -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 { diff --git a/src/database/migrations/2021_12_04_124746_create_voice_acting_table.php b/src/database/migrations/2021_12_04_124746_create_voice_acting_table.php index e5754ac5..09ab0f0e 100644 --- a/src/database/migrations/2021_12_04_124746_create_voice_acting_table.php +++ b/src/database/migrations/2021_12_04_124746_create_voice_acting_table.php @@ -9,8 +9,6 @@ return new class extends Migration { /** * Run the migrations. - * - * @return void */ public function up(): void { @@ -18,13 +16,12 @@ public function up(): void $table->uuid('id')->primary(); $table->string('name')->unique(); $table->timestamps(); + $table->softDeletes(); }); } /** * Reverse the migrations. - * - * @return void */ public function down(): void { diff --git a/src/database/migrations/2021_12_04_124796_create_animes_table.php b/src/database/migrations/2021_12_04_124796_create_animes_table.php index 3db5e4ff..795c323d 100644 --- a/src/database/migrations/2021_12_04_124796_create_animes_table.php +++ b/src/database/migrations/2021_12_04_124796_create_animes_table.php @@ -9,19 +9,13 @@ 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(); }); @@ -29,8 +23,6 @@ public function up(): void /** * Reverse the migrations. - * - * @return void */ public function down(): void { diff --git a/src/database/migrations/2021_12_05_131321_alter_telegram_users_table.php b/src/database/migrations/2021_12_05_131321_alter_telegram_users_table.php deleted file mode 100644 index 5a74623a..00000000 --- a/src/database/migrations/2021_12_05_131321_alter_telegram_users_table.php +++ /dev/null @@ -1,40 +0,0 @@ -dropColumn(['nickname']); - }); - - Schema::table('telegram_users', function (Blueprint $table) { - $table->string('first_name')->nullable()->after('telegram_id'); - $table->string('last_name')->nullable()->after('first_name'); - $table->string('username')->nullable()->change(); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down(): void - { - Schema::table('telegram_users', function (Blueprint $table) { - $table->string('nickname')->after('telegram_id'); - $table->string('username')->after('nickname')->change(); - }); - } -}; diff --git a/src/database/migrations/2021_12_24_171255_remove_favourite_voice_acting_column_from_animes_table.php b/src/database/migrations/2021_12_24_171255_remove_favourite_voice_acting_column_from_animes_table.php deleted file mode 100644 index d2ba94d8..00000000 --- a/src/database/migrations/2021_12_24_171255_remove_favourite_voice_acting_column_from_animes_table.php +++ /dev/null @@ -1,36 +0,0 @@ -dropConstrainedForeignId('favourite_voice_acting'); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down(): void - { - Schema::table('animes', function (Blueprint $table) { - $table->foreignUuid('favourite_voice_acting') - ->nullable() - ->constrained('voice_acting') - ->nullOnDelete(); - }); - } -}; diff --git a/src/database/migrations/2021_12_24_171703_create_anime_voice_acting_table.php b/src/database/migrations/2021_12_24_171703_create_anime_voice_acting_table.php index 225e5619..60140097 100644 --- a/src/database/migrations/2021_12_24_171703_create_anime_voice_acting_table.php +++ b/src/database/migrations/2021_12_24_171703_create_anime_voice_acting_table.php @@ -9,23 +9,20 @@ return new class extends Migration { /** * Run the migrations. - * - * @return void */ public function up(): void { Schema::create('anime_voice_acting', function (Blueprint $table) { $table->uuid('id')->primary(); $table->foreignUuid('anime_id')->constrained()->cascadeOnDelete(); - $table->foreignUuid('voice_acting_id')->constrained('voice_acting') - ->cascadeOnDelete(); + $table->foreignUuid('voice_acting_id')->constrained('voice_acting')->cascadeOnDelete(); + + $table->unique(['anime_id', 'voice_acting_id']); }); } /** * Reverse the migrations. - * - * @return void */ public function down(): void { diff --git a/src/database/migrations/2021_12_26_113712_create_images_table.php b/src/database/migrations/2021_12_26_113712_create_images_table.php index b4df48b0..dab53936 100644 --- a/src/database/migrations/2021_12_26_113712_create_images_table.php +++ b/src/database/migrations/2021_12_26_113712_create_images_table.php @@ -9,8 +9,6 @@ return new class extends Migration { /** * Run the migrations. - * - * @return void */ public function up(): void { @@ -24,8 +22,6 @@ public function up(): void /** * Reverse the migrations. - * - * @return void */ public function down(): void { diff --git a/src/database/migrations/2021_12_26_121558_alter_users_table.php b/src/database/migrations/2021_12_26_121558_change_type_of_id_column_in_users_table.php similarity index 92% rename from src/database/migrations/2021_12_26_121558_alter_users_table.php rename to src/database/migrations/2021_12_26_121558_change_type_of_id_column_in_users_table.php index fc1c4a28..0370d24d 100644 --- a/src/database/migrations/2021_12_26_121558_alter_users_table.php +++ b/src/database/migrations/2021_12_26_121558_change_type_of_id_column_in_users_table.php @@ -9,8 +9,6 @@ return new class extends Migration { /** * Run the migrations. - * - * @return void */ public function up(): void { @@ -22,8 +20,6 @@ public function up(): void /** * Reverse the migrations. - * - * @return void */ public function down(): void { diff --git a/src/database/migrations/2021_12_26_174126_add_new_columns_to_animes_table.php b/src/database/migrations/2021_12_26_174126_add_new_columns_to_animes_table.php index c926dce2..67e0bd91 100644 --- a/src/database/migrations/2021_12_26_174126_add_new_columns_to_animes_table.php +++ b/src/database/migrations/2021_12_26_174126_add_new_columns_to_animes_table.php @@ -9,8 +9,6 @@ return new class extends Migration { /** * Run the migrations. - * - * @return void */ public function up(): void { @@ -18,7 +16,7 @@ public function up(): void // Values are hardcoded here so that migration would not fail if enum will be deleted. $statuses = ['Анонс', 'Онгоинг', 'Вышел']; - $table->enum('status', $statuses)->after('title')->default('Анонс'); + $table->enum('status', $statuses)->after('title')->default('Анонс')->index(); $table->float('rating', precision: 24)->after('status')->default(1); $table->string('episodes')->after('rating')->nullable(); }); @@ -26,8 +24,6 @@ public function up(): void /** * Reverse the migrations. - * - * @return void */ public function down(): void { diff --git a/src/database/migrations/2021_12_26_174705_create_genres_table.php b/src/database/migrations/2021_12_26_174705_create_genres_table.php index f2610dbb..50ed5c7f 100644 --- a/src/database/migrations/2021_12_26_174705_create_genres_table.php +++ b/src/database/migrations/2021_12_26_174705_create_genres_table.php @@ -9,8 +9,6 @@ return new class extends Migration { /** * Run the migrations. - * - * @return void */ public function up(): void { @@ -24,8 +22,6 @@ public function up(): void /** * Reverse the migrations. - * - * @return void */ public function down(): void { diff --git a/src/database/migrations/2021_12_26_174721_create_anime_genres_table.php b/src/database/migrations/2021_12_26_174721_create_anime_genres_table.php index 87db9724..5bad554d 100644 --- a/src/database/migrations/2021_12_26_174721_create_anime_genres_table.php +++ b/src/database/migrations/2021_12_26_174721_create_anime_genres_table.php @@ -9,8 +9,6 @@ return new class extends Migration { /** * Run the migrations. - * - * @return void */ public function up(): void { @@ -18,13 +16,13 @@ public function up(): void $table->uuid('id')->primary(); $table->foreignUuid('anime_id')->constrained()->cascadeOnDelete(); $table->foreignUuid('genre_id')->constrained()->cascadeOnDelete(); + + $table->unique(['anime_id', 'genre_id']); }); } /** * Reverse the migrations. - * - * @return void */ public function down(): void { diff --git a/src/database/migrations/2021_12_29_173201_add_alias_column_to_images_table.php b/src/database/migrations/2021_12_29_173201_add_alias_column_to_images_table.php index c4959755..a84fc6c7 100644 --- a/src/database/migrations/2021_12_29_173201_add_alias_column_to_images_table.php +++ b/src/database/migrations/2021_12_29_173201_add_alias_column_to_images_table.php @@ -9,8 +9,6 @@ return new class extends Migration { /** * Run the migrations. - * - * @return void */ public function up(): void { @@ -21,8 +19,6 @@ public function up(): void /** * Reverse the migrations. - * - * @return void */ public function down(): void { diff --git a/src/database/migrations/2022_12_06_123617_create_anime_synonyms_table.php b/src/database/migrations/2022_12_06_123617_create_anime_synonyms_table.php index 5690e053..2e91da98 100644 --- a/src/database/migrations/2022_12_06_123617_create_anime_synonyms_table.php +++ b/src/database/migrations/2022_12_06_123617_create_anime_synonyms_table.php @@ -9,22 +9,21 @@ return new class extends Migration { /** * Run the migrations. - * - * @return void */ public function up(): void { Schema::create('anime_synonyms', function (Blueprint $table) { + $table->uuid('id')->primary(); $table->foreignUuid('anime_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate(); - $table->string('synonym')->unique(); + $table->string('synonym'); $table->timestamps(); + + $table->unique(['anime_id', 'synonym']); }); } /** * Reverse the migrations. - * - * @return void */ public function down(): void { diff --git a/src/database/migrations/2022_12_06_123645_create_anime_urls_table.php b/src/database/migrations/2022_12_06_123645_create_anime_urls_table.php index a75959e0..dcc77633 100644 --- a/src/database/migrations/2022_12_06_123645_create_anime_urls_table.php +++ b/src/database/migrations/2022_12_06_123645_create_anime_urls_table.php @@ -9,22 +9,21 @@ return new class extends Migration { /** * Run the migrations. - * - * @return void */ public function up(): void { Schema::create('anime_urls', function (Blueprint $table) { + $table->uuid('id')->primary(); $table->foreignUuid('anime_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate(); - $table->string('url')->unique(); + $table->string('url'); $table->timestamps(); + + $table->unique(['anime_id', 'url']); }); } /** * Reverse the migrations. - * - * @return void */ public function down(): void { diff --git a/src/database/migrations/2022_12_06_183700_transfer_url_from_animes_table_to_anime_urls_table.php b/src/database/migrations/2022_12_06_183700_transfer_url_from_animes_table_to_anime_urls_table.php index 53fd240f..6931a00b 100644 --- a/src/database/migrations/2022_12_06_183700_transfer_url_from_animes_table_to_anime_urls_table.php +++ b/src/database/migrations/2022_12_06_183700_transfer_url_from_animes_table_to_anime_urls_table.php @@ -11,8 +11,6 @@ return new class extends Migration { /** * Run the migrations. - * - * @return void */ public function up(): void { @@ -27,8 +25,6 @@ public function up(): void /** * Reverse the migrations. - * - * @return void */ public function down(): void { diff --git a/src/database/migrations/2023_12_23_152045_add_unique_index_for_title_column_in_animes_table.php b/src/database/migrations/2023_12_23_152045_add_unique_index_for_title_column_in_animes_table.php deleted file mode 100644 index acb912b5..00000000 --- a/src/database/migrations/2023_12_23_152045_add_unique_index_for_title_column_in_animes_table.php +++ /dev/null @@ -1,29 +0,0 @@ -unique('title'); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::table('animes', function (Blueprint $table) { - $table->dropUnique('animes_title_unique'); - }); - } -}; diff --git a/src/database/migrations/2023_12_23_152543_add_index_for_status_column_in_animes_table.php b/src/database/migrations/2023_12_23_152543_add_index_for_status_column_in_animes_table.php deleted file mode 100644 index 1cf0c6ef..00000000 --- a/src/database/migrations/2023_12_23_152543_add_index_for_status_column_in_animes_table.php +++ /dev/null @@ -1,29 +0,0 @@ -index('status'); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::table('animes', function (Blueprint $table) { - $table->dropIndex('animes_status_index'); - }); - } -}; diff --git a/src/database/migrations/2024_03_14_134722_rename_synonym_column_to_name_in_anime_synonyms_table.php b/src/database/migrations/2024_03_14_134722_rename_synonym_column_to_name_in_anime_synonyms_table.php index 0669bdf9..2da0eac8 100644 --- a/src/database/migrations/2024_03_14_134722_rename_synonym_column_to_name_in_anime_synonyms_table.php +++ b/src/database/migrations/2024_03_14_134722_rename_synonym_column_to_name_in_anime_synonyms_table.php @@ -14,6 +14,7 @@ public function up(): void { Schema::table('anime_synonyms', function (Blueprint $table) { $table->renameColumn('synonym', 'name'); + $table->renameIndex('anime_synonyms_anime_id_synonym_unique', 'anime_synonyms_anime_id_name_unique'); }); } @@ -24,6 +25,7 @@ public function down(): void { Schema::table('anime_synonyms', function (Blueprint $table) { $table->renameColumn('name', 'synonym'); + $table->renameIndex('anime_synonyms_anime_id_name_unique', 'anime_synonyms_anime_id_synonym_unique'); }); } }; diff --git a/src/database/migrations/2024_06_24_213212_add_new_columns_to_animes_table.php b/src/database/migrations/2024_06_24_213212_add_new_columns_to_animes_table.php index 5aa50f3f..f1001755 100644 --- a/src/database/migrations/2024_06_24_213212_add_new_columns_to_animes_table.php +++ b/src/database/migrations/2024_06_24_213212_add_new_columns_to_animes_table.php @@ -16,7 +16,7 @@ public function up(): void // Values are hardcoded here so that migration would not fail if enum will be deleted. $types = ['ТВ Сериал', 'Фильм']; - $table->enum('type', $types)->after('title'); + $table->enum('type', $types)->after('title')->index(); $table->year('year')->after('episodes'); }); } diff --git a/src/tests/Feature/Console/Commands/Lists/Anime/GenerateCommandTest.php b/src/tests/Feature/Console/Commands/Lists/Anime/GenerateCommandTest.php index 35687609..5c7a7b78 100644 --- a/src/tests/Feature/Console/Commands/Lists/Anime/GenerateCommandTest.php +++ b/src/tests/Feature/Console/Commands/Lists/Anime/GenerateCommandTest.php @@ -66,7 +66,7 @@ public function testCommandCanGenerateAnimeList(): void $this->assertJson($json); $this->assertJsonStringEqualsJsonString( $this->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', diff --git a/src/tests/Feature/Http/Controllers/Telegram/TelegramControllerTest.php b/src/tests/Feature/Http/Controllers/Telegram/TelegramControllerTest.php index ab50061a..23f6fd11 100644 --- a/src/tests/Feature/Http/Controllers/Telegram/TelegramControllerTest.php +++ b/src/tests/Feature/Http/Controllers/Telegram/TelegramControllerTest.php @@ -7,8 +7,8 @@ use App\Models\TelegramUser; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; -use Tests\TestCase; use Tests\Concerns\Fake\CanCreateFakeUsers; +use Tests\TestCase; class TelegramControllerTest extends TestCase { @@ -45,9 +45,7 @@ private function getValidTelegramData(): array public function testUserCannotConnectToTelegramWithoutTelegramSignature(): void { - $this->actingAs($this->createUser()) - ->post(route('telegram.assign')) - ->assertBadRequest(); + $this->actingAs($this->createUser())->post(route('telegram.assign'))->assertBadRequest(); } public function testUserCannotConnectToTelegramWithInvalidTelegramSignature(): void @@ -57,30 +55,34 @@ public function testUserCannotConnectToTelegramWithInvalidTelegramSignature(): v ->assertForbidden(); } - public function testUserCannotConnectToTelegramIfHeAlreadyHasAssignedTelegramUser(): void + public function testUserCannotAssignTelegramAccountIfHeAlreadyHasOne(): void { $user = $this->createUser(); - $user->telegramUser()->save(TelegramUser::factory()->create()); + $user->telegramUser()->save(TelegramUser::factory()->make()); - $this->actingAs($user)->post(route('telegram.assign'), $this->getValidTelegramData())->assertRedirect(); + $this->actingAs($user) + ->post(route('telegram.assign'), $this->getValidTelegramData()) + ->assertRedirect() + ->assertSessionHasErrors('message'); } - public function testUserCanConnectHisTelegram(): void + public function testUserCanAssignHisTelegramAccount(): void { $user = $this->createUser(); + $user->telegramUser()->save(TelegramUser::factory()->make()); $this->actingAs($user)->post(route('telegram.assign'), $this->getValidTelegramData())->assertRedirect(); $user->refresh(); - $this->assertNotNull($user->telegramUser); } - public function testUserCanDetachHisTelegram(): void + public function testUserCanDetachHisTelegramAccount(): void { - $user = $this->createUser(); - $telegramUser = $user->telegramUser()->save(TelegramUser::factory()->create()); + $user = $this->createUser(); + $telegramUser = $user->telegramUser()->save(TelegramUser::factory()->make()); + $this->assertInstanceOf(TelegramUser::class, $telegramUser); $this->assertEquals($user->id, $telegramUser->user_id); $this->actingAs($user)->delete(route('telegram.detach'))->assertRedirect(); @@ -88,7 +90,7 @@ public function testUserCanDetachHisTelegram(): void $user->refresh(); $telegramUser->refresh(); - $this->assertNull($telegramUser->user_id); $this->assertNull($user->telegramUser); + $this->assertSoftDeleted($telegramUser); } } diff --git a/src/vite.config.ts b/src/vite.config.ts index ebd0e3fd..3fe73d46 100644 --- a/src/vite.config.ts +++ b/src/vite.config.ts @@ -6,6 +6,9 @@ export default defineConfig({ server: { https: false, open: false, + hmr: { + host: 'localhost', + }, }, plugins: [ laravel({