Skip to content

Commit 88b6c11

Browse files
authored
Merge pull request #5 from ren807/issue-1
Issue 1
2 parents 4d216b1 + 2ff5ba5 commit 88b6c11

31 files changed

+986
-24
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Services\PostService;
6+
use Illuminate\Http\Request;
7+
8+
class PostController extends Controller
9+
{
10+
private $post;
11+
12+
public function __construct(PostService $post_service, Request $request)
13+
{
14+
$this->post = $post_service;
15+
$this->post->setPagerInfo($request->input('page_id'));
16+
}
17+
18+
public function index()
19+
{
20+
$userId = 1;
21+
$posts = [];
22+
23+
$shopDetails = $this->post->getShopDetails();
24+
$favorites = $this->post->getFavorites($userId);
25+
$pagerInfo = $this->post->getPagerInfo();
26+
27+
$posts = [
28+
'shopDetails' => $shopDetails,
29+
'favorites' => $favorites,
30+
'pagerInfo' => $pagerInfo,
31+
];
32+
33+
return View('posts.index', ['posts' => $posts]);
34+
}
35+
}

app/Models/Favorite.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Favorite extends Model
9+
{
10+
/** @use HasFactory<\Database\Factories\FavoriteFactory> */
11+
use HasFactory;
12+
}

app/Models/Image.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Image extends Model
9+
{
10+
/** @use HasFactory<\Database\Factories\ImageFactory> */
11+
use HasFactory;
12+
}

app/Models/Post.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Post extends Model
9+
{
10+
/** @use HasFactory<\Database\Factories\PostFactory> */
11+
use HasFactory;
12+
}

app/Models/PostDetail.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class PostDetail extends Model
9+
{
10+
/** @use HasFactory<\Database\Factories\PostDetailFactory> */
11+
use HasFactory;
12+
}

app/Models/Rating.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Rating extends Model
9+
{
10+
/** @use HasFactory<\Database\Factories\RatingFactory> */
11+
use HasFactory;
12+
}

app/Models/Tag.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Tag extends Model
8+
{
9+
//
10+
}
Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App;
3+
namespace App\Models;
44

55
use Illuminate\Notifications\Notifiable;
66
use Illuminate\Contracts\Auth\MustVerifyEmail;
@@ -19,21 +19,4 @@ class User extends Authenticatable
1919
'name', 'email', 'password',
2020
];
2121

22-
/**
23-
* The attributes that should be hidden for arrays.
24-
*
25-
* @var array
26-
*/
27-
protected $hidden = [
28-
'password', 'remember_token',
29-
];
30-
31-
/**
32-
* The attributes that should be cast to native types.
33-
*
34-
* @var array
35-
*/
36-
protected $casts = [
37-
'email_verified_at' => 'datetime',
38-
];
3922
}

app/Services/PostService.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace App\Services;
4+
use Illuminate\Support\Facades\DB;
5+
6+
class PostService
7+
{
8+
private $maxPage; // 最大ページ数
9+
private $nowPage; // 現在のページ番号
10+
11+
/**
12+
* ページャー情報をセットする
13+
*/
14+
public function setPagerInfo($pageId) {
15+
$this->nowPage = $pageId;
16+
}
17+
18+
/**
19+
* ページャーの情報を返す
20+
*/
21+
public function getPagerInfo() {
22+
return ['maxPage' => $this->maxPage, 'nowPage' => $this->nowPage];
23+
}
24+
25+
/**
26+
* 店舗情報を取得する
27+
* @return array<int, object>
28+
*/
29+
public function getShopDetails(): array
30+
{
31+
// 店舗情報を取得する
32+
$shopInfo = $this->getShopInfo();
33+
34+
// タグを全種類取得する
35+
$tags = $this->getAllTags();
36+
37+
// タグ名を追加する処理
38+
foreach ($shopInfo as $si) {
39+
$tagIds = explode(',', $si->tags);
40+
41+
// タグリストの初期化
42+
$tagList = [];
43+
44+
foreach ($tagIds as $tagId) {
45+
if (isset($tags[$tagId])) {
46+
$tagList[] = $tags[$tagId];
47+
}
48+
}
49+
50+
$si->tags = $tagList;
51+
}
52+
53+
return $shopInfo;
54+
}
55+
56+
/**
57+
* 店舗情報をDBから取得する
58+
* @return array<int, object>
59+
*/
60+
private function getShopInfo(): array
61+
{
62+
$sql = 'SELECT posts.id, posts.shopname, posts.tags, deleted_flg,'.PHP_EOL;
63+
$sql .= 'ROUND(AVG(rating)::numeric, 1) AS avg_rating, MAX(image_url) AS image_url'.PHP_EOL;
64+
$sql .= 'FROM posts'.PHP_EOL;
65+
$sql .= ' INNER JOIN ratings'.PHP_EOL;
66+
$sql .= ' ON posts.id = ratings.post_id'.PHP_EOL;
67+
$sql .= ' INNER JOIN images'.PHP_EOL;
68+
$sql .= ' ON posts.id = images.post_id'.PHP_EOL;
69+
$sql .= 'GROUP BY posts.id, posts.shopname, posts.tags, posts.deleted_flg'.PHP_EOL;
70+
$sql .= 'ORDER BY avg_rating DESC'.PHP_EOL;
71+
72+
return $this->pager(DB::select($sql));
73+
}
74+
75+
/**
76+
* タグを全て取得する
77+
* @return array<int, object>
78+
*/
79+
private function getAllTags(): array
80+
{
81+
$sql = 'SELECT id, name'.PHP_EOL;
82+
$sql .= 'FROM tags'.PHP_EOL;
83+
84+
return DB::select($sql);
85+
}
86+
87+
/**
88+
* ユーザーがお気に入り(いいね)した投稿の post_id を取得する
89+
* @param int $user_id
90+
* @return array ユーザーが良いねした投稿のidリスト
91+
*/
92+
public function getFavorites(int $user_id): array
93+
{
94+
$sql = 'SELECT Favorites.post_id'.PHP_EOL;
95+
$sql .= ' FROM Favorites'.PHP_EOL;
96+
$sql .= 'WHERE user_id = :user_id'.PHP_EOL;
97+
98+
return DB::select($sql, ['user_id' => $user_id]);
99+
}
100+
101+
/**
102+
* ページャー
103+
* @param array $shopData
104+
* @return array 表示用データ
105+
*/
106+
private function pager(array $shopData): array
107+
{
108+
$max = 10; // 1ページの最大表示件数
109+
110+
$shopNum = count($shopData); // 現在の合計店舗数
111+
112+
$this->maxPage = ceil($shopNum / $max); // 最大ページ数
113+
114+
if (!isset($this->nowPage)) {
115+
$this->nowPage = 1; // 特に指定されていなければ1ページ目を取得する
116+
}
117+
118+
$startNo = ($this->nowPage - 1) * $max; // 開始ページ番号を取得する
119+
120+
$dispData = array_slice($shopData, $startNo, $max, true);
121+
122+
return $dispData;
123+
}
124+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
7+
/**
8+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Favorite>
9+
*/
10+
class FavoriteFactory extends Factory
11+
{
12+
/**
13+
* Define the model's default state.
14+
*
15+
* @return array<string, mixed>
16+
*/
17+
public function definition(): array
18+
{
19+
return [
20+
//
21+
];
22+
}
23+
}

0 commit comments

Comments
 (0)