@@ -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}
0 commit comments