Artem Petrov 5 rokov pred
rodič
commit
6234a83219

+ 12 - 8
app/Http/Controllers/PostsController.php

@@ -11,19 +11,27 @@ namespace App\Http\Controllers;
 use App\Http\Requests\PostRequest;
 use App\Jobs\SendMailJob;
 use App\Post;
+use App\Services\PostService;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Mail;
 
 class PostsController extends Controller
 {
+    protected $postService;
+
+    /**
+     * PostsController constructor.
+     */
+    public function __construct()
+    {
+        $this->postService = new PostService();
+    }
+
     /**
      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
      */
     public function index()
     {
-
-        SendMailJob::dispatch('the9thlaw@ukr.net');
-
         $posts = Post::all();
 
         return view('posts.index', [
@@ -57,11 +65,7 @@ class PostsController extends Controller
      */
     public function store(PostRequest $request)
     {
-        $post = new Post();
-        $post->fill($request->all() + [
-                'user_id' => Auth::id(),
-            ]);
-        $post->save();
+        $this->postService->create($request->all());
 
         return redirect(route('posts.index'));
     }

+ 5 - 6
app/Http/Requests/PostRequest.php

@@ -2,17 +2,16 @@
 /**
  * Created by PhpStorm.
  * User: artem
- * Date: 21.09.18
- * Time: 21:39
+ * Date: 31.10.18
+ * Time: 21:15
  */
 
 namespace App\Http\Requests;
 
 use Illuminate\Foundation\Http\FormRequest;
 
-class PostRequest extends FormRequest
+abstract class AbstractPostRequest extends FormRequest
 {
-
     /**
      * @return bool
      */
@@ -27,8 +26,8 @@ class PostRequest extends FormRequest
     public function rules()
     {
         return [
-            'title' => 'required|max:100',
+            'title' => 'required|max:100|unique:posts,title',
             'description' => 'required|max:5000',
         ];
     }
-}
+}

+ 14 - 0
app/Http/Requests/CreatePostRequest.php

@@ -0,0 +1,14 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: artem
+ * Date: 21.09.18
+ * Time: 21:39
+ */
+
+namespace App\Http\Requests;
+
+class CreatePostRequest extends AbstractPostRequest
+{
+
+}

+ 32 - 0
app/Http/Requests/UpdatePostRequest.php

@@ -0,0 +1,32 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: artem
+ * Date: 31.10.18
+ * Time: 21:09
+ */
+
+namespace App\Http\Requests;
+
+use Illuminate\Foundation\Http\FormRequest;
+
+class UpdatePostRequest extends AbstractPostRequest
+{
+    /**
+     * @return bool
+     */
+    public function authorize()
+    {
+        return true;
+    }
+
+    /**
+     * @return array
+     */
+    public function rules()
+    {
+        return parent::rules() + [
+            'title' => 'required|max:100|unique:posts,title' . $this->title,
+        ];
+    }
+}

+ 5 - 1
app/Providers/AppServiceProvider.php

@@ -2,6 +2,8 @@
 
 namespace App\Providers;
 
+use App\Repository\PostRepository;
+use App\Repository\PostRepositoryInterface;
 use Illuminate\Support\ServiceProvider;
 use Illuminate\Support\Facades\Schema;
 
@@ -24,6 +26,8 @@ class AppServiceProvider extends ServiceProvider
      */
     public function register()
     {
-        //
+        $this->app->bind(PostRepositoryInterface::class, function () {
+            return new PostRepository();
+        });
     }
 }

+ 24 - 0
app/Repository/PostApiRepository.php

@@ -0,0 +1,24 @@
+
+<?php
+/**
+ * Created by PhpStorm.
+ * User: artem
+ * Date: 31.10.18
+ * Time: 20:44
+ */
+
+namespace App\Repository;
+
+class PostApiRepository implements PostRepositoryInterface
+{
+
+    /**
+     * @param iterable $data
+     * @param int $userId
+     * @return mixed
+     */
+    public function create(iterable $data, int $userId)
+    {
+        // TODO: Implement create() method.
+    }
+}

+ 23 - 0
app/Repository/PostRepository.php

@@ -0,0 +1,23 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: artem
+ * Date: 31.10.18
+ * Time: 20:32
+ */
+
+namespace App\Repository;
+
+use App\Post;
+
+class PostRepository implements PostRepositoryInterface
+{
+    public function create(iterable $data, int $userId)
+    {
+        $post = new Post();
+        $post->fill($data + [
+                'user_id' => $userId,
+            ]);
+        $post->save();
+    }
+}

+ 20 - 0
app/Repository/PostRepositoryInterface.php

@@ -0,0 +1,20 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: artem
+ * Date: 31.10.18
+ * Time: 20:43
+ */
+
+namespace App\Repository;
+
+
+interface PostRepositoryInterface
+{
+    /**
+     * @param iterable $data
+     * @param int $userId
+     * @return mixed
+     */
+    public function create(iterable $data, int $userId);
+}

+ 37 - 0
app/Services/PostService.php

@@ -0,0 +1,37 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: artem
+ * Date: 31.10.18
+ * Time: 20:14
+ */
+
+namespace App\Services;
+
+use App\Jobs\SendMailJob;
+use App\Repository\PostRepositoryInterface;
+use Illuminate\Support\Facades\Auth;
+
+class PostService
+{
+    protected $repository;
+
+    /**
+     * PostService constructor.
+     */
+    public function __construct(PostRepositoryInterface $repository)
+    {
+        $this->repository = $repository;
+
+    }
+
+    /**
+     * @param iterable $data
+     */
+    public function create(iterable $data)
+    {
+        $this->repository->create($data, Auth::id);
+
+        SendMailJob::dispatch('the9thlaw@ukr.net');
+    }
+}