Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Split up PostsController into multiple files #355

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions app/Http/Controllers/Posts/Archive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Http\Controllers\Posts;

use Illuminate\Http\Request;
use App\Models\Posts;
use DB;
use Carbon\Carbon;

trait Archive
{
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 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;
}
}
21 changes: 21 additions & 0 deletions app/Http/Controllers/Posts/ByAuthor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Http\Controllers\Posts;

use Illuminate\Http\Request;

trait ByAuthor
{
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);
}
}
25 changes: 25 additions & 0 deletions app/Http/Controllers/Posts/ById.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Http\Controllers\Posts;

use Illuminate\Http\Request;

trait ByID
{
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);
}
}
23 changes: 23 additions & 0 deletions app/Http/Controllers/Posts/ByLabel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Controllers\Posts;

use Illuminate\Http\Request;

trait ByLabel
{
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);
}
}
7 changes: 7 additions & 0 deletions app/Http/Controllers/Posts/CountByMonth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App\Http\Controllers\Posts;

trait CountByMonth
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is unused, could be deleted

{
}
34 changes: 34 additions & 0 deletions app/Http/Controllers/Posts/GetPreview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Http\Controllers\Posts;

use Illuminate\Http\Request;

trait GetPreview
{
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);
}
}
22 changes: 22 additions & 0 deletions app/Http/Controllers/Posts/ListFeatured.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Controllers\Posts;

use Illuminate\Http\Request;

trait ListFeatured
{
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);
}
}
15 changes: 15 additions & 0 deletions app/Http/Controllers/Posts/ListPosts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Http\Controllers\Posts;

use Illuminate\Http\Request;

trait ListPosts
{
public function listPosts(Request $request)
{
$posts = self::_resolvedPosts();

return self::_after($request, $posts, null, false);
}
}
24 changes: 24 additions & 0 deletions app/Http/Controllers/Posts/Search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Controllers\Posts;

use Illuminate\Http\Request;

trait Search
{
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);
}
}
157 changes: 16 additions & 141 deletions app/Http/Controllers/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
{
Expand Down