[api] handle no latest

This commit is contained in:
xiaomlove
2022-04-08 02:44:30 +08:00
parent 196950885b
commit e659983724
3 changed files with 33 additions and 27 deletions
+7 -1
View File
@@ -7,6 +7,7 @@ use App\Models\News;
use App\Repositories\NewsRepository; use App\Repositories\NewsRepository;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
use Nexus\Database\NexusDB; use Nexus\Database\NexusDB;
@@ -104,10 +105,15 @@ class NewsController extends Controller
{ {
$user = Auth::user(); $user = Auth::user();
$result = News::query()->orderBy('id', 'desc')->first(); $result = News::query()->orderBy('id', 'desc')->first();
$resource = new NewsResource($result); if ($result) {
$resource = new NewsResource($result);
} else {
$resource = new JsonResource(null);
}
$resource->additional([ $resource->additional([
'site_info' => site_info(), 'site_info' => site_info(),
]); ]);
/** /**
* Visiting the home page is the same as viewing the latest news * Visiting the home page is the same as viewing the latest news
* @see functions.php line 2590 * @see functions.php line 2590
+4 -2
View File
@@ -6,6 +6,7 @@ use App\Http\Resources\PollResource;
use App\Models\Poll; use App\Models\Poll;
use App\Repositories\PollRepository; use App\Repositories\PollRepository;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
@@ -96,7 +97,6 @@ class PollController extends Controller
} }
/** /**
* @todo 弃权选项
* @return array * @return array
*/ */
public function latest() public function latest()
@@ -130,9 +130,11 @@ class PollController extends Controller
foreach ($answerStats as $index => &$value) { foreach ($answerStats as $index => &$value) {
$value = number_format(($value / $poll->answers_count) * 100, 2) . '%'; $value = number_format(($value / $poll->answers_count) * 100, 2) . '%';
} }
$resource = new PollResource($poll);
} else {
$resource = new JsonResource(null);
} }
$resource = new PollResource($poll);
$resource->additional([ $resource->additional([
'selection' => $selection, 'selection' => $selection,
'answer_stats' => $answerStats, 'answer_stats' => $answerStats,
+22 -24
View File
@@ -25,17 +25,16 @@ if ($currentStep == 1) {
if ($currentStep == 2) { if ($currentStep == 2) {
$tableRows = []; $tableRows = [];
$versionTable = $versions = []; $versionTable = $versions = [];
$cacheKkey = '__versions_' . date('Ymd_H'); $tableRows[] = [
'checkbox' => sprintf('<input type="radio" name="version_url" value="manual"/>'),
'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 { try {
$versions = $update->listVersions(); $versions = $update->listVersions();
// if (!empty($_SESSION[$cacheKkey])) {
// $update->doLog("get versions from session.");
// $versions = $_SESSION[$cacheKkey];
// } else {
// $_SESSION[$cacheKkey] = $versions;
// }
} catch (\Exception $exception) { } catch (\Exception $exception) {
$error = $exception->getMessage(); $update->doLog("can not fetch versions from github: " . $exception->getMessage());
} }
if (!$isPost) { if (!$isPost) {
$versionHeader = [ $versionHeader = [
@@ -51,23 +50,22 @@ if ($currentStep == 2) {
$update->doLog("no .env file, release time is github original"); $update->doLog("no .env file, release time is github original");
$timezone = null; $timezone = null;
} }
$tableRows[] = [
'checkbox' => sprintf('<input type="radio" name="version_url" value="manual"/>'), try {
'tag_name' => 'Manual', $latestCommit = $update->getLatestCommit();
'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', $time = \Carbon\Carbon::parse($latestCommit['commit']['committer']['date']);
'published_at' => '---', if ($timezone) {
]; $time->tz = $timezone;
$latestCommit = $update->getLatestCommit(); }
$time = \Carbon\Carbon::parse($latestCommit['commit']['committer']['date']); $tableRows[] = [
if ($timezone) { 'checkbox' => sprintf('<input type="radio" name="version_url" value="development|%s"/>', $latestCommit['sha']),
$time->tz = $timezone; '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('<input type="radio" name="version_url" value="development|%s"/>', $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) { foreach ($versions as $version) {
if ($version['draft']) { if ($version['draft']) {
continue; continue;