forked from laravel/passport
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request laravel#14 from Artisans-PXM/templates
Templates
- Loading branch information
Showing
14 changed files
with
441 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\Api\TemplateRequest; | ||
use App\Http\Resources\Api\TemplateResource; | ||
use App\Queries\TemplateQueries; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection; | ||
|
||
class TemplateController extends Controller | ||
{ | ||
public function __construct( | ||
protected TemplateQueries $templateQueries | ||
) { | ||
|
||
} | ||
|
||
public function fetch(): AnonymousResourceCollection | ||
{ | ||
$templates = $this->templateQueries->listQuery(); | ||
|
||
return TemplateResource::collection($templates->getCollection()); | ||
} | ||
|
||
public function create(TemplateRequest $request): JsonResponse | ||
{ | ||
$validatedData = $request->validated(); | ||
|
||
$this->templateQueries->create($validatedData); | ||
|
||
return response()->json([ | ||
'success' => __('Template created successfully.'), | ||
]); | ||
} | ||
|
||
public function delete(string $id): JsonResponse | ||
{ | ||
$this->templateQueries->delete($id); | ||
|
||
return response()->json([ | ||
'success' => __('Template deleted successfully.'), | ||
]); | ||
} | ||
|
||
public function update(TemplateRequest $request, string $id): JsonResponse | ||
{ | ||
$validatedData = $request->validated(); | ||
|
||
$this->templateQueries->update($validatedData, $id); | ||
|
||
return response()->json([ | ||
'success' => __('Template updated successfully.'), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Requests\Api; | ||
|
||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Support\Str; | ||
|
||
class TemplateRequest extends FormRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, (ValidationRule | array<int, string> | string)> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'name' => ['required', 'string'], | ||
'description' => ['nullable', 'string'], | ||
'slug' => ['sometimes'], | ||
]; | ||
} | ||
|
||
/** | ||
* @param array<int, string>|int|string|null $key | ||
* @return array<string, mixed> | ||
*/ | ||
public function validated($key = null, $default = null): array | ||
{ | ||
return array_merge(parent::validated(), [ | ||
'slug' => Str::slug($this->slug), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Resources\Api; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class TemplateResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
$template = $this->resource; | ||
|
||
return [ | ||
'id' => $template->id, | ||
'name' => $template->name, | ||
'description' => $template->description, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
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; | ||
use Spatie\Sluggable\HasSlug; | ||
use Spatie\Sluggable\SlugOptions; | ||
|
||
class Template extends Model | ||
{ | ||
use HasFactory; | ||
use HasUuids; | ||
use HasSlug; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array<int, string> | ||
*/ | ||
protected $fillable = ['company_id', 'name', 'description', 'slug']; | ||
|
||
/** | ||
* Get the options for generating the slug. | ||
*/ | ||
public function getSlugOptions(): SlugOptions | ||
{ | ||
return SlugOptions::create() | ||
->generateSlugsFrom('name') | ||
->saveSlugsTo('slug') | ||
->preventOverwrite(); | ||
} | ||
|
||
public function company(): BelongsTo | ||
{ | ||
return $this->belongsTo(Company::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Queries; | ||
|
||
use App\Models\Template; | ||
use Illuminate\Pagination\LengthAwarePaginator; | ||
use Spatie\QueryBuilder\QueryBuilder; | ||
|
||
class TemplateQueries | ||
{ | ||
public function listQuery(): LengthAwarePaginator | ||
{ | ||
return QueryBuilder::for(Template::class) | ||
->allowedFields(['id', 'name', 'description']) | ||
->defaultSort('-id') | ||
->allowedSorts(['id', 'name']) | ||
->jsonPaginate(); | ||
} | ||
|
||
/** | ||
* @param array<string, string> $data | ||
*/ | ||
public function create(array $data): void | ||
{ | ||
$data['company_id'] ??= app('company_id'); | ||
|
||
Template::create($data); | ||
} | ||
|
||
public function delete(string $id): void | ||
{ | ||
Template::query() | ||
->where('company_id', app('company_id')) | ||
->where('id', $id) | ||
->delete(); | ||
} | ||
|
||
/** | ||
* @param array<string, string> $data | ||
*/ | ||
public function update(array $data, string $id): void | ||
{ | ||
$data['company_id'] ??= app('company_id'); | ||
|
||
Template::query() | ||
->where('company_id', app('company_id')) | ||
->where('id', $id) | ||
->update($data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Database\Factories; | ||
|
||
use App\Models\Template; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Model; | ||
use App\Models\Company; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
/** | ||
* @extends Factory<Template> | ||
*/ | ||
class TemplateFactory extends Factory | ||
{ | ||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'company_id' => fn (): Collection|Model => Company::factory()->create(), | ||
'name' => fake()->word(), | ||
'description' => fake()->sentence(), | ||
]; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
database/migrations/2023_07_12_094121_create_templates_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use App\Models\Company; | ||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration { | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('templates', function (Blueprint $table): void { | ||
$table->uuid('id')->primary(); | ||
$table->foreignIdFor(Company::class, 'company_id')->constrained('companies'); | ||
$table->string('name'); | ||
$table->longText('description')->nullable(); | ||
$table->string('slug'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('templates'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.