news
parent
0d07c9b02d
commit
842b26e659
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ class Publication extends Model {
|
|||
'type',
|
||||
'name',
|
||||
'excerpt',
|
||||
'description',
|
||||
'is_published'
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,6 @@ namespace App\Services\Forms\Pages;
|
|||
|
||||
class PageFormsServices {
|
||||
public static array $services = [
|
||||
|
||||
'published' => PublicationForms::class
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\Forms\Publications;
|
||||
|
||||
use App\Models\Asset;
|
||||
use App\Models\Objects\FieldType;
|
||||
use App\Models\Pages\Page;
|
||||
use App\Models\Publications\Publication;
|
||||
use App\Models\Publications\PublicationType;
|
||||
use App\Services\Forms\FormsService;
|
||||
use App\Transformers\Assets\AssetTransformer;
|
||||
use App\Transformers\Publications\PublicationTransformer;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class PublicationForms extends FormsService {
|
||||
public array $formTitles = ['create' => 'Создание новой новости', '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();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\Forms\Publications;
|
||||
|
||||
class PublicationFormsServices {
|
||||
public static array $services = [
|
||||
'publications' => PublicationForms::class
|
||||
];
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddDescriptionToPublicationsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('publications', function (Blueprint $table) {
|
||||
$table->addColumn('text', 'description')->after('excerpt')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('publications', function (Blueprint $table) {
|
||||
$table->dropColumn('description');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -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');
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue