Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/public/storage
/storage/*.key
/vendor
.env
.env.backup
.phpunit.result.cache
Homestead.json
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function validator(array $data)
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'password' => ['required', 'string', 'min:1', 'confirmed'],
]);
}

Expand Down
24 changes: 22 additions & 2 deletions app/Http/Controllers/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,38 @@

class PostsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('can:update,post')->except(['index', 'userPosts', 'adminIndex', 'create']);
}

public function index()
{
$posts = Post::with('tags')->latest()->get();
return view('/index', compact('posts'));
}

public function userPosts()
{
// $posts = Post::where('owner_id', auth()->id())->with('tags')->latest()->get();
$posts = Auth()->user()->posts()->with('tags')->latest()->get();
return view('/posts.index', compact('posts'));
}

public function adminIndex() {
$posts = Post::with('tags')->latest()->get();
return view('/posts.admin-index', compact('posts'));
}

public function create()
{
return view('/posts.create');
}

public function store(Request $request)
{
$request->validate([
$attr = $request->validate([
'code' => 'required|unique:posts|regex:/[a-zA-Z0-9_-]+/',
'name' => 'required|min:5|max:100',
'description' => 'required|max:255',
Expand All @@ -32,9 +49,12 @@ public function store(Request $request)

if ($request->all(['published'])) {
$request->merge(['published' => 1]);
$attr['published'] = 1;
}

Post::create($request->all());
$attr['owner_id'] = auth()->id();

Post::create($attr);

return redirect('/');
}
Expand Down
15 changes: 15 additions & 0 deletions app/Http/Controllers/TagsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Http\Controllers;

use App\Tag;
use Illuminate\Http\Request;

class TagsController extends Controller
{
public function index(Tag $tag)
{
$posts = $tag->posts()->with('tags')->get();
return view('index', compact('posts'));
}
}
17 changes: 17 additions & 0 deletions app/Policies/PostPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Policies;

use App\Post;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class PostPolicy
{
use HandlesAuthorization;

public function update(User $user, Post $post)
{
return $post->owner_id == $user->id;
}
}
5 changes: 4 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use App\Tag;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -13,7 +14,9 @@ class AppServiceProvider extends ServiceProvider
*/
public function register()
{
//
view()->composer('layouts.aside-tags', function ($view) {
$view->with('tagsCloud', Tag::all());
});
}

/**
Expand Down
9 changes: 7 additions & 2 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@ class AuthServiceProvider extends ServiceProvider
*/
protected $policies = [
// 'App\Model' => 'App\Policies\ModelPolicy',
'App\Post' => 'App\Policies\PostPolicy',
];

/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
public function boot(\Illuminate\Contracts\Auth\Access\Gate $gate)
{
$this->registerPolicies();

//
$gate->before(function ($user) {
if ($user->id == 2) {
return true;
}
});
}
}
5 changes: 5 additions & 0 deletions app/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public function posts()
{
return $this->belongsToMany(Post::class);
}

public function getRouteKeyName()
{
return 'name';
}
}
4 changes: 4 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

public function posts() {
return $this->hasMany(Post::class, 'owner_id');
}
}
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
|
*/

'timezone' => 'UTC',
'timezone' => 'Europe/Moscow',

/*
|--------------------------------------------------------------------------
Expand Down
7 changes: 4 additions & 3 deletions database/dumps/apsky-laravel.sql
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ CREATE TABLE `posts` (

LOCK TABLES `posts` WRITE;
/*!40000 ALTER TABLE `posts` DISABLE KEYS */;
INSERT INTO `posts` VALUES (4,'4123','Post4','Post desc4','Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias beatae consequatur consequuntur, debitis dicta eos explicabo fugit labore molestiae, nam nemo odit placeat quae quisquam quos repellat repellendus tempore, voluptates?<script>hello</script>',1,'2020-08-26 17:05:56','2020-09-07 14:10:35'),(5,'12313','qweqe','qweqe','qweqeq',0,'2020-08-28 05:05:04','2020-08-28 05:05:04'),(6,'qweqe','qweqe','qweqe','qweqweqe',0,'2020-08-28 06:58:18','2020-08-28 06:58:18'),(7,'qweq','qweqe','qweqe','qweqe',0,'2020-08-28 07:01:17','2020-08-28 07:01:17'),(8,'1231qweq','qweqe','qweqeq','qweqeqe',0,'2020-08-28 07:20:33','2020-08-28 07:20:33'),(9,'qweqeqeqweqw','eeqweqeqe','qweqe','11',0,'2020-08-28 07:28:29','2020-08-28 07:28:29');
INSERT INTO `posts` VALUES (4,'4123','Post4','Post desc4','Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias beatae consequatur consequuntur, debitis dicta eos explicabo fugit labore molestiae, nam nemo odit placeat quae quisquam quos repellat repellendus tempore, voluptates?<script>hello</script>',1,'2020-08-26 17:05:56','2020-09-07 14:10:35'),(5,'12313','qweqe','qweqe','Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias beatae consequatur consequuntur, debitis dicta eos explicabo fugit labore molestiae, nam nemo odit placeat quae quisquam quos repellat repellendus tempore, voluptates?<script>hello</script>',0,'2020-08-28 05:05:04','2020-09-10 17:23:00'),(6,'qweqe','qweqe','qweqe','Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias beatae consequatur consequuntur, debitis dicta eos explicabo fugit labore molestiae, nam nemo odit placeat quae quisquam quos repellat repellendus tempore, voluptates?<script>hello</script>',0,'2020-08-28 06:58:18','2020-09-10 17:23:00'),(7,'qweq','qweqe','qweqe','Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias beatae consequatur consequuntur, debitis dicta eos explicabo fugit labore molestiae, nam nemo odit placeat quae quisquam quos repellat repellendus tempore, voluptates?<script>hello</script>',0,'2020-08-28 07:01:17','2020-09-10 17:23:00'),(8,'1231qweq','qweqe','qweqeq','Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias beatae consequatur consequuntur, debitis dicta eos explicabo fugit labore molestiae, nam nemo odit placeat quae quisquam quos repellat repellendus tempore, voluptates?<script>hello</script>',0,'2020-08-28 07:20:33','2020-09-10 17:23:00'),(9,'qweqeqeqweqw','eeqweqeqe','qweqe','Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias beatae consequatur consequuntur, debitis dicta eos explicabo fugit labore molestiae, nam nemo odit placeat quae quisquam quos repellat repellendus tempore, voluptates?<script>hello</script>',0,'2020-08-28 07:28:29','2020-09-10 17:23:01');
/*!40000 ALTER TABLE `posts` ENABLE KEYS */;
UNLOCK TABLES;

