Skip to content
This repository was archived by the owner on Apr 14, 2023. It is now read-only.

Commit 120216f

Browse files
committed
mini blog updates
1 parent b2e6224 commit 120216f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+5875
-6319
lines changed

app/Console/Kernel.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ protected function schedule(Schedule $schedule)
3838
if (getenv('APP_ENV') === self::ENV_PROD) {
3939
$schedule->command(CveCheckCommand::class)
4040
->hourlyAt(12);
41-
$schedule->command(ReadHostDataCommand::class)
42-
->hourlyAt(2);
43-
$schedule->command(ReadOperatingSystemDataCommand::class)
44-
->hourlyAt(3);
4541
$schedule->command(SitemapGeneratorCommand::class)
4642
->weekends();
4743
}

app/Http/Controllers/Api/v2/ApiBaseController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function createMetaData() : array
4848
];
4949
}
5050

51-
public function createSuccessResponse($data, $etag) : Response
51+
protected function createSuccessResponse($data, $etag) : Response
5252
{
5353
return $this->response
5454
->header('content-type', ConstantService::ACCEPT_TYPE)
@@ -60,7 +60,7 @@ public function createSuccessResponse($data, $etag) : Response
6060
->setContent($data);
6161
}
6262

63-
public function createResourceNotFoundResponse() : Response
63+
protected function createResourceNotFoundResponse() : Response
6464
{
6565

6666
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Controllers\Api\v2\Blog;
6+
7+
use App\Http\Controllers\Api\v2\ApiBaseController;
8+
use App\Models\Post;
9+
use App\Repositories\PostsRepository;
10+
use App\Transformers\Api\v2\PostsTransformer;
11+
use Illuminate\Http\Request;
12+
use Illuminate\Http\Response;
13+
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
14+
use League\Fractal\Resource\Collection;
15+
use League\Fractal\Resource\Item;
16+
17+
class PostsController extends ApiBaseController
18+
{
19+
/** @var Response */
20+
private $response;
21+
22+
/** @var PostsTransformer */
23+
private $transformer;
24+
25+
/** @var PostsRepository */
26+
private $postsRepository;
27+
28+
public function __construct(Response $response, PostsTransformer $transformer, PostsRepository $postsRepository)
29+
{
30+
parent::__construct($response);
31+
$this->transformer = $transformer;
32+
$this->postsRepository = $postsRepository;
33+
}
34+
35+
public function index() : Response
36+
{
37+
$posts = $this->postsRepository->get();
38+
$collection = $posts->getCollection();
39+
40+
$manager = $this->createManager();
41+
42+
$resources = new Collection($collection, $this->transformer, 'Posts');
43+
$resources->setMeta($this->createMetaData());
44+
$resources->setPaginator(new IlluminatePaginatorAdapter($posts));
45+
46+
$data = $manager->createData($resources)->toArray();
47+
48+
$etag = md5(json_encode($resources));
49+
50+
return $this->createSuccessResponse($data, $etag);
51+
}
52+
53+
public function fetch(Request $request) : Response
54+
{
55+
$post = $this->postsRepository->findPostBySlug($request->slug);
56+
57+
$manager = $this->createManager();
58+
59+
$resources = new Item($post, $this->transformer, 'Posts');
60+
$resources->setMeta($this->createMetaData());
61+
62+
$data = $manager->createData($resources)->toArray();
63+
64+
$etag = md5(json_encode($resources));
65+
66+
return $this->createSuccessResponse($data, $etag);
67+
}
68+
}

app/Http/Controllers/Api/v2/VulnerabilitiesController.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class VulnerabilitiesController extends ApiBaseController
2727

2828
public function __construct(Response $response, VulnerabilityRepository $vulnerabilityRepository, VulnerabilitiesTransformer $transformer)
2929
{
30-
$this->response = $response;
30+
parent::__construct($response);
3131
$this->vulnerabilityRepository = $vulnerabilityRepository;
3232
$this->transformer = $transformer;
3333
}
@@ -44,14 +44,8 @@ public function index() : Response
4444

4545
$data = $manager->createData($resources)->toArray();
4646

47-
$etag = md5($vulnerabilities);
47+
$etag = md5(json_encode($vulnerabilities));
4848

49-
return $this->response
50-
->setStatusCode(200)
51-
->setEtag($etag)
52-
->header('Content-type', 'application/json')
53-
->header('accept', 'application/json')
54-
->header('accept-encoding', 'identity')
55-
->setContent($data);
49+
return $this->createSuccessResponse($data, $etag);
5650
}
5751
}

app/Models/Blog/Post.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace App\Models\Blog;
4+
5+
use App\Models\User;
6+
use DateTime;
7+
use Illuminate\Database\Eloquent\Builder;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Database\Eloquent\Relations\HasOne;
10+
11+
class Post extends Model
12+
{
13+
public function getTitle() : string
14+
{
15+
return $this->title;
16+
}
17+
18+
public function getHeadline() : string
19+
{
20+
return $this->headline;
21+
}
22+
23+
public function getPost() : string
24+
{
25+
return $this->post;
26+
}
27+
28+
public function getSlug() : string
29+
{
30+
return $this->slug;
31+
}
32+
33+
public function getPublishedAt() : DateTime
34+
{
35+
return new DateTime($this->published_at);
36+
}
37+
38+
public function author() : HasOne
39+
{
40+
return $this->hasOne(User::class, 'id', 'user_id');
41+
}
42+
43+
public function scopeBySlug(Builder $query, string $slug) : Builder
44+
{
45+
return $query->where('slug', '=', $slug);
46+
}
47+
}

app/Models/Blog/Tag.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models\Blog;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Tag extends Model
8+
{
9+
//
10+
}

app/Models/Contact.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Contact extends Model
8+
{
9+
//
10+
}

app/Models/User.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@ class User extends Authenticatable
2626
protected $hidden = [
2727
'password', 'remember_token',
2828
];
29+
30+
public function getName() : string
31+
{
32+
return $this->name;
33+
}
34+
35+
public function getEmail() : string
36+
{
37+
return $this->email;
38+
}
2939
}

app/Repositories/PostsRepository.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Repositories;
6+
7+
use App\Models\Blog\Post;
8+
use Illuminate\Database\Eloquent\Collection;
9+
use Illuminate\Pagination\LengthAwarePaginator;
10+
11+
class PostsRepository
12+
{
13+
/** @var Post */
14+
private $post;
15+
16+
public function __construct(Post $post)
17+
{
18+
$this->post = $post;
19+
}
20+
21+
public function get() : LengthAwarePaginator
22+
{
23+
return $this->post->paginate();
24+
}
25+
26+
public function findPostBySlug(string $slug) : Post
27+
{
28+
return $this->post->bySlug($slug)->first();
29+
}
30+
}

app/Services/SitemapService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public function writeSitemap(string $path) : void
4646
->setChangeFrequency(Url::CHANGE_FREQUENCY_DAILY)
4747
->setPriority(0.9)
4848
)
49+
->add(Url::create('/blog')
50+
->setLastModificationDate(Carbon::yesterday())
51+
->setChangeFrequency(Url::CHANGE_FREQUENCY_DAILY)
52+
->setPriority(0.7)
53+
)
4954
->writeToFile($path);
5055
}
5156
}

0 commit comments

Comments
 (0)