Skip to content

Commit 96b3b4c

Browse files
authored
Merge pull request #7 from ren807/issue-2
Issue 2
2 parents 88b6c11 + 01b6999 commit 96b3b4c

File tree

20 files changed

+17061
-4379
lines changed

20 files changed

+17061
-4379
lines changed

app/Http/Controllers/PostController.php

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,63 @@ class PostController extends Controller
99
{
1010
private $post;
1111

12-
public function __construct(PostService $post_service, Request $request)
12+
public function __construct(PostService $post_service)
1313
{
1414
$this->post = $post_service;
15-
$this->post->setPagerInfo($request->input('page_id'));
1615
}
1716

18-
public function index()
17+
public function index(Request $request)
1918
{
2019
$userId = 1;
21-
$posts = [];
22-
23-
$shopDetails = $this->post->getShopDetails();
20+
$this->post->setPagerInfo($request->input('page_id'));
21+
22+
$shopData = $this->post->convShopData();
2423
$favorites = $this->post->getFavorites($userId);
2524
$pagerInfo = $this->post->getPagerInfo();
2625

2726
$posts = [
28-
'shopDetails' => $shopDetails,
27+
'shopData' => $shopData,
2928
'favorites' => $favorites,
3029
'pagerInfo' => $pagerInfo,
3130
];
3231

3332
return View('posts.index', ['posts' => $posts]);
3433
}
34+
35+
public function show(int $id)
36+
{
37+
$shopDetail = $this->post->convShopDetailData($id);
38+
$images = $this->post->getShopImages($id);
39+
40+
$post = [
41+
'shopDetail' => $shopDetail,
42+
'images' => $images,
43+
];
44+
45+
return View('posts.show', ['post' => $post]);
46+
}
47+
48+
public function eval()
49+
{
50+
$userId = 4;
51+
$postId = request()->post('postId');
52+
$rating = request()->post('rating');
53+
54+
// 以下、ログインユーザがすでに評価しているか確認する
55+
$shopRating = $this->post->getRating($userId, $postId);
56+
57+
if (empty($shopRating)) {
58+
$this->post->storeReview($userId, $postId, $rating); // 未評価の場合、新たにDBに追加する
59+
} else {
60+
$this->post->updateReview($userId, $postId, $rating); // 評価済みの場合、DBを更新する
61+
}
62+
63+
$data = [
64+
'postId' => $postId,
65+
'rating' => $rating,
66+
'shopRating' => $shopRating,
67+
];
68+
69+
return response()->json($data);
70+
}
3571
}

app/Services/PostService.php

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public function getPagerInfo() {
2323
}
2424

2525
/**
26-
* 店舗情報を取得する
26+
* 店舗一覧情報をView用に変換する
2727
* @return array<int, object>
2828
*/
29-
public function getShopDetails(): array
29+
public function convShopData(): array
3030
{
3131
// 店舗情報を取得する
3232
$shopInfo = $this->getShopInfo();
@@ -53,11 +53,36 @@ public function getShopDetails(): array
5353
return $shopInfo;
5454
}
5555

