Skip to content

Commit 3c7f0fa

Browse files
committed
Change /connect/reviews/add to use JSON instead of formdata
1 parent 30cf704 commit 3c7f0fa

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

src/Controller/API/Connect/APIConnectReviewsController.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,20 @@ public function addReview(Request $request, $songID)
7373
{
7474
$em = $this->getDoctrine()->getManager();
7575
$data = [];
76-
7776
$connectToken = $request->query->get('connectToken');
7877

79-
$reviewRecommend = $request->request->get('recommend');
80-
$reviewComment = $request->request->get('comment');
78+
// Decode JSON request body
79+
$content = json_decode($request->getContent(), true);
80+
$reviewRecommend = isset($content['recommended']) ? $content['recommended'] : null;
81+
$reviewComment = isset($content['comment']) ? $content['comment'] : '';
8182

8283
// 422 - Parameter Missing
83-
if($connectToken == "" || $reviewRecommend == "" || $songID == "") {
84+
if($connectToken == "" || $reviewRecommend === null || $songID == "") {
8485
$response = new JsonResponse(['version' => $this->getParameter('api_version'), 'status' => 422, 'data' => []]);
8586
return $response;
8687
}
8788

8889
$connection = $em->getRepository(Connection::class)->findOneBy(array('connectToken' => $connectToken));
89-
9090
if($connection) {
9191
// Find Song
9292
$songToReview = $em->getRepository(Song::class)->findOneBy(array('id' => $songID));
@@ -99,27 +99,24 @@ public function addReview(Request $request, $songID)
9999
}
100100

101101
$previousReview = $em->getRepository(SongReview::class)->findOneBy(array('song' => $songToReview, 'user' => $connection->getUser()));
102-
102+
103103
if($previousReview) {
104104
// Update Existing Review
105-
$previousReview->setRecommended($reviewRecommend == "true" | $reviewRecommend == "1" ? true : false);
106-
if($reviewComment != "") { $previousReview->setComment($reviewComment); }
105+
$previousReview->setRecommended($reviewRecommend);
106+
if($reviewComment !== "") { $previousReview->setComment($reviewComment); }
107107
$previousReview->setReviewDate(new \DateTime('NOW'));
108-
109108
$em->persist($previousReview);
110109
$em->flush();
111-
112110
$response = new JsonResponse(['version' => $this->getParameter('api_version'), 'status' => 200, 'data' => []]);
113111
return $response;
114112
} else {
115113
// Create new Review
116114
$newReview = new SongReview();
117115
$newReview->setUser($connection->getUser());
118116
$newReview->setSong($songToReview);
119-
$newReview->setRecommended($reviewRecommend == "true" || $reviewRecommend == "1" ? true : false);
117+
$newReview->setRecommended($reviewRecommend);
120118
$newReview->setComment($reviewComment);
121119
$newReview->setReviewDate(new \DateTime('NOW'));
122-
123120
$em->persist($newReview);
124121
$em->flush();
125122

templates/apidocs/connect/reviews.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@
135135
<td>(string) connectToken</td>
136136
</tr>
137137
<tr>
138-
<th>POST Form-Data</th>
139-
<td>(int) recommend<br />
138+
<th>JSON Body</th>
139+
<td>(bool) recommend<br />
140140
(string) comment</td>
141141
</tr>
142142
</table>

0 commit comments

Comments
 (0)