Expand Down Expand Up @@ -247,7 +247,7 @@ CREATE TABLE `users` (
`updated_at` timestamp NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
Expand All @@ -256,6 +256,7 @@ CREATE TABLE `users` (

LOCK TABLES `users` WRITE;
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
INSERT INTO `users` VALUES (2,'Павел','ap.sky@yandex.ru',NULL,'$2y$10$2yK9UmERiX.O4V7n7gdJiu/96XvQqbFHadj5ISipVZwLBYVOs6LMW',NULL,'2020-09-10 17:13:37','2020-09-10 17:13:37');
/*!40000 ALTER TABLE `users` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
Expand All @@ -268,4 +269,4 @@ UNLOCK TABLES;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2020-09-07 23:02:08
-- Dump completed on 2020-09-10 21:13:40
2 changes: 2 additions & 0 deletions database/migrations/2020_08_26_090725_create_posts_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function up()
$table->boolean('published')->default(0);
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));

$table->foreignId('owner_id')->constrained('users')->onDelete('cascade');
});
}

Expand Down
10 changes: 9 additions & 1 deletion resources/views/auth/login.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
@extends('layouts.app')

@section('header')
@include('layouts.base.header')
@endsection

@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="row justify-content-center mt-4">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Login') }}</div>
Expand Down Expand Up @@ -71,3 +75,7 @@
</div>
</div>
@endsection

@section('footer')
@include('layouts.base.footer')
@endsection
10 changes: 9 additions & 1 deletion resources/views/auth/register.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
@extends('layouts.app')

@section('header')
@include('layouts.base.header')
@endsection

@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="row justify-content-center mt-4">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
Expand Down Expand Up @@ -75,3 +79,7 @@
</div>
</div>
@endsection

@section('footer')
@include('layouts.base.footer')
@endsection
15 changes: 3 additions & 12 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<main class="py-4" style="min-height: 88vh">
<div class="container">
<section class="posts-section mb-2 row flex-column flex-sm-row">
<h3 class="posts-section__header col-12 order-2 order-sm-0 text-center">Most recent posts</h3>
<h3 class="posts-section__header col-12 order-2 order-sm-0 text-center">Latest posts</h3>

@if ($posts->count())
<div class="posts-section__posts order-2 order-sm-0 col-12 col-sm-8 col-lg-10 post">
Expand All @@ -27,7 +27,7 @@
@if($post->tags->isNotEmpty())
<div class="post__tags mb-2">
@foreach($post->tags as $tag)
<a href="#" class="badge badge-info text-white">{{ $tag->name }}</a>
<a href="/tags/{{ $tag->name }}" class="badge badge-info text-white">{{ $tag->name }}</a>
@endforeach
</div>
@endif
Expand All @@ -47,16 +47,7 @@
@endforeach
</div>

<div class="tags-cloud d-flex flex-column col-12 col-sm-4 col-lg-2 order-1">
<h3 class="tags-cloud__header text-center">Available Tags</h3>

<div class="tags-cloud__tags list-group flex-row flex-wrap flex-sm-column justify-content-start mb-3 mb-sm-0">
@foreach($tags as $tag)
<a href="#" class="btn btn-sm btn-info text-white m-1">{{ strtoupper($tag->name) }}</a>
@endforeach
</div>
</div>

@include('layouts.aside-tags')
@else
<p class="no-posts">Not available posts yet</p>
@endif
Expand Down
2 changes: 1 addition & 1 deletion resources/views/layouts/admin/admin-header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</li>

<li class="nav-item">
<a class="nav-link" href="{{ route('post-index') }}">{{ __('Posts') }}</a>
<a class="nav-link" href="{{ route('admin-post-index') }}">{{ __('Posts') }}</a>
</li>
</ul>

Expand Down
9 changes: 9 additions & 0 deletions resources/views/layouts/aside-tags.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="tags-cloud d-flex flex-column col-12 col-sm-4 col-lg-2 order-1">
@if($tagsCloud->isNotEmpty())
<h3 class="tags-cloud__header text-center">Available Tags</h3>

@include('layouts.posts.tags', ['tags' => $tagsCloud])
@else
<h3 class="tags-cloud__header text-center">Not available tags</h3>
@endif
</div>
4 changes: 2 additions & 2 deletions resources/views/layouts/base/header.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">
<div class="container">
<a class="navbar-brand" href="{{ url('/') }}">
<a class="navbar-brand" href="{{ route('home') }}">
APSKY <span class="text-danger">LARAVEL</span>
</a>

Expand All @@ -12,7 +12,7 @@
<!-- Left Side Of Navbar -->
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="{{ route('home') }}">{{ __('Home') }}</a>
<a class="nav-link" href="{{ route('user-posts') }}">{{ __('My posts') }}</a>
</li>

<li class="nav-item">
Expand Down
5 changes: 5 additions & 0 deletions resources/views/layouts/posts/tags.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="tags-cloud__tags list-group flex-row flex-wrap flex-sm-column justify-content-start mb-3 mb-sm-0">
@foreach($tags as $tag)
<a href="/tags/{{$tag->name}}" class="btn btn-sm btn-info text-white m-1">{{ strtoupper($tag->name) }}</a>
@endforeach
</div>
Loading