diff --git a/app/Http/Controllers/Posts/Archive.php b/app/Http/Controllers/Posts/Archive.php new file mode 100644 index 00000000..c9925ea0 --- /dev/null +++ b/app/Http/Controllers/Posts/Archive.php @@ -0,0 +1,42 @@ +input('year'); + $month = $request->input('month'); + + if (is_null($year) || is_null($month)) { + return response()->json([], 400); + } + + $posts = self::_resolvedPosts()->whereMonth('date', '=', $month)->whereYear('date', '=', $year); + + return self::_after($request, $posts); + } + + public function countByMonth(Request $request) + { + $data = Posts::select(DB::raw('(COUNT(*)) as count'), DB::raw('YEAR(date) as year'), DB::raw('MONTH(date) as month')) + ->where('published', true) + ->groupBy(DB::raw('year'), DB::raw('month')) + ->orderBy('year', 'desc')->orderBy('month', 'desc') + ->get(); + + $maxDate = $data->count() ? Carbon::parse(Posts::latest()->first()->date) : null; + $modSince = self::_modSince($request); + if (isset($maxDate) && $maxDate->lte($modSince)) { + return response()->json([], 304); + } + + return $data; + } +} diff --git a/app/Http/Controllers/Posts/ByAuthor.php b/app/Http/Controllers/Posts/ByAuthor.php new file mode 100644 index 00000000..18339755 --- /dev/null +++ b/app/Http/Controllers/Posts/ByAuthor.php @@ -0,0 +1,21 @@ +input('id'); + + if (is_null($authorId)) { + return response()->json([], 400); + } + + $posts = self::_resolvedPosts()->where('author_id', '=', $authorId); + + return self::_after($request, $posts); + } +} diff --git a/app/Http/Controllers/Posts/ById.php b/app/Http/Controllers/Posts/ById.php new file mode 100644 index 00000000..823f3015 --- /dev/null +++ b/app/Http/Controllers/Posts/ById.php @@ -0,0 +1,25 @@ +input('id'); + if (is_null($postId)) { + return response()->json([], 400); + } + + $post = self::_resolvedPosts()->where('id', '=', $postId)->get()->first(); + + $maxDate = null; + if (!is_null($post)) { + $maxDate = $post->updated_at; + } + + return self::_after($request, $post, $maxDate); + } +} diff --git a/app/Http/Controllers/Posts/ByLabel.php b/app/Http/Controllers/Posts/ByLabel.php new file mode 100644 index 00000000..bba14fc4 --- /dev/null +++ b/app/Http/Controllers/Posts/ByLabel.php @@ -0,0 +1,23 @@ +input('id'); + + if (is_null($labelId)) { + return response()->json([], 400); + } + + $posts = self::_resolvedPosts()->whereHas('labels', function ($query) use ($labelId) { + $query->where('id', '=', $labelId); + }); + + return self::_after($request, $posts); + } +} diff --git a/app/Http/Controllers/Posts/CountByMonth.php b/app/Http/Controllers/Posts/CountByMonth.php new file mode 100644 index 00000000..abd31dc5 --- /dev/null +++ b/app/Http/Controllers/Posts/CountByMonth.php @@ -0,0 +1,7 @@ +input('id'); + $token = $request->input('token'); + if (is_null($postId)) { + return response()->json([], 400); + } + + $post = self::_resolvedPosts(true)->where('published', '!=', true)->where('id', '=', $postId)->get()->first(); + + if (empty($post)) { + return response()->json([], 404); + } + + if (empty($token) || $token != $post->previewToken) { + return response()->json([], 401); + } + + $maxDate = null; + if (!is_null($post)) { + $maxDate = $post->updated_at; + } + + return self::_after($request, $post, $maxDate); + } +} diff --git a/app/Http/Controllers/Posts/ListFeatured.php b/app/Http/Controllers/Posts/ListFeatured.php new file mode 100644 index 00000000..17d85bf4 --- /dev/null +++ b/app/Http/Controllers/Posts/ListFeatured.php @@ -0,0 +1,22 @@ +where('featured', '=', true)->orderBy('date', 'desc')->take($NUMBER_TO_RETURN)->get(); + $count = $result->count(); + if ($count !== $NUMBER_TO_RETURN) { + $nonFeatured = self::_resolvedPosts()->where('featured', '=', false)->orderBy('date', 'desc')->take($NUMBER_TO_RETURN - $count)->get(); + $result = $result->merge($nonFeatured); + } + + return self::_after($request, $result); + } +} diff --git a/app/Http/Controllers/Posts/ListPosts.php b/app/Http/Controllers/Posts/ListPosts.php new file mode 100644 index 00000000..64ed2f02 --- /dev/null +++ b/app/Http/Controllers/Posts/ListPosts.php @@ -0,0 +1,15 @@ +input('term'); + + if (is_null($searchTerm)) { + return response()->json([], 400); + } + $posts = self::_resolvedPosts()->where(function ($query) use ($searchTerm) { + $query->where('content', 'like', '%'.$searchTerm.'%') + ->orWhere('description', 'like', '%'.$searchTerm.'%') + ->orWhere('title', 'LIKE', "%$searchTerm%"); + }); + + return self::_after($request, $posts); + } +} diff --git a/app/Http/Controllers/PostsController.php b/app/Http/Controllers/PostsController.php index 6855cc07..86fa4b45 100644 --- a/app/Http/Controllers/PostsController.php +++ b/app/Http/Controllers/PostsController.php @@ -2,11 +2,16 @@ namespace App\Http\Controllers; -use Illuminate\Http\Request; use App\Models\Posts; use App\Markdown; -use DB; -use Carbon\Carbon; +use App\Http\Controllers\Posts\ById; +use App\Http\Controllers\Posts\GetPreview; +use App\Http\Controllers\Posts\ListFeatured; +use App\Http\Controllers\Posts\ByAuthor; +use App\Http\Controllers\Posts\Archive; +use App\Http\Controllers\Posts\ListPosts; +use App\Http\Controllers\Posts\ByLabel; +use App\Http\Controllers\Posts\Search; class PostsController extends Controller { @@ -16,144 +21,14 @@ class PostsController extends Controller } const PAGESIZE = 20; - public function byId(Request $request) - { - $postId = $request->input('id'); - if (is_null($postId)) { - return response()->json([], 400); - } - - $post = self::_resolvedPosts()->where('id', '=', $postId)->get()->first(); - - $maxDate = null; - if (!is_null($post)) { - $maxDate = $post->updated_at; - } - - return self::_after($request, $post, $maxDate); - } - - public function getPreview(Request $request) - { - $postId = $request->input('id'); - $token = $request->input('token'); - if (is_null($postId)) { - return response()->json([], 400); - } - - $post = self::_resolvedPosts(true)->where('published', '!=', true)->where('id', '=', $postId)->get()->first(); - - if (empty($post)) { - return response()->json([], 404); - } - - if (empty($token) || $token != $post->previewToken) { - return response()->json([], 401); - } - - $maxDate = null; - if (!is_null($post)) { - $maxDate = $post->updated_at; - } - - return self::_after($request, $post, $maxDate); - } - - public function listFeaturedPosts(Request $request) - { - $NUMBER_TO_RETURN = 5; - - $result = self::_resolvedPosts()->where('featured', '=', true)->orderBy('date', 'desc')->take($NUMBER_TO_RETURN)->get(); - $count = $result->count(); - if ($count !== $NUMBER_TO_RETURN) { - $nonFeatured = self::_resolvedPosts()->where('featured', '=', false)->orderBy('date', 'desc')->take($NUMBER_TO_RETURN - $count)->get(); - $result = $result->merge($nonFeatured); - } - - return self::_after($request, $result); - } - - public function byAuthor(Request $request) - { - $authorId = $request->input('id'); - - if (is_null($authorId)) { - return response()->json([], 400); - } - - $posts = self::_resolvedPosts()->where('author_id', '=', $authorId); - - return self::_after($request, $posts); - } - - public function byYearMonth(Request $request) - { - $year = $request->input('year'); - $month = $request->input('month'); - - if (is_null($year) || is_null($month)) { - return response()->json([], 400); - } - - $posts = self::_resolvedPosts()->whereMonth('date', '=', $month)->whereYear('date', '=', $year); - - return self::_after($request, $posts); - } - - public function listPosts(Request $request) - { - $posts = self::_resolvedPosts(); - - return self::_after($request, $posts, null, false); - } - - public function byLabel(Request $request) - { - $labelId = $request->input('id'); - - if (is_null($labelId)) { - return response()->json([], 400); - } - - $posts = self::_resolvedPosts()->whereHas('labels', function ($query) use ($labelId) { - $query->where('id', '=', $labelId); - }); - - return self::_after($request, $posts); - } - - public function search(Request $request) - { - $searchTerm = $request->input('term'); - - if (is_null($searchTerm)) { - return response()->json([], 400); - } - $posts = self::_resolvedPosts()->where(function ($query) use ($searchTerm) { - $query->where('content', 'like', '%'.$searchTerm.'%') - ->orWhere('description', 'like', '%'.$searchTerm.'%') - ->orWhere('title', 'LIKE', "%$searchTerm%"); - }); - - return self::_after($request, $posts); - } - - public function countByMonth(Request $request) - { - $data = Posts::select(DB::raw('(COUNT(*)) as count'), DB::raw('YEAR(date) as year'), DB::raw('MONTH(date) as month')) - ->where('published', true) - ->groupBy(DB::raw('year'), DB::raw('month')) - ->orderBy('year', 'desc')->orderBy('month', 'desc') - ->get(); - - $maxDate = $data->count() ? Carbon::parse(Posts::latest()->first()->date) : null; - $modSince = self::_modSince($request); - if (isset($maxDate) && $maxDate->lte($modSince)) { - return response()->json([], 304); - } - - return $data; - } + use ById; + use GetPreview; + use ListFeatured; + use ByAuthor; + use Archive; + use ListPosts; + use ByLabel; + use Search; protected static function _after($request, $result, $maxDate = null, $makeThumbnail = true) {