This commit is contained in:
aslan 2024-03-20 13:09:39 +03:00
parent c3eed199ac
commit d5ace86b61
15 changed files with 483 additions and 0 deletions

View File

@ -0,0 +1,61 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\admin\StoreNewsRequest;
use App\Http\Requests\admin\UpdateNewsRequest;
use App\Models\News;
use Illuminate\Support\Facades\Storage;
class NewsController extends Controller
{
public function index()
{
$news = News::all()->sortDesc();
return view('admin.news.index', compact('news'));
}
public function create()
{
return view('admin.news.create');
}
public function store(StoreNewsRequest $request)
{
$validated = $request->validated();
$urlPhoto = Storage::put('public', $request->file('photo'));
$news = new News();
$news->name = $validated['name'];
$news->text = $validated['text'];
$news->photo = Storage::url($urlPhoto);
$news->save();
return redirect()->route('news.index');
}
public function show(News $news)
{
return view('admin.news.show', compact('news'));
}
public function edit(News $news)
{
return view('admin.news.edit', compact('news'));
}
public function update(UpdateNewsRequest $request, News $news)
{
$validated = $request->validated();
$news->name = $validated['name'];
$news->text = $validated['text'];
$news->save();
return redirect()->route('news.index');
}
public function destroy(News $news)
{
$news->delete();
return redirect()->route('news.index');
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace App\Http\Requests\admin;
use Illuminate\Foundation\Http\FormRequest;
class StoreNewsRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'text' => 'string',
'photo' => 'required|file',
];
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace App\Http\Requests\admin;
use Illuminate\Foundation\Http\FormRequest;
class UpdateNewsRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'text' => 'string',
];
}
}

19
app/Models/News.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class News extends Model
{
use HasFactory;
protected $fillable = [
'id',
'name',
'text',
'photo',
'created_at',
];
}

View File

@ -0,0 +1,18 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
class NewsFactory extends Factory
{
public function definition(): array
{
return [
'name' => fake()->name(),
'text' => fake()->text(),
'photo' => fake()->url(),
];
}
}

View File

@ -0,0 +1,30 @@
<?php
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('news', function (Blueprint $table) {
$table->id();
$table->string('name', 255);
$table->text('text');
$table->string('photo', 255);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('news');
}
};

View File

@ -0,0 +1,29 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class NewsSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
DB::table('news')->insert([
[
'name' => 'Первая новость',
'text' => fake()->realText(),
'photo' => '1',
],
[
'name' => 'Вторая новость',
'text' => fake()->realText(),
'photo' => '2',
],
]);
}
}

View File

@ -82,4 +82,9 @@ return [
'faculty_id' => 'Поле "Факультет" указывает на привязку к факультету',
'slug' => 'Поле "URL" нужно для отображения в браузере'
],
'news' => [
'name' => 'Поле "Название" - заголовок новости для отображения на сайте',
'text' => 'Поле "Текст" - текст новости для отображения на сайте',
'photo' => 'Поле "Путь к фото" - текст новости для отображения на сайте',
],
];

View File

