-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from PRTIMES/feature/combine-front-and-back
バックエンド改修(その1)
- Loading branch information
Showing
6 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
backend/app/Http/Controllers/Api/PressRelease/CompanyController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\PressRelease; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\PressRelease; | ||
use App\UseCases\PressRelease\ListFilterByCompanyId as PressReleaseListFilterByCompanyId; | ||
use Exception; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Request; | ||
|
||
class CompanyController extends Controller | ||
{ | ||
const PRESS_RELEASE_COUNT = 10; | ||
|
||
/** | ||
* @param Request $request | ||
* @return JsonResponse | ||
* @throws Exception | ||
*/ | ||
public function __invoke(Request $request): JsonResponse | ||
{ | ||
$company_ids_raw = $request->query("company_ids"); | ||
if ($company_ids_raw === null) | ||
return response()->json(); | ||
|
||
$company_ids = is_array($company_ids_raw) | ||
? array_map(fn ($company_id) => (int) $company_id, $company_ids_raw) | ||
: [(int) $company_ids_raw]; | ||
|
||
$releases = PressReleaseListFilterByCompanyId::run($company_ids, self::PRESS_RELEASE_COUNT); | ||
|
||
return response()->json( | ||
$releases->map(function ($press_release) { | ||
/* @var PressRelease $press_release */ | ||
|
||
return [ | ||
"title" => $press_release->title, | ||
"summary" => $press_release->summary, | ||
"company_name" => $press_release->company->name, | ||
"url" => route( | ||
"press-release.redirect", | ||
[ | ||
"company_id" => $press_release->company_id, | ||
"release_id" => $press_release->release_id | ||
] | ||
) | ||
]; | ||
}) | ||
->toArray() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
backend/app/UseCases/PressRelease/ListFilterByCompanyId.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\UseCases\PressRelease; | ||
|
||
use App\Models\PressRelease; | ||
use Illuminate\Database\Eloquent\Collection; | ||
|
||
class ListFilterByCompanyId | ||
{ | ||
|
||
/** | ||
* @param array $company_ids | ||
* @param int $take | ||
* @return Collection<int, PressRelease> | ||
*/ | ||
public static function run( | ||
array $company_ids, | ||
int $take = 10 | ||
): Collection | ||
{ | ||
return PressRelease::with("company") | ||
->whereIn("company_id", $company_ids) | ||
->inRandomOrder()->take($take)->get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters