-
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 #24 from PRTIMES/feature/add-recommend
レコメンド機能の作成(フェーズ4)
- Loading branch information
Showing
17 changed files
with
523 additions
and
48 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
34 changes: 34 additions & 0 deletions
34
backend/app/Http/Controllers/Api/Company/ListController.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,34 @@ | ||
<?php | ||
|
||
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; | ||
|
||
class ListController extends Controller | ||
{ | ||
/** | ||
* @param InvokeRequest $request | ||
* @return JsonResponse | ||
*/ | ||
public function __invoke(InvokeRequest $request): JsonResponse | ||
{ | ||
/* @var int[] $industry_ids */ | ||
$industry_ids = $request->validated()['industry_ids']; | ||
|
||
$companies = ListFilterByIndustryIds::run($industry_ids); | ||
|
||
return response()->json( | ||
$companies->map(function ($company) { | ||
/* @var Company $company */ | ||
return [ | ||
"id" => $company->id, | ||
"name" => $company->name | ||
]; | ||
})->toArray() | ||
); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
backend/app/Http/Requests/Api/Company/List/InvokeRequest.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,22 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Api\Company\List; | ||
|
||
use App\Http\Requests\Api\Request; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
|
||
class InvokeRequest extends Request | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, ValidationRule|array|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
"industry_ids" => ["required", "array"], | ||
"industry_ids.*" => ["integer", "min:1", "max:16"], | ||
]; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
namespace App\Libs; | ||
|
||
use Exception; | ||
use GuzzleHttp\Client as GuzzleClient; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
|
||
class PrTimesApi | ||
{ | ||
private GuzzleClient $guzzle; | ||
private array $header; | ||
|
||
private array $industries = [ | ||
1 => "水産・農林業", | ||
2 => "鉱業", | ||
3 => "建設業", | ||
4 => "製造業", | ||
5 => "電気・ガス業", | ||
6 => "倉庫・運輸関連業", | ||
7 => "情報通信", | ||
8 => "商業(卸売業、小売業)", | ||
9 => "金融・保険業", | ||
10 => "不動産業", | ||
11 => "水産・農林業", | ||
12 => "官公庁・地方自治体", | ||
13 => "飲食店・宿泊業", | ||
14 => "医療・福祉", | ||
15 => "教育・学習支援業", | ||
16 => "財団法人・社団法人・宗教法人" | ||
]; | ||
|
||
public function __construct( | ||
string $token, | ||
string $endpoint = "https://hackathon.stg-prtimes.net/" | ||
) | ||
{ | ||
$this->header = [ | ||
"Authorization" => "Bearer " . $token | ||
]; | ||
|
||
$this->guzzle = new GuzzleClient([ | ||
"base_uri" => "https://hackathon.stg-prtimes.net/" | ||
]); | ||
} | ||
|
||
/** | ||
* @param int $company_id | ||
* @param int $release_id | ||
* @return array{ | ||
* body: string, | ||
* company_id: int, | ||
* company_name: string, | ||
* created_at: string, | ||
* lead_paragraph: string, | ||
* like: int, | ||
* main_category_id: int, | ||
* main_category_name: string, | ||
* main_image: string, | ||
* main_image_fastly: string, | ||
* release_id: int, | ||
* release_type: string, | ||
* sub_category_id: string, | ||
* sub_category_name: string, | ||
* subtitle: string, | ||
* title: string, | ||
* url: string | ||
* } | ||
* @throws GuzzleException | ||
* @throws Exception | ||
*/ | ||
public function release(int $company_id, int $release_id): array | ||
{ | ||
$response = $this->guzzle->get("/api/companies/$company_id/releases/$release_id", ["headers" => $this->header]); | ||
|
||
if ($response->getStatusCode() !== 200) | ||
throw new Exception("PR TIMES API Error"); | ||
|
||
return json_decode($response->getBody()->getContents(), true); | ||
} | ||
|
||
/** | ||
* @param int $per_page | ||
* @param int $page | ||
* @param string $from_date | ||
* @return array<array{ | ||
* body: string, | ||
* company_id: int, | ||
* company_name: string, | ||
* created_at: string, | ||
* lead_paragraph: string, | ||
* like: int, | ||
* main_category_id: int, | ||
* main_category_name: string, | ||
* main_image: string, | ||
* main_image_fastly: string, | ||
* release_id: int, | ||
* release_type: string, | ||
* sub_category_id: string, | ||
* sub_category_name: string, | ||
* subtitle: string, | ||
* title: string, | ||
* url: string | ||
* }> | ||
* @throws GuzzleException | ||
* @throws Exception | ||
*/ | ||
public function releases( | ||
int $per_page = 10, | ||
int $page = 0, | ||
string $from_date = "2020-01-01" | ||
): array | ||
{ | ||
$response = $this->guzzle->get("/api/releases?per_page=$per_page&page=$page&from_date=$from_date", ["headers" => $this->header]); | ||
|
||
if ($response->getStatusCode() !== 200) | ||
throw new Exception("PR TIMES API Error"); | ||
|
||
return json_decode($response->getBody()->getContents(), true); | ||
} | ||
|
||
/** | ||
* @param int $company_id | ||
* @return array{ | ||
* company_id: int, | ||
* company_name: string, | ||
* president_name: string, | ||
* address: string, | ||
* phone: string, | ||
* description: string, | ||
* industry: string, | ||
* industry_id: int, | ||
* ipo_type: string, | ||
* capital: int, | ||
* foundation_date: string, | ||
* url: string, | ||
* twitter_screen_name: string | ||
* } | ||
* @throws GuzzleException | ||
* @throws Exception | ||
*/ | ||
public function company( | ||
int $company_id | ||
): array | ||
{ | ||
$response = $this->guzzle->get("/api/companies/$company_id", ["headers" => $this->header]); | ||
|
||
if ($response->getStatusCode() !== 200) | ||
throw new Exception("PR TIMES API Error"); | ||
|
||
$industries = array_flip($this->industries); | ||
|
||
$company_data = json_decode($response->getBody()->getContents(), true); | ||
if (!array_key_exists($company_data["industry"], $industries)) | ||
$company_data["industry_id"] = 17; | ||
else | ||
$company_data["industry_id"] = $industries[$company_data["industry"]]; | ||
return $company_data; | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
|
||
class Company extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $fillable = [ | ||
"name", | ||
"popularity", | ||
"company_id", | ||
"industry_id" | ||
]; | ||
|
||
/** | ||
* @return HasMany | ||
*/ | ||
public function press_releases(): HasMany | ||
{ | ||
return $this->hasMany(PressRelease::class, 'company_id', 'company_id'); | ||
} | ||
} |
Oops, something went wrong.