diff --git a/app/Http/Controllers/Api/Publications/PublicationsController.php b/app/Http/Controllers/Api/Publications/PublicationsController.php index e0db9dc..2118bc4 100644 --- a/app/Http/Controllers/Api/Publications/PublicationsController.php +++ b/app/Http/Controllers/Api/Publications/PublicationsController.php @@ -8,6 +8,7 @@ use App\Models\Publications\Publication; use App\Transformers\Publications\PublicationTransformer; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Auth; class PublicationsController extends Controller { protected Publication $model; @@ -22,14 +23,23 @@ class PublicationsController extends Controller { } public function index(Request $request): JsonResponse { - $query = $this->model->query(); - if ($page = Page::byUuid($request->get('page'))->first()) $query->where(['page_id' => $page->id]); + $where = ['is_published' => true]; + if (Auth::check()) { + if ($request->has('edit_mode')) { + if ($request->get('edit_mode')==='yes') { + unset($where['is_published']); + } + } + } + $query = $this->model->query()->orderBy('id', 'desc'); + if ($page = Page::byUuid($request->get('page'))->first()) $where[] = ['page_id' => $page->id]; + $query->where($where); $paginator = $query->paginate(config('app.pagination_limit')); return fractal($paginator, new PublicationTransformer())->respond(); } public function show(Request $request, $id): JsonResponse { - $model = $this->model->byUuid($id)->firstOrFail(); + $model = $this->model->bySlug($id)->firstOrFail(); return fractal($model, new PublicationTransformer())->respond(); } @@ -41,10 +51,18 @@ class PublicationsController extends Controller { public function update(Request $request, $uuid): void { } - public function destroy(Request $request, $uuid): JsonResponse { + public function destroy(Request $request, $uuid) { $model = $this->model->byUuid($uuid)->firstOrFail(); $model->delete(); return response()->json(null, 204); } + public function published(Request $request, $uuid): JsonResponse { + $model = $this->model->byUuid($uuid)->firstOrFail(); + $data = [ + 'is_published' => $request->get('publish') + ]; + $model->update($data); + return fractal($model, new PublicationTransformer())->respond(); + } } diff --git a/app/Models/Pages/Page.php b/app/Models/Pages/Page.php index 1809557..29fc752 100644 --- a/app/Models/Pages/Page.php +++ b/app/Models/Pages/Page.php @@ -96,9 +96,22 @@ class Page extends Model { public static function byUrl($url) { if ($url = trim($url, '/ ')) { $query = self::query(); - collect(explode('/', $url))->reverse()->values()->each(function($slug, $index) use ($query) { - if ($slug !== '') { - $index ? $query->nthParentSlug($index, $slug) : $query->bySlug($slug); + $reduceIndex = 0; + collect(explode('/', $url))->reverse()->values()->each(function($slug, $index) use ($query, &$reduceIndex) { + $exist = self::query()->where(['slug' => $slug])->exists(); + if ($exist) { + if ($slug !== '') { + $currentIndex = $index - $reduceIndex; + $currentIndex ? $query->nthParentSlug($currentIndex, $slug) : $query->bySlug($slug); + } + } else { + $reduceIndex++; + $exist = Publication::query()->where(['slug' => $slug])->exists(); + if ($exist) { + $query->whereHas('publications', function($query) use($slug) { + $query->where(['slug' => $slug]); + }); + } } }); return $query->first(); diff --git a/app/Models/Publications/Publication.php b/app/Models/Publications/Publication.php index a03476d..bd90cfb 100644 --- a/app/Models/Publications/Publication.php +++ b/app/Models/Publications/Publication.php @@ -28,6 +28,7 @@ class Publication extends Model { 'type', 'name', 'excerpt', + 'description', 'is_published' ]; diff --git a/app/Services/Forms/FormsService.php b/app/Services/Forms/FormsService.php index e03717b..ebff592 100644 --- a/app/Services/Forms/FormsService.php +++ b/app/Services/Forms/FormsService.php @@ -3,6 +3,7 @@ namespace App\Services\Forms; use App\Services\Forms\Pages\PageFormsServices; +use App\Services\Forms\Publications\PublicationFormsServices; use App\Services\Forms\Users\UserFormsServices; class FormsService { @@ -10,6 +11,7 @@ class FormsService { public static array $services = [ PageFormsServices::class, + PublicationFormsServices::class, UserFormsServices::class ]; diff --git a/app/Services/Forms/Pages/PageFormsServices.php b/app/Services/Forms/Pages/PageFormsServices.php index ca10d49..f660866 100644 --- a/app/Services/Forms/Pages/PageFormsServices.php +++ b/app/Services/Forms/Pages/PageFormsServices.php @@ -4,6 +4,6 @@ namespace App\Services\Forms\Pages; class PageFormsServices { public static array $services = [ - + 'published' => PublicationForms::class ]; } diff --git a/app/Services/Forms/Publications/PublicationForms.php b/app/Services/Forms/Publications/PublicationForms.php new file mode 100644 index 0000000..453bbb6 --- /dev/null +++ b/app/Services/Forms/Publications/PublicationForms.php @@ -0,0 +1,88 @@ + 'Создание новой новости', 'update' => 'Редактирование новости']; + + public function form(?string $id = null, array $data = []): array { + $model = Publication::byUuid($id)->first(); + $groups = [ + ['name' => 'common', 'fields' => $this->commonGroupFields($model)] + ]; + return ['title' => $this->formTitle($model), 'data' => $groups]; + } + + public function commonGroupFields(?Publication $model): array { + $fields = [ + [ + 'name' => 'name', + 'title' => 'Название', + 'type' => FieldType::STRING, + 'required' => true, + 'value' => $model->name ?? null + ], + [ + 'name' => 'excerpt', + 'title' => 'Краткое описание', + 'type' => FieldType::TEXT, + 'required' => true, + 'value' => $model->excerpt ?? null + ], + [ + 'name' => 'description', + 'title' => 'Описание', + 'type' => FieldType::HTML, + 'value' => $model->description ?? null + ], + [ + 'name' => 'poster_id', + 'title' => 'Фотография профиля', + 'type' => FieldType::IMAGE, + 'value' => ($model->poster ?? null) ? fractal($model->poster, new AssetTransformer()) : null + ] + ]; + return ['data' => $fields]; + } + + + + public function store(array $data): ?JsonResponse { + if (!empty($data['attach']['page_slug'])) { + $page = Page::query()->where(['slug' => $data['attach']['page_slug']])->first(); + $data['page_id'] = $page->id; + } + $data['user_id'] = Auth::user()->id; + $data['slug'] = Str::slug(Str::transliterate($data['name'])); + $data['type'] = PublicationType::NEWS; + if (!empty($data['poster_id'])) { + $asset = Asset::query()->where(['uuid' => $data['poster_id']])->first(); + $data['poster_id'] = $asset->id; + } + unset($data['attach']); + $model = Publication::create($data, true); + return fractal($model, new PublicationTransformer())->respond(); + } + + public function update(string $id, array $data): ?JsonResponse { + $model = Publication::byUuid($id)->firstOrFail(); + if (!empty($data['poster_id'])) { + $asset = Asset::query()->where(['uuid' => $data['poster_id']])->first(); + $data['poster_id'] = $asset->id; + } + $model->update($data); + return fractal($model->fresh(), new PublicationTransformer())->respond(); + } +} diff --git a/app/Services/Forms/Publications/PublicationFormsServices.php b/app/Services/Forms/Publications/PublicationFormsServices.php new file mode 100644 index 0000000..d0074e1 --- /dev/null +++ b/app/Services/Forms/Publications/PublicationFormsServices.php @@ -0,0 +1,9 @@ + PublicationForms::class + ]; +} diff --git a/app/Transformers/Publications/PublicationTransformer.php b/app/Transformers/Publications/PublicationTransformer.php index d1f6abe..e795b78 100644 --- a/app/Transformers/Publications/PublicationTransformer.php +++ b/app/Transformers/Publications/PublicationTransformer.php @@ -30,6 +30,7 @@ class PublicationTransformer extends TransformerAbstract { 'type' => $model->parsedType, 'name' => $model->name, 'excerpt' => $model->excerpt, + 'description' => $model->description, 'is_published' => boolval($model->is_published), 'created_at' => $model->created_at ? $model->created_at->toIso8601String() : null, 'updated_at' => $model->updated_at ? $model->updated_at->toIso8601String() : null diff --git a/database/migrations/2023_06_21_071812_add_description_to_publications_table.php b/database/migrations/2023_06_21_071812_add_description_to_publications_table.php new file mode 100644 index 0000000..b3aec44 --- /dev/null +++ b/database/migrations/2023_06_21_071812_add_description_to_publications_table.php @@ -0,0 +1,32 @@ +addColumn('text', 'description')->after('excerpt')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('publications', function (Blueprint $table) { + $table->dropColumn('description'); + }); + } +} diff --git a/routes/api.php b/routes/api.php index 3a689d7..bd62a53 100644 --- a/routes/api.php +++ b/routes/api.php @@ -56,4 +56,7 @@ Route::group(['middleware' => ['auth:api']], function() { Route::get('filters/{type}', 'Api\Forms\FormsController@filters'); Route::get('dadata/{inn}', 'Api\Companies\CompaniesController@getDataByInn'); + + Route::put('publications/published/{id}', 'Api\Publications\PublicationsController@published'); + Route::delete('publications/{id}', 'Api\Publications\PublicationsController@destroy'); });