Skip to content

Commit

Permalink
Merge pull request #28 from PRTIMES/feature/add-recommend
Browse files Browse the repository at this point in the history
レコメンド機能の作成(フェーズ6)
  • Loading branch information
kyoya0819 authored Aug 29, 2024
2 parents 5d05e52 + 12d3db6 commit 8642e84
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public function __invoke(Request $request): JsonResponse
if ($user_id === null || is_string($user_id)) // auth:sanctumで弾いているため最低限の例外
throw new Exception("想定外エラー");

$love = PressReleaseListFilterByViewHistory::run($user_id, self::PRESS_RELEASE_COUNT * 0.7, "love");
$like = PressReleaseListFilterByViewHistory::run($user_id, self::PRESS_RELEASE_COUNT * 0.2, "like");
$love = PressReleaseListFilterByViewHistory::run($user_id, "love", self::PRESS_RELEASE_COUNT * 0.7);
$like = PressReleaseListFilterByViewHistory::run($user_id, "like", self::PRESS_RELEASE_COUNT * 0.2);

$neutral = PressReleaseListFilterByViewHistory::run(
$user_id,
self::PRESS_RELEASE_COUNT - $love->count() - $like->count(),
"neutral"
"neutral",
self::PRESS_RELEASE_COUNT - $love->count() - $like->count()
);

$recommend = $neutral->merge($love)->merge($like)->shuffle();
Expand Down
20 changes: 11 additions & 9 deletions backend/app/UseCases/PressRelease/ListFilterByViewHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ public static function run(
int $take = 20
): Collection
{
return PressRelease::with("keywords.view_histories")->whereHas("keywords.view_histories", function ($query) use ($user_id, $mode) {
$query->where("user_id", $user_id)
->when($mode === "love", function ($query) {
$query->where("score", ">", 10);
})
->when($mode === "like", function ($query) {
$query->where("score", ">", 2)
->where("score", "<", 8);
});
return PressRelease::when($mode !== "neutral", function ($query) use ($user_id, $mode) {
$query->whereHas("keywords.view_histories", function ($query) use ($user_id, $mode) {
$query->where("user_id", $user_id)
->when($mode === "love", function ($query) {
$query->where("score", ">", 10);
})
->when($mode === "like", function ($query) {
$query->where("score", ">", 2)
->where("score", "<", 8);
});
});
})->inRandomOrder()->take($take)->get();
}
}

0 comments on commit 8642e84

Please sign in to comment.