first commit
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CreateApiUser extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'user:create-api-user {name} {email} {password}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Command description';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$name = $this->argument('name');
|
||||
$email = $this->argument('email');
|
||||
$password = $this->argument('password');
|
||||
|
||||
if (User::where('email', $email)->exists()) {
|
||||
$this->error("User with email '{$email}' already exists!");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
DB::Transaction(function () use ($name, $email, $password) {
|
||||
// 1. Создаем пользователя в БД
|
||||
$user = User::create([
|
||||
'name' => $name,
|
||||
'email' => $email,
|
||||
'password' => Hash::make($password),
|
||||
]);
|
||||
|
||||
$token = $user->createToken('api-custom-token');
|
||||
|
||||
$this->newLine();
|
||||
$this->table(
|
||||
['Field', 'Value'],
|
||||
[
|
||||
['User ID', $user->id],
|
||||
['Name', $name],
|
||||
['Email', $email],
|
||||
]
|
||||
);
|
||||
|
||||
$this->info('--- BEARER TOKEN FOR YOUR REQUESTS ---');
|
||||
$this->comment($token->plainTextToken);
|
||||
$this->info('--------------------------------------');
|
||||
|
||||
return Command::SUCCESS;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\GetGigsRequest;
|
||||
use App\Http\Resources\GigResource;
|
||||
use App\Models\ORM\Gig;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
|
||||
class GigsController extends Controller
|
||||
{
|
||||
public function index(GetGigsRequest $request): AnonymousResourceCollection
|
||||
{
|
||||
// Используем eager loading (with), чтобы избежать проблемы N+1 запросов к категориям
|
||||
$query = Gig::with('categories');
|
||||
|
||||
// Фильтрация по нескольким категориям (Many-to-Many)
|
||||
if ($request->filled('categories')) {
|
||||
$query->whereHas('categories', function ($q) use ($request) {
|
||||
$q->whereIn('categories.id', $request->input('categories'));
|
||||
});
|
||||
}
|
||||
|
||||
// Фильтрация по промежутку дат
|
||||
if ($request->filled('date_from')) {
|
||||
$query->whereDate('event_date', '>=', $request->input('date_from'));
|
||||
}
|
||||
|
||||
if ($request->filled('date_to')) {
|
||||
$query->whereDate('event_date', '<=', $request->input('date_to'));
|
||||
}
|
||||
|
||||
// Сортируем события: сначала ближайшие
|
||||
$query->orderBy('event_date', 'asc');
|
||||
|
||||
// Пагинация по 10 элементов на страницу
|
||||
$gigs = $query->paginate(10);
|
||||
|
||||
return GigResource::collection($gigs);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class GetGigsRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true; // Разрешаем доступ всем
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'categories' => ['nullable', 'array'],
|
||||
'categories.*' => ['integer', 'exists:categories,id'], // Проверяем, что ID категорий существуют
|
||||
'date_from' => ['nullable', 'date', 'date_format:Y-m-d'],
|
||||
'date_to' => ['nullable', 'date', 'date_format:Y-m-d', 'after_or_equal:date_from'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class GigResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'description' => $this->description,
|
||||
'event_date' => $this->event_date ? Carbon::parse($this->event_date)->toIso8601String() : null,
|
||||
'archived' => $this->archived,
|
||||
'categories' => $this->categories->map(fn($category) => [
|
||||
'id' => $category->id,
|
||||
'name' => $category->name,
|
||||
'slug' => $category->slug,
|
||||
]),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\ORM;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Category extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $table = 'categories';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
public function gigs(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Gig::class, 'gig_category');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\ORM;
|
||||
|
||||
use Database\Factories\GigFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Gig extends Model
|
||||
{
|
||||
use SoftDeletes, HasFactory;
|
||||
|
||||
protected $table = 'gigs';
|
||||
protected $primaryKey = 'id';
|
||||
protected $fillable = [ 'title', 'description' ];
|
||||
|
||||
protected static function newFactory(): GigFactory
|
||||
{
|
||||
return GigFactory::new();
|
||||
}
|
||||
|
||||
public function categories(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Category::class, 'gig_category');
|
||||
}
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'event_date' => 'datetime',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable, HasApiTokens;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user