Skip to content

Commit

Permalink
Merge pull request #31 from PRTIMES/feature/add-recommend
Browse files Browse the repository at this point in the history
ルーティングの修正
  • Loading branch information
kyoya0819 authored Aug 29, 2024
2 parents c01a4a8 + 97351bd commit e7a229b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 47 deletions.
15 changes: 10 additions & 5 deletions backend/app/Http/Controllers/Api/Company/ListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
namespace App\Http\Controllers\Api\Company;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\Company\List\InvokeRequest;
use App\Models\Company;
use App\UseCases\Company\ListFilterByIndustryIds;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ListController extends Controller
{
/**
* @param InvokeRequest $request
* @param Request $request
* @return JsonResponse
*/
public function __invoke(InvokeRequest $request): JsonResponse
public function __invoke(Request $request): JsonResponse
{
/* @var int[] $industry_ids */
$industry_ids = $request->validated()['industry_ids'];
$industry_ids_raw = $request->query("industry_ids");
if ($industry_ids_raw === null)
return response()->json();

$industry_ids = is_array($industry_ids_raw)
? array_map(fn ($industry_id) => (int) $industry_id, $industry_ids_raw)
: [(int) $industry_ids_raw];

$companies = ListFilterByIndustryIds::run($industry_ids);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ public function __invoke(Request $request): JsonResponse
/* @var PressRelease $press_release */

return [
"id" => $press_release->id,
"title" => $press_release->title,
"summary" => $press_release->summary
"summary" => $press_release->summary,
"url" => route(
"press-release.redirect",
[
"company_id" => $press_release->company_id,
"release_id" => $press_release->release_id
]
)
];
})
->toArray()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Controllers\Api\PressRelease;
namespace App\Http\Controllers\Web\PressRelease;

use App\Exceptions\HttpJsonResponseException;
use App\Http\Controllers\Controller;
Expand All @@ -9,41 +9,38 @@
use App\UseCases\PressRelease\FindByIdsAction;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ViewController extends Controller
class RedirectController extends Controller
{
/**
* @param string $company_id
* @param string $release_id
* @param Request $request
* @return JsonResponse
* @return RedirectResponse
* @throws Exception
*/
public function __invoke(Request $request): JsonResponse
public function __invoke(string $company_id, string $release_id, Request $request): RedirectResponse
{
$user_id = Auth::id();
if ($user_id === null || is_string($user_id)) // auth:sanctumで弾いているため最低限の例外
throw new Exception("想定外エラー");

$company_id = $request->get("company_id");
$release_id = $request->get("release_id");

$press_release = FindByIdsAction::run(company_id: $company_id, release_id: $release_id);
$press_release = FindByIdsAction::run(company_id: (int) $company_id, release_id: (int) $release_id);

if (!$press_release)
throw new HttpJsonResponseException(
400,
"notfound",
"PressRelease Not Found",
compact("company_id", "release_id")
);
abort(404);

/* @var Keyword $keyword */
foreach ($press_release->keywords() as $keyword) {
foreach ($press_release->keywords()->get() as $keyword) {

ViewHistoryUpdateOrCreateAction::run($user_id, $keyword->id);
}

return response()->json(["result" => "ok"]);
$company_id_str = str_pad($company_id, 9, 0, STR_PAD_LEFT);
$release_id_str = str_pad($release_id, 9, 0, STR_PAD_LEFT);
return response()->redirectTo("https://prtimes.jp/main/html/rd/p/$release_id_str.$company_id_str.html");
}
}
22 changes: 0 additions & 22 deletions backend/app/Http/Requests/Api/Company/List/InvokeRequest.php

This file was deleted.

3 changes: 1 addition & 2 deletions backend/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

Route::middleware("auth:sanctum")->group(function () {

Route::post("/press-release/view", PressRelease\ViewController::class);
Route::get("/press-release/recommend", PressRelease\RecommendController::class);

Route::post("/company/list", Company\ListController::class);
Route::get("/company/list", Company\ListController::class);
});
4 changes: 4 additions & 0 deletions backend/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@

use Illuminate\Support\Facades\Route;

Route::middleware("auth")->group(function () {

Route::get("/press-release/redirect/{company_id}/{release_id}", PressRelease\RedirectController::class)->name("press-release.redirect");
});

0 comments on commit e7a229b

Please sign in to comment.