diff --git a/.env b/.env index 445c485..edae30c 100644 --- a/.env +++ b/.env @@ -2,7 +2,7 @@ APP_NAME=Laravel APP_ENV=local APP_KEY=base64:0hUj54Gbe261h5UYbuXmWsigir+WrXBQuQAZKLsMe4Q= APP_DEBUG=true -APP_URL=http://localhost:8000/api +APP_URL=http://localhost:8000 LOG_CHANNEL=stack LOG_DEPRECATIONS_CHANNEL=null diff --git a/app/Http/Controllers/ApplianceController.php b/app/Http/Controllers/ApplianceController.php new file mode 100644 index 0000000..0507fa4 --- /dev/null +++ b/app/Http/Controllers/ApplianceController.php @@ -0,0 +1,26 @@ +where('user_id', $userId) + ->allowedFilters([ + AllowedFilter::partial('appliance_type'), + AllowedFilter::partial('brand'), + AllowedFilter::partial('problem_details'), + AllowedFilter::partial('application_date'), + ]) + ->paginate(5); + + return new ServiceRequestCollection($appliances); + } +} diff --git a/app/Http/Controllers/ServiceRequestController.php b/app/Http/Controllers/ServiceRequestController.php index 7e9b1bf..6d11ba8 100644 --- a/app/Http/Controllers/ServiceRequestController.php +++ b/app/Http/Controllers/ServiceRequestController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Http\Requests\ServiceRequestRequest; +use App\Http\Resources\ServiceRequestResource; use App\Models\ServiceRequest; use Illuminate\Http\Request; @@ -16,14 +17,6 @@ public function index() // } - /** - * Show the form for creating a new resource. - */ - public function create() - { - // - } - /** * Store a newly created resource in storage. */ @@ -38,17 +31,10 @@ public function store(ServiceRequestRequest $request) /** * Display the specified resource. */ - public function show(string $id) + public function show($id) { - // - } - - /** - * Show the form for editing the specified resource. - */ - public function edit(string $id) - { - // + $serviceRequest = ServiceRequest::with('user')->findOrFail($id); + return new ServiceRequestResource($serviceRequest); } /** diff --git a/app/Http/Resources/ServiceRequestCollection.php b/app/Http/Resources/ServiceRequestCollection.php new file mode 100644 index 0000000..204ae09 --- /dev/null +++ b/app/Http/Resources/ServiceRequestCollection.php @@ -0,0 +1,21 @@ + + */ + public function toArray($request) + { + return $this->collection->transform(function($serviceRequest) { + return new ServiceRequestResource($serviceRequest); + }); + } +} diff --git a/app/Http/Resources/ServiceRequestResource.php b/app/Http/Resources/ServiceRequestResource.php new file mode 100644 index 0000000..3826f05 --- /dev/null +++ b/app/Http/Resources/ServiceRequestResource.php @@ -0,0 +1,32 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'id' => $this->id, + 'user' => new UserResource($this->whenLoaded('user')), + 'appliance_type' => $this->appliance_type, + 'brand' => $this->brand, + 'problem_details' => $this->problem_details, + 'collection_address' => $this->collection_address, + 'service_type' => $this->service_type, + 'preferred_contact_method' => $this->preferred_contact_method, + 'damaged_appliance_image' => $this->getFirstMediaUrl(), + 'application_date' => $this->application_date, + 'created_at' => $this->created_at ? $this->created_at->format('d/M/Y H:i:s') : null, + 'updated_at' => $this->updated_at ? $this->updated_at->format('d/M/Y H:i:s') : null, + ]; + } +} diff --git a/app/Http/Resources/UserResource.php b/app/Http/Resources/UserResource.php new file mode 100644 index 0000000..a2f01b6 --- /dev/null +++ b/app/Http/Resources/UserResource.php @@ -0,0 +1,26 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'id'=>$this->id, + 'first_name' =>$this->first_name, + 'last_name'=>$this->last_name, + 'email' =>$this->email, + 'phone_number'=>$this->phone_number, + 'address'=>$this->address, + ]; + } +} diff --git a/app/Models/ServiceRequest.php b/app/Models/ServiceRequest.php index 480e8a6..0eaf54c 100644 --- a/app/Models/ServiceRequest.php +++ b/app/Models/ServiceRequest.php @@ -17,6 +17,8 @@ class ServiceRequest extends Model implements HasMedia 'service_type', 'preferred_contact_method', 'damaged_appliance_image', 'state' ]; + public $timestamps = true; + public function user() { return $this->belongsTo(User::class); diff --git a/composer.json b/composer.json index 6b49078..03f2ee0 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "laravel/framework": "^10.10", "laravel/sanctum": "^3.3", "laravel/tinker": "^2.8", - "spatie/laravel-medialibrary": "^10.0.0" + "spatie/laravel-medialibrary": "^10.0.0", + "spatie/laravel-query-builder": "^5.7" }, "require-dev": { "fakerphp/faker": "^1.9.1", diff --git a/composer.lock b/composer.lock index 23dc4de..d400e62 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d0626405d9fbdea67510af8a8a255ee6", + "content-hash": "486e5652f5004b892bb9a2136e792093", "packages": [ { "name": "arcanedev/log-viewer", @@ -4062,6 +4062,140 @@ ], "time": "2023-11-03T13:09:19+00:00" }, + { + "name": "spatie/laravel-package-tools", + "version": "1.16.4", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-package-tools.git", + "reference": "ddf678e78d7f8b17e5cdd99c0c3413a4a6592e53" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-package-tools/zipball/ddf678e78d7f8b17e5cdd99c0c3413a4a6592e53", + "reference": "ddf678e78d7f8b17e5cdd99c0c3413a4a6592e53", + "shasum": "" + }, + "require": { + "illuminate/contracts": "^9.28|^10.0|^11.0", + "php": "^8.0" + }, + "require-dev": { + "mockery/mockery": "^1.5", + "orchestra/testbench": "^7.7|^8.0", + "pestphp/pest": "^1.22", + "phpunit/phpunit": "^9.5.24", + "spatie/pest-plugin-test-time": "^1.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "Spatie\\LaravelPackageTools\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "role": "Developer" + } + ], + "description": "Tools for creating Laravel packages", + "homepage": "https://github.com/spatie/laravel-package-tools", + "keywords": [ + "laravel-package-tools", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/laravel-package-tools/issues", + "source": "https://github.com/spatie/laravel-package-tools/tree/1.16.4" + }, + "funding": [ + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2024-03-20T07:29:11+00:00" + }, + { + "name": "spatie/laravel-query-builder", + "version": "5.7.0", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-query-builder.git", + "reference": "ff61a9ea32c5b55cbe63de8e3c6e35a201e6b746" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-query-builder/zipball/ff61a9ea32c5b55cbe63de8e3c6e35a201e6b746", + "reference": "ff61a9ea32c5b55cbe63de8e3c6e35a201e6b746", + "shasum": "" + }, + "require": { + "illuminate/database": "^9.0|^10.0", + "illuminate/http": "^9.0|^10.0", + "illuminate/support": "^9.0|^10.0", + "php": "^8.0", + "spatie/laravel-package-tools": "^1.11" + }, + "require-dev": { + "ext-json": "*", + "mockery/mockery": "^1.4", + "orchestra/testbench": "^7.0|^8.0", + "pestphp/pest": "^1.20", + "phpunit/phpunit": "^9.6.11", + "spatie/invade": "^2.0", + "spatie/laravel-ray": "^1.28" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Spatie\\QueryBuilder\\QueryBuilderServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Spatie\\QueryBuilder\\": "src", + "Spatie\\QueryBuilder\\Database\\Factories\\": "database/factories" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Alex Vanderbist", + "email": "alex@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Easily build Eloquent queries from API requests", + "homepage": "https://github.com/spatie/laravel-query-builder", + "keywords": [ + "laravel-query-builder", + "spatie" + ], + "support": { + "issues": "https://github.com/spatie/laravel-query-builder/issues", + "source": "https://github.com/spatie/laravel-query-builder" + }, + "funding": [ + { + "url": "https://spatie.be/open-source/support-us", + "type": "custom" + } + ], + "time": "2024-01-08T09:57:13+00:00" + }, { "name": "spatie/temporary-directory", "version": "2.2.1", diff --git a/config/query-builder.php b/config/query-builder.php new file mode 100644 index 0000000..36d3d9f --- /dev/null +++ b/config/query-builder.php @@ -0,0 +1,63 @@ + [ + 'include' => 'include', + + 'filter' => 'filter', + + 'sort' => 'sort', + + 'fields' => 'fields', + + 'append' => 'append', + ], + + /* + * Related model counts are included using the relationship name suffixed with this string. + * For example: GET /users?include=postsCount + */ + 'count_suffix' => 'Count', + + /* + * Related model exists are included using the relationship name suffixed with this string. + * For example: GET /users?include=postsExists + */ + 'exists_suffix' => 'Exists', + + /* + * By default the package will throw an `InvalidFilterQuery` exception when a filter in the + * URL is not allowed in the `allowedFilters()` method. + */ + 'disable_invalid_filter_query_exception' => false, + + /* + * By default the package will throw an `InvalidSortQuery` exception when a sort in the + * URL is not allowed in the `allowedSorts()` method. + */ + 'disable_invalid_sort_query_exception' => false, + + /* + * By default the package will throw an `InvalidIncludeQuery` exception when an include in the + * URL is not allowed in the `allowedIncludes()` method. + */ + 'disable_invalid_includes_query_exception' => false, + + /* + * By default, the package expects relationship names to be snake case plural when using fields[relationship]. + * For example, fetching the id and name for a userOwner relation would look like this: + * GET /users?fields[user_owner]=id,name + * + * Set this to `false` if you don't want that and keep the requested relationship names as-is and allows you to + * request the fields using a camelCase relationship name: + * GET /users?fields[userOwner]=id,name + */ + 'convert_relation_names_to_snake_case_plural' => true, +]; diff --git a/routes/api.php b/routes/api.php index df5cd59..f3ccd1c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,5 +1,6 @@ get('user', [AuthController::class, 'user']); -Route::post('product', [ServiceRequestController::class, 'store']); \ No newline at end of file +Route::post('product', [ServiceRequestController::class, 'store']); +Route::get('product/{product}', [ServiceRequestController::class, 'show']); +// routes/api.php +Route::get('/appliances/{userId}', [ApplianceController::class, 'getUserAppliances']);