56+
public function convShopDetailData($shopId)
57+
{
58+
// 店舗詳細情報を取得する
59+
$shopDetail = $this->getShopDetail($shopId);
60+
61+
// タグを全種類取得する
62+
$tags = $this->getAllTags();
63+
64+
// 以下、タグ名を追加する処理
65+
$tagIds = explode(',', $shopDetail['tags']);
66+
67+
// タグリストの初期化
68+
$tagList = [];
69+
70+
foreach ($tagIds as $tagId) {
71+
if (isset($tags[$tagId])) {
72+
$tagList = $tags[$tagId];
73+
}
74+
}
75+
76+
$shopDetail['tags'] = $tagList;
77+
78+
return $shopDetail;
79+
}
80+
5681
/**
5782
* 店舗情報をDBから取得する
5883
* @return array<int, object>
5984
*/
60-
private function getShopInfo(): array
85+
public function getShopInfo(): array
6186
{
6287
$sql = 'SELECT posts.id, posts.shopname, posts.tags, deleted_flg,'.PHP_EOL;
6388
$sql .= 'ROUND(AVG(rating)::numeric, 1) AS avg_rating, MAX(image_url) AS image_url'.PHP_EOL;
@@ -76,7 +101,7 @@ private function getShopInfo(): array
76101
* タグを全て取得する
77102
* @return array<int, object>
78103
*/
79-
private function getAllTags(): array
104+
public function getAllTags(): array
80105
{
81106
$sql = 'SELECT id, name'.PHP_EOL;
82107
$sql .= 'FROM tags'.PHP_EOL;
@@ -99,7 +124,7 @@ public function getFavorites(int $user_id): array
99124
}
100125

101126
/**
102-
* ページャー
127+
* 投稿一覧ページのページャー
103128
* @param array $shopData
104129
* @return array 表示用データ
105130
*/
@@ -121,4 +146,82 @@ private function pager(array $shopData): array
121146

122147
return $dispData;
123148
}
149+
150+
/**
151+
* 店舗詳細を取得
152+
* @param $shopId 店舗id
153+
* @return array 店舗情報
154+
*/
155+
public function getShopDetail(int $shopId)
156+
{
157+
$sql = 'SELECT posts.id, posts.shopname, posts.tags,'.PHP_EOL;
158+
$sql .= 'ROUND(AVG(rating)::numeric, 1) AS avg_rating, post_details.address'.PHP_EOL;
159+
$sql .= 'FROM posts'.PHP_EOL;
160+
$sql .= ' INNER JOIN ratings'.PHP_EOL;
161+
$sql .= ' ON posts.id = ratings.post_id'.PHP_EOL;
162+
$sql .= ' INNER JOIN post_details'.PHP_EOL;
163+
$sql .= ' ON posts.id = post_details.post_id'.PHP_EOL;
164+
$sql .= 'WHERE posts.id = :shopId'.PHP_EOL;
165+
$sql .= 'GROUP BY posts.id, posts.shopname, posts.tags, posts.deleted_flg, post_details.address'.PHP_EOL;
166+
$sql .= 'ORDER BY avg_rating DESC'.PHP_EOL;
167+
168+
$result = DB::select($sql, ['shopId' => $shopId]);
169+
170+
return !empty($result) ? (array) current($result) : [];
171+
}
172+
173+
public function getShopImages($shopId)
174+
{
175+
$sql = 'SELECT images.image_url'.PHP_EOL;
176+
$sql .= 'FROM posts'.PHP_EOL;
177+
$sql .= ' INNER JOIN images'.PHP_EOL;
178+
$sql .= ' ON posts.id = images.post_id'.PHP_EOL;
179+
$sql .= ' WHERE posts.id = :shopId'.PHP_EOL;
180+
181+
return DB::select($sql, ['shopId' => $shopId]);
182+
}
183+
184+
public function getRating(int $userId, int $postId)
185+
{
186+
$sql = 'SELECT * FROM ratings'.PHP_EOL;
187+
$sql .= 'WHERE user_id = :userId AND post_id = :postId'.PHP_EOL;
188+
189+
$params = [
190+
'userId' => $userId,
191+
'postId' => $postId,
192+
];
193+
194+
$result = DB::select($sql, $params);
195+
196+
return !empty($result) ? (array) current($result) : [];
197+
}
198+
199+
public function storeReview(int $userId, int $postId, int $rating)
200+
{
201+
$sql = 'INSERT INTO ratings'.PHP_EOL;
202+
$sql .= '(user_id, post_id, rating, created_at, updated_at)'.PHP_EOL;
203+
$sql .= 'VALUES (:userId, :postId, :rating, NOW(), NOW())'.PHP_EOL;
204+
205+
$params = [
206+
'userId' => $userId,
207+
'postId' => $postId,
208+
'rating' => $rating
209+
];
210+
211+
DB::insert($sql, $params);
212+
}
213+
214+
public function updateReview(int $userId, int $postId, int $rating)
215+
{
216+
$sql = 'UPDATE ratings SET rating = :rating'.PHP_EOL;
217+
$sql .= 'WHERE user_id = :userId AND post_id = :postId'.PHP_EOL;
218+
219+
$params = [
220+
'userId' => $userId,
221+
'postId' => $postId,
222+
'rating' => $rating
223+
];
224+
225+
DB::update($sql, $params);
226+
}
124227
}

database/factories/PostDetailFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ class PostDetailFactory extends Factory
1717
public function definition(): array
1818
{
1919
return [
20-
//
20+
'address' => $this->faker->address,
21+
'post_id' => null,
22+
'created_at' => now(),
23+
'updated_at' => now(),
2124
];
2225
}
2326
}

database/migrations/2025_03_10_055628_create_post_details_table.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ public function up(): void
1313
{
1414
Schema::create('post_details', function (Blueprint $table) {
1515
$table->id();
16+
$table->string('address', 255);
17+
$table->unsignedBigInteger('post_id');
1618
$table->timestamps();
19+
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
1720
});
1821
}
1922

database/seeds/DatabaseSeeder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22

3-
use App\Models\Rating;
3+
use App\Models\PostDetail;
44
use Database\Seeders\ImageSeeder;
5+
use Database\Seeders\PostDetailSeeder;
56
use Illuminate\Database\Seeder;
67
use Database\Seeders\PostSeeder;
78
use Database\Seeders\RatingSeeder;
@@ -22,5 +23,6 @@ public function run()
2223
$this->call(TagSeeder::class);
2324
$this->call(UserSeeder::class);
2425
$this->call(RatingSeeder::class);
26+
$this->call(PostDetailSeeder::class);
2527
}
2628
}

database/seeds/PostDetailSeeder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Database\Seeders;
44

5+
use App\Models\PostDetail;
56
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
67
use Illuminate\Database\Seeder;
78

@@ -12,6 +13,9 @@ class PostDetailSeeder extends Seeder
1213
*/
1314
public function run(): void
1415
{
15-
//
16+
PostDetail::factory()
17+
->count(11)
18+
->sequence(fn ($sequence) => ['post_id' => $sequence->index + 1])
19+
->create();
1620
}
1721
}

0 commit comments

Comments
 (0)