From e659983724f189b90fc30b478990726cc2d7f13c Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Fri, 8 Apr 2022 02:44:30 +0800 Subject: [PATCH] [api] handle no latest --- app/Http/Controllers/NewsController.php | 8 ++++- app/Http/Controllers/PollController.php | 6 ++-- nexus/Install/update/update.php | 46 ++++++++++++------------- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/app/Http/Controllers/NewsController.php b/app/Http/Controllers/NewsController.php index a766e85b..411e6e36 100644 --- a/app/Http/Controllers/NewsController.php +++ b/app/Http/Controllers/NewsController.php @@ -7,6 +7,7 @@ use App\Models\News; use App\Repositories\NewsRepository; use Carbon\Carbon; use Illuminate\Http\Request; +use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Support\Facades\Auth; use Illuminate\Validation\Rule; use Nexus\Database\NexusDB; @@ -104,10 +105,15 @@ class NewsController extends Controller { $user = Auth::user(); $result = News::query()->orderBy('id', 'desc')->first(); - $resource = new NewsResource($result); + if ($result) { + $resource = new NewsResource($result); + } else { + $resource = new JsonResource(null); + } $resource->additional([ 'site_info' => site_info(), ]); + /** * Visiting the home page is the same as viewing the latest news * @see functions.php line 2590 diff --git a/app/Http/Controllers/PollController.php b/app/Http/Controllers/PollController.php index 8e4ed42e..61b53ddb 100644 --- a/app/Http/Controllers/PollController.php +++ b/app/Http/Controllers/PollController.php @@ -6,6 +6,7 @@ use App\Http\Resources\PollResource; use App\Models\Poll; use App\Repositories\PollRepository; use Illuminate\Http\Request; +use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Support\Facades\Auth; use Illuminate\Validation\Rule; @@ -96,7 +97,6 @@ class PollController extends Controller } /** - * @todo 弃权选项 * @return array */ public function latest() @@ -130,9 +130,11 @@ class PollController extends Controller foreach ($answerStats as $index => &$value) { $value = number_format(($value / $poll->answers_count) * 100, 2) . '%'; } + $resource = new PollResource($poll); + } else { + $resource = new JsonResource(null); } - $resource = new PollResource($poll); $resource->additional([ 'selection' => $selection, 'answer_stats' => $answerStats, diff --git a/nexus/Install/update/update.php b/nexus/Install/update/update.php index b7d0b432..48304742 100644 --- a/nexus/Install/update/update.php +++ b/nexus/Install/update/update.php @@ -25,17 +25,16 @@ if ($currentStep == 1) { if ($currentStep == 2) { $tableRows = []; $versionTable = $versions = []; - $cacheKkey = '__versions_' . date('Ymd_H'); + $tableRows[] = [ + 'checkbox' => sprintf(''), + 'tag_name' => 'Manual', + 'name' => 'If there are changes that are not suitable for full coverage, please check this box and make sure you have updated the code manually', + 'published_at' => '---', + ]; try { $versions = $update->listVersions(); -// if (!empty($_SESSION[$cacheKkey])) { -// $update->doLog("get versions from session."); -// $versions = $_SESSION[$cacheKkey]; -// } else { -// $_SESSION[$cacheKkey] = $versions; -// } } catch (\Exception $exception) { - $error = $exception->getMessage(); + $update->doLog("can not fetch versions from github: " . $exception->getMessage()); } if (!$isPost) { $versionHeader = [ @@ -51,23 +50,22 @@ if ($currentStep == 2) { $update->doLog("no .env file, release time is github original"); $timezone = null; } - $tableRows[] = [ - 'checkbox' => sprintf(''), - 'tag_name' => 'Manual', - 'name' => 'If there are changes that are not suitable for full coverage, please check this box and make sure you have updated the code manually', - 'published_at' => '---', - ]; - $latestCommit = $update->getLatestCommit(); - $time = \Carbon\Carbon::parse($latestCommit['commit']['committer']['date']); - if ($timezone) { - $time->tz = $timezone; + + try { + $latestCommit = $update->getLatestCommit(); + $time = \Carbon\Carbon::parse($latestCommit['commit']['committer']['date']); + if ($timezone) { + $time->tz = $timezone; + } + $tableRows[] = [ + 'checkbox' => sprintf('', $latestCommit['sha']), + 'tag_name' => 'Latest development code', + 'name' => "Development testing only! Latest commit:" . $latestCommit['commit']['message'], + 'published_at' => $time->format('Y-m-d H:i:s'), + ]; + } catch (\Exception $exception) { + $update->doLog("can not fetch latest commit from github: " . $exception->getMessage()); } - $tableRows[] = [ - 'checkbox' => sprintf('', $latestCommit['sha']), - 'tag_name' => 'Latest development code', - 'name' => "Development testing only! Latest commit:" . $latestCommit['commit']['message'], - 'published_at' => $time->format('Y-m-d H:i:s'), - ]; foreach ($versions as $version) { if ($version['draft']) { continue;