@ -0,0 +1,68 @@
@php use App\Helpers\PositionHelper; @endphp
@extends('layouts.admin_layout')
@section('content')
@auth()
<div class="row">
<div class="col">
<h1 class=""> Создать Новость</h1>
{{ Form::open(['url' => route('news.store'), 'method' => 'POST', 'files'=>'true', 'class' => 'needs-validation', 'novalidate']) }}
<div class="col">
<div class="mt-3">
{{ Form::label('name', 'Название', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.news.name')]) }}
<span class="text-danger">*</span>
</div>
<div class="mt-1">
{{ Form::text('name', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.news.name'), 'required']) }}
<div class="invalid-feedback">
Поле "Название" обязательно!
</div>
</div>
<div class="text-danger">
@if ($errors->any())
{{ $errors->first('name') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('text', 'Текст', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.news.text')]) }}
<span class="text-danger">*</span>
</div>
<div class="mt-1">
{{ Form::textarea('text', '', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.news.text'), 'required']) }}
<div class="invalid-feedback">
Поле "Текст" обязательно!
</div>
</div>
<div class="text-danger">
@if ($errors->any())
{{ $errors->first('name') }}
@endif
</div>
<div class="mt-2">
{{ Form::label('photo', 'Путь к фото') }}
<span class="text-danger">*</span>
</div>
<div class="mt-2">
{{ Form::file('photo', ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.news.photo'), 'required']) }}
<div class="invalid-feedback">
Поле "Путь к фото" обязательно!
</div>
</div>
<div class="text-danger">
@if ($errors->any())
{{ $errors->first('photo') }}
@endif
</div>
<div class="mt-3">
{{ Form::submit('Создать', ['class' => 'btn btn-primary']) }}
</div>
</div>
{{ Form::close() }}
</div>
</div>
@endauth
@include('layouts.bootstrap_validation')
@endsection

View File

@ -0,0 +1,52 @@
@php use App\Helpers\PositionHelper; @endphp
@extends('layouts.admin_layout')
@section('content')
@auth()
<div class="row">
<div class="col">
<h1 class="">Изменить Новость</h1>
{{ Form::open(['url' => route('news.update', $news), 'method' => 'PATCH', 'files'=>'true', 'class' => 'needs-validation', 'novalidate']) }}
<div class="col">
<div class="mt-3">
{{ Form::label('name', 'Название', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.news.name')]) }}
<span class="text-danger">*</span>
</div>
<div class="mt-1">
{{ Form::text('name', $news->name, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.news.name'), 'required']) }}
<div class="invalid-feedback">
Поле "Название" обязательно!
</div>
</div>
<div class="text-danger">
@if ($errors->any())
{{ $errors->first('name') }}
@endif
</div>
<div class="mt-3">
{{ Form::label('text', 'Текст', ['data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.news.text')]) }}
<span class="text-danger">*</span>
</div>
<div class="mt-1">
{{ Form::textarea('text', $news->text, ['class' => 'form-control', 'data-bs-toggle' => "tooltip", 'data-bs-title' => __('tooltips.news.text'), 'required']) }}
<div class="invalid-feedback">
Поле "Текст" обязательно!
</div>
</div>
<div class="text-danger">
@if ($errors->any())
{{ $errors->first('name') }}
@endif
</div>
<div class="mt-3">
{{ Form::submit('Изменить', ['class' => 'btn btn-primary']) }}
</div>
</div>
{{ Form::close() }}
</div>
</div>
@endauth
@include('layouts.bootstrap_validation')
@endsection

View File

@ -0,0 +1,38 @@
@extends('layouts.admin_layout')
@section('content')
<div class="container">
<h2>Новости</h2>
<br>
<a href="{{ route('news.create') }}" class="btn btn-primary">Создать Новость</a>
<br>
<br>
<table class="table">
<thead class="border-b-2 border-solid border-black text-left" style="text-align: left">
<tr>
<th scope="col">Название</th>
<th scope="col">Текст</th>
<th scope="col">Действия</th>
</tr>
</thead>
<tbody>
@foreach($news as $item)
<tr class="">
<td><a href="{{ route('news.show', $item) }}">{{ $item->name }}</a></td>
<td>{{ Str::words($item->text, 10, '...') }}</td>
<td><a href="{{ route("news.edit", $item) }}"
class="btn btn-secondary">редактировать</a>
<a rel="nofollow" data-method="delete" data-confirm="Вы действительно хотите удалить?"
href="{{ route('news.destroy', $item) }}"
class="btn btn-danger">
удалить
</a>
</td>
</tr>
@endforeach
</tbody>
</table>
<br>
<br>
</div>
@endsection

View File

@ -0,0 +1,13 @@
@extends('layouts.admin_layout')
@section('content')
@auth()
<div class="container mt-4">
<h2>Название</h2>
<p>{{ $news->name }}</p>
<h2>Текст</h2>
<p>{{ $news->text }}</p>
<h2>Фото</h2>
<img src="{{ asset($news->photo) }}" alt="фото статьи">
</div>
@endauth
@endsection

View File

@ -48,6 +48,7 @@
<div class="row align-items-start">
<aside class="col-2">
<ul class="list-group ">
<li class="list-group-item {{ request()->is('admin/news*') ? 'active' : '' }}"><a class="{{ request()->is('admin/news*') ? 'link-light' : '' }}" href="{{ route('news.index') }}">Новости</a></li>
<li class="list-group-item {{ request()->is('admin/documents*') ? 'active' : '' }}"><a class="{{ request()->is('admin/documents*') ? 'link-light' : '' }}" href="{{ route('documents.index') }}">Документы</a></li>
<li class="list-group-item {{ request()->is('admin/admissions*') ? 'active' : '' }}"><a class="{{ request()->is('admin/admissions*') ? 'link-light' : '' }}" href="{{ route('admissions.index') }}">Экран Приема</a></li>
<li class="list-group-item {{ request()->is('admin/directions*') ? 'active' : '' }}"><a class="{{ request()->is('admin/directions*') ? 'link-light' : '' }}" href="{{ route('directions.index') }}">Направления</a></li>

View File

@ -14,6 +14,7 @@ use App\Http\Controllers\admin\Catalog\EducationalInstitutionController;
use App\Http\Controllers\admin\Catalog\FacultyController;
use App\Http\Controllers\admin\DocumentController;
use App\Http\Controllers\admin\UserController;
use App\Http\Controllers\NewsController;
use Rap2hpoutre\LaravelLogViewer\LogViewerController;
Route::middleware(['auth', 'verified'])->prefix('admin')->group(function () {
@ -70,5 +71,6 @@ Route::middleware(['auth', 'verified'])->prefix('admin')->group(function () {
Route::resources([
'/documents' => DocumentController::class,
'/users' => UserController::class,
'/news' => NewsController::class,
]);
});

View File

@ -0,0 +1,104 @@
<?php
namespace Tests\Feature\admin\catalog;
use App\Models\Department;
use App\Models\EducationalInstitution;
use App\Models\Faculty;
use App\Models\News;
use App\Models\User;
use Illuminate\Http\UploadedFile;
use Tests\TestCase;
class NewsTest extends TestCase
{
private User $user;
private News $news;
private array $data;
protected function setUp(): void
{
parent::setUp();
$this->news = News::factory()->create();
$this->data = News::factory()->make()->only([
'name',
'text',
]);
$this->user = User::factory()->create([
'name' => 'admin',
'email' => 'test@example.com',
'password' => 123456
]);
}
public function testIndexNewsPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('news.index'));
$response->assertOk();
}
public function testCreateNewsPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('news.create'));
$response->assertOk();
}
public function testStoreNews(): void
{
$file = UploadedFile::fake()->create('fake.jpg', 100);
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->post(route('news.store'), [...$this->data, 'photo' => $file]);
$response->assertRedirect(route('news.index'));
$this->assertDatabaseHas('news', $this->data);
}
public function testShowNewsPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('news.show', $this->news));
$response->assertOk();
}
public function testEditNewsPage(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->get(route('news.edit', $this->news));
$response->assertOk();
}
public function testUpdateNews(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->patch(route('news.update', $this->news), $this->data);
$response->assertRedirect(route('news.index'));
$this->assertDatabaseHas('news', $this->data);
}
public function testDestroyNews(): void
{
$response = $this->actingAs($this->user)
->withSession(['banned' => false])
->delete(route('news.destroy', $this->news));
$response->assertRedirect(route('news.index'));
$this->assertDatabaseMissing('news', $this->news->toArray());
}
}