diff --git a/app/Http/Controllers/Api/Adverts/AdvertsController.php b/app/Http/Controllers/Api/Adverts/AdvertsController.php new file mode 100644 index 00000000..f87fb203 --- /dev/null +++ b/app/Http/Controllers/Api/Adverts/AdvertsController.php @@ -0,0 +1,98 @@ +advertsService = $advertsService; + } + + /** + * Display a listing of the resource. + * + * @param AdvertsListRequest $request + * @return AdvertsResource + */ + public function index(AdvertsListRequest $request) + { + + $limit = $request->getLimit(); + $offset = $request->getOffset(); + + $adverts =$this->advertsService->pageApi($limit, $offset); + + //return response()->json($adverts); + //return AdvertResource::collection($adverts); + + return new AdvertsResource($adverts); + + } + + /** + * Store a newly created resource in storage. + * + * @param StoreAdvertRequest $request + * @return string + */ + public function store(StoreAdvertRequest $request) + { + $data = $request->getFormData(); + $this->advertsService->storeAdvert($data); + return 'saved'; + } + + /** + * Display the specified resource. + * + * @param Advert $advert + * @return AdvertWithMessagesResource + */ + public function show(Advert $advert) + { + return new AdvertWithMessagesResource($advert); + } + + /** + * Update the specified resource in storage. + * + * @param StoreAdvertRequest $request + * @param Advert $advert + * @return string + */ + public function update(StoreAdvertRequest $request, Advert $advert) + { + $this->advertsService->updateAdvert($advert, $request->all()); + return 'Updated'; + } + + /** + * Remove the specified resource from storage. + * + * @param Advert $advert + * @return void + */ + public function destroy(Advert $advert) + { + $this->advertsService->deleteAdvert($advert); + } +} diff --git a/app/Http/Controllers/Api/Adverts/Request/AdvertsListRequest.php b/app/Http/Controllers/Api/Adverts/Request/AdvertsListRequest.php new file mode 100644 index 00000000..e1902b27 --- /dev/null +++ b/app/Http/Controllers/Api/Adverts/Request/AdvertsListRequest.php @@ -0,0 +1,32 @@ + 'nullable|integer|min:1|max:'.self::MAX_PER_PAGE, + 'offset' => 'nullable|integer|min:0', + ]; + } + + public function getLimit(): int + { + return $this->request->get('limit', self::MAX_PER_PAGE); + } + + public function getOffset(): int + { + return $this->request->get('offset', 0); + } + +} diff --git a/app/Http/Controllers/Api/Adverts/Resources/AdvertResource.php b/app/Http/Controllers/Api/Adverts/Resources/AdvertResource.php new file mode 100644 index 00000000..ad3c01ed --- /dev/null +++ b/app/Http/Controllers/Api/Adverts/Resources/AdvertResource.php @@ -0,0 +1,26 @@ +user->name; + $town = $this->town->name; + $division = $this->division->name; + + $messages = $this->messages; + + $messagesData = []; + foreach ($messages as $message) { + $messagesData[] = ['userId' => $message->user_id, 'content' => $message->content]; + } + + $data['owner'] = $owner; + $data['town'] = $town; + $data['division'] = $division; + $data['messages'] = $messagesData; + + return $data; + + +// return [ +// 'id' => $this->id, +// 'title' => $this->title, +// ]; + + } +} diff --git a/app/Http/Controllers/Api/Adverts/Resources/AdvertsResource.php b/app/Http/Controllers/Api/Adverts/Resources/AdvertsResource.php new file mode 100644 index 00000000..135caf55 --- /dev/null +++ b/app/Http/Controllers/Api/Adverts/Resources/AdvertsResource.php @@ -0,0 +1,27 @@ + AdvertResource::collection($this), + //'limit' => $request->get('limit'), +// 'token' => Str::random(80), + ]; + } +} diff --git a/app/Http/Controllers/Auth/ApiTokenController.php b/app/Http/Controllers/Auth/ApiTokenController.php new file mode 100644 index 00000000..a1f428a6 --- /dev/null +++ b/app/Http/Controllers/Auth/ApiTokenController.php @@ -0,0 +1,34 @@ +user()->forceFill([ + 'api_token' => hash('sha256', $token), + ])->save(); + + return ['token' => $token]; + } + + // незабыть поменять в auth.php в 'guards'-->'api' --> 'hash' => true, + + +} diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index a51bf997..80265026 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -8,6 +8,7 @@ use Illuminate\Foundation\Auth\RegistersUsers; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; +use Illuminate\Support\Str; class RegisterController extends Controller { @@ -69,6 +70,7 @@ protected function create(array $data) 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), + 'api_token' => Str::random(80), ]); } } diff --git a/app/Services/Adverts/AdvertsService.php b/app/Services/Adverts/AdvertsService.php index 96d6838c..a6602109 100644 --- a/app/Services/Adverts/AdvertsService.php +++ b/app/Services/Adverts/AdvertsService.php @@ -51,6 +51,11 @@ public function showTownList() return ItemsDTO::make($town); } + public function pageApi(int $limit, int $offset) + { + return $this->advertCacheRepository->cachingPageApi($limit, $offset); + } + public function page($qty) { // return $this->advertCacheRepository->cachingPage($qty); diff --git a/app/Services/Adverts/Repositories/AdvertCacheRepository.php b/app/Services/Adverts/Repositories/AdvertCacheRepository.php index c3f3af4e..14331677 100644 --- a/app/Services/Adverts/Repositories/AdvertCacheRepository.php +++ b/app/Services/Adverts/Repositories/AdvertCacheRepository.php @@ -36,4 +36,15 @@ public function cachingPage($qty) } + public function cachingPageApi(int $limit, int $offset) + { + + $cacheKey = self::PAGE_CACHE_KEY.$limit.'_'.$offset; //вынести в класс-генератор ключа, генерит ключ под запрос, незабыть в пргреве + + return \Cache::remember($cacheKey, self::CACHE_TIME, function() use($limit, $offset){ + return $this->advertRepository->paginateListApi($limit, $offset); + }); + + } + } diff --git a/app/Services/Adverts/Repositories/AdvertRepositoryInterface.php b/app/Services/Adverts/Repositories/AdvertRepositoryInterface.php index d1573fc7..cc464e00 100644 --- a/app/Services/Adverts/Repositories/AdvertRepositoryInterface.php +++ b/app/Services/Adverts/Repositories/AdvertRepositoryInterface.php @@ -17,6 +17,7 @@ public function divisionList(); public function townList(); public function paginateList($qty); + public function paginateListApi(int $limit, int $offset); public function createFromArray(array $data): Advert; diff --git a/app/Services/Adverts/Repositories/EloquentAdvertRepository.php b/app/Services/Adverts/Repositories/EloquentAdvertRepository.php index a10451f9..e1c9a88a 100644 --- a/app/Services/Adverts/Repositories/EloquentAdvertRepository.php +++ b/app/Services/Adverts/Repositories/EloquentAdvertRepository.php @@ -38,6 +38,16 @@ public function paginateList($qty) return Advert::with('town', 'user', 'division')->paginate($qty); } + public function paginateListApi(int $limit, int $offset) + { + $advert = Advert::query(); + if ($limit) $advert->take($limit); + if($offset) $advert->skip($offset); + + $advert->with('town', 'user', 'division'); + return $advert->get(['adverts.*']); // ??? + } + public function createFromArray(array $data): Advert { // $advert = new Advert(); diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index e3a82c6d..0af0f8da 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -24,6 +24,7 @@ 'role'=>'user', 'email_verified_at' => now(), 'password' => Hash::make(123), //'password' => bcrypt('123'), + 'api_token' => Str::random(20), 'remember_token' => Str::random(10), ]; }); diff --git a/database/migrations/2020_07_23_120756_add_api_token_to_user.php b/database/migrations/2020_07_23_120756_add_api_token_to_user.php new file mode 100644 index 00000000..8d0da89e --- /dev/null +++ b/database/migrations/2020_07_23_120756_add_api_token_to_user.php @@ -0,0 +1,38 @@ +string('api_token', 80)->after('password') + ->unique() + ->nullable() + ->default(null); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('user', function (Blueprint $table) { + $table->dropColumn(['api_token']); + }); + + + } +} diff --git a/routes/api.php b/routes/api.php index bcb8b189..6151e7a7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -14,6 +14,16 @@ | */ -Route::middleware('auth:api')->get('/user', function (Request $request) { - return $request->user(); +//Route::middleware('auth:api')->get('/user', function (Request $request) { +// return $request->user(); +//}); + +Route::middleware('auth:api')->group(function (){ + + Route::apiResource('/adverts', 'Api\Adverts\AdvertsController'); + }); + + + +