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

New Feature: Series #193

Open
wants to merge 21 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
53 changes: 53 additions & 0 deletions app/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Article extends Model
'is_draft',
'is_original',
'published_at',
'series_id',
'number_in_series',
];

protected $casts = [
Expand Down Expand Up @@ -152,4 +154,55 @@ public function scopeCheckAuth($query)
}
return $query;
}

/**
* isPartOfSeries
*
* @author Daydevelops
*
* @param none
* @return bool
*/
public function isPartOfSeries() {
return !! $this->series_id;
}


/**
* nextArticle - returns the next article in the same series, or null if it doesn't exist
*
* @author Daydevelops
*
* @param none
* @return \App\Article
*/
public function nextArticle() {
if ($this->isPartOfSeries()) {
return Article::where([
'series_id'=>$this->series_id,
'number_in_series'=>$this->number_in_series + 1
])->first();
} else {
return null;
}
}

/**
* previousArticle - returns the previous article in the same series, or null if it doesn't exist
*
* @author Daydevelops
*
* @param none
* @return \App\Article
*/
public function previousArticle() {
if ($this->isPartOfSeries()) {
return Article::where([
'series_id'=>$this->series_id,
'number_in_series'=>$this->number_in_series - 1
])->first();
} else {
return null;
}
}
}
133 changes: 133 additions & 0 deletions app/Http/Controllers/Api/SeriesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace App\Http\Controllers\Api;

use \App\Series;
use \App\Article;
use Illuminate\Http\Request;

class SeriesController extends ApiController
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$keyword = $request->get('keyword');

$seriess = Series::checkAuth()
->when($keyword, function ($query) use ($keyword) {
$query->where('title', 'like', "%{$keyword}%");
})
->orderBy('created_at', 'desc')->paginate(10);

return $this->response->collection($seriess);
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|max:140',
'description' => 'required|max:280',
]);
Series::create($validatedData);
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Series $series)
{

return [
"articles_available"=>Article::where(['series_id'=>null])->get(),
"series"=>$series->load('articles')
];
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Series $series)
{
$validatedData = $request->validate([
'name' => 'required|max:140',
'description' => 'required|max:280',
]);
$series->update($validatedData);
}

public function updateOrder(Request $request, Series $series) {
// reset all articles in this series
$series->articles()->update([
'series_id'=>null,
'number_in_series'=>null
]);
foreach (request()->articles as $key => $a) {
Article::find($a)->update([
'series_id'=>$series->id,
'number_in_series'=>$key+1
]);
}
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Series $series)
{
// reset all articles in this series
$series->articles()->update([
'series_id'=>null,
'number_in_series'=>null
]);
$series->delete();

}

public function addArticle(Series $series, Article $article) {
$series->add($article);
}
public function destroyArticle(Series $series, Article $article) {
$series->remove($article);
}
}
4 changes: 3 additions & 1 deletion app/Http/Controllers/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function show(Request $request, $slug)

$article->increment('view_count');

$next_article = $article->nextArticle();

$ip = $request->getClientIp();

if ($ip == '::1') {
Expand All @@ -45,6 +47,6 @@ public function show(Request $request, $slug)

Visitor::log($article->id, $ip);

return view('article.show', compact('article'));
return view('article.show', compact('article','next_article'));
}
}
38 changes: 38 additions & 0 deletions app/Http/Controllers/SeriesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Http\Controllers;

use App\Article;
use App\Series;
use Illuminate\Http\Request;

class SeriesController extends Controller
{
/**
* Display the articles resource.
*
* @return mixed
*/
public function index()
{
$seriess = Series::with('articles')->paginate(config('blog.article.number'));
return view('series.index', compact('seriess'));
}

/**
* Display the article resource by article slug.
*
* @author Huiwang <[email protected]>
*
* @param Request $request
* @param $slug
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show(Request $request, Series $series)
{
$articles = $series->articles()
->orderBy('number_in_series', config('blog.article.sort'))
->paginate(config('blog.article.number'));
return view('series.show', compact('series','articles'));
}
}
69 changes: 69 additions & 0 deletions app/Series.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Series extends Model
{
protected $fillable = ['name', 'description'];



/**
* Get the articles for this series.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function articles()
{
return $this->hasMany(\App\Article::class)->orderBy('number_in_series', 'ASC');
}




/**
* add - adds an article to the end of this series
*
* @author Daydevelops
*
* @param \App\Article
*/
public function add($article)
{
$article->update([
'series_id' => $this->id,
'number_in_series' => $this->articles->count() + 1
]);
}

/**
* add - removes an article from this series
*
* @author Daydevelops
*
* @param \App\Article
*/

public function remove($article_to_delete)
{

$articles_to_update = $this->articles()
->where('number_in_series', '>', $article_to_delete->number_in_series)
->decrement('number_in_series', 1);

$article_to_delete->update([
'series_id' => null,
'number_in_series' => null
]);
}

public function scopeCheckAuth($query)
{
if (auth()->check() && auth()->user()->is_admin) {
$query->withoutGlobalScope(DraftScope::class);
}
return $query;
}
}
22 changes: 22 additions & 0 deletions app/Transformers/SeriesTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Transformers;

use App\Series;
use League\Fractal\TransformerAbstract;

class SeriesTransformer extends TransformerAbstract
{
protected $availableIncludes = [];

public function transform(Series $series)
{
return [
'id' => $series->id,
'name' => $series->name,
'created_at' => $series->created_at->diffForHumans(),
'updated_at' => $series->updated_at->toDateTimeString(),
];
}

}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"require": {
"php": ">=7.1.3",
"dflydev/apache-mime-types": "^1.0",
"doctrine/dbal": "^2.10",
"erusev/parsedown": "^1.7",
"fideloper/proxy": "~4.0",
"intervention/image": "^2.4",
Expand Down
Loading