Skip to content

Commit 315871f

Browse files
romanetarsmarcet
authored andcommitted
Feature/marketplace reviews endpoint (#485)
* feat: review endpoint (WIP) Signed-off-by: romanetar <roman_ag@hotmail.com> * feat: add marketplace review endpoint Signed-off-by: romanetar <roman_ag@hotmail.com> * fix: review fixes Signed-off-by: romanetar <roman_ag@hotmail.com> --------- Signed-off-by: romanetar <roman_ag@hotmail.com>
1 parent 11e3d2b commit 315871f

19 files changed

Lines changed: 641 additions & 9 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php namespace App\Http\Controllers;
2+
/**
3+
* Copyright 2026 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
15+
use App\Http\ValidationRulesFactories\AbstractValidationRulesFactory;
16+
17+
/**
18+
* Class ReviewValidationRulesFactory
19+
* @package App\Http\Controllers
20+
*/
21+
final class ReviewValidationRulesFactory extends AbstractValidationRulesFactory
22+
{
23+
public static function buildForAdd(array $payload = []): array
24+
{
25+
return [
26+
'title' => 'required|string|max:50',
27+
'comment' => 'required|string|max:255',
28+
'rating' => 'required|numeric|in:0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0',
29+
];
30+
}
31+
32+
public static function buildForUpdate(array $payload = []): array
33+
{
34+
return ReviewValidationRulesFactory::buildForAdd($payload);
35+
}
36+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php namespace App\Http\Controllers;
2+
3+
/**
4+
* Copyright 2026 OpenStack Foundation
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
**/
15+
16+
use App\Models\Foundation\Marketplace\IReviewRepository;
17+
use App\ModelSerializers\SerializerUtils;
18+
use App\Services\Model\Marketplace\IReviewService;
19+
use Illuminate\Support\Facades\Request;
20+
use models\oauth2\IResourceServerContext;
21+
use ModelSerializers\SerializerRegistry;
22+
23+
/**
24+
* Class OAuth2ReviewsApiController
25+
* @package App\Http\Controllers
26+
*/
27+
final class OAuth2ReviewsApiController extends AbstractCompanyServiceApiController
28+
{
29+
/**
30+
* @var IReviewService
31+
*/
32+
private $service;
33+
34+
/**
35+
* ReviewsApiController constructor.
36+
* @param IReviewRepository $repository
37+
* @param IReviewService $review_service
38+
* @param IResourceServerContext $resource_server_context
39+
*/
40+
public function __construct
41+
(
42+
IReviewRepository $repository,
43+
IReviewService $review_service,
44+
IResourceServerContext $resource_server_context
45+
)
46+
{
47+
parent::__construct($repository, $resource_server_context);
48+
49+
$this->service = $review_service;
50+
}
51+
52+
use RequestProcessor;
53+
54+
use GetAndValidateJsonPayload;
55+
56+
/**
57+
* @param $company_service_id
58+
* @return mixed
59+
*/
60+
public function addReview($company_service_id)
61+
{
62+
return $this->processRequest(function () use ($company_service_id) {
63+
$payload = $this->getJsonPayload(ReviewValidationRulesFactory::buildForAdd(Request::all()));
64+
65+
$review = $this->service->addReview(intval($company_service_id), $payload);
66+
67+
return $this->created(SerializerRegistry::getInstance()->getSerializer($review)->serialize(
68+
SerializerUtils::getExpand(),
69+
SerializerUtils::getFields(),
70+
SerializerUtils::getRelations()
71+
));
72+
});
73+
}
74+
}

app/ModelSerializers/Marketplace/MarketPlaceReviewSerializer.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14+
use Libs\ModelSerializers\One2ManyExpandSerializer;
1415
use ModelSerializers\SilverStripeSerializer;
1516
/**
1617
* Class MarketPlaceReviewSerializer
@@ -24,6 +25,20 @@ final class MarketPlaceReviewSerializer extends SilverStripeSerializer
2425
protected static $array_mappings = [
2526
'Title' => 'title:json_string',
2627
'Comment' => 'comment:json_string',
27-
'Rating' => 'rating:json_int',
28+
'Rating' => 'rating:json_float',
29+
'AuthorId' => 'author_id:json_int',
30+
];
31+
32+
protected static $allowed_relations = [
33+
'author',
34+
];
35+
36+
protected static $expand_mappings = [
37+
'author' => [
38+
'type' => One2ManyExpandSerializer::class,
39+
'original_attribute' => 'author_id',
40+
'getter' => 'getAuthor',
41+
'has' => 'hasAuthor'
42+
],
2843
];
2944
}

app/Models/Foundation/Marketplace/CompanyService.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ public function getName()
152152
return $this->name;
153153
}
154154

155+
/**
156+
* @param string $name
157+
* @return void
158+
*/
159+
public function setName(string $name): void
160+
{
161+
$this->name = $name;
162+
}
163+
155164
/**
156165
* @return string
157166
*/
@@ -160,6 +169,15 @@ public function getSlug()
160169
return $this->slug;
161170
}
162171

172+
/**
173+
* @param string $slug
174+
* @return void
175+
*/
176+
public function setSlug(string $slug): void
177+
{
178+
$this->slug = $slug;
179+
}
180+
163181
/**
164182
* @return string
165183
*/
@@ -176,6 +194,15 @@ public function isActive()
176194
return $this->is_active;
177195
}
178196

197+
/**
198+
* @param bool $is_active
199+
* @return void
200+
*/
201+
public function setIsActive(bool $is_active): void
202+
{
203+
$this->is_active = $is_active;
204+
}
205+
179206
/**
180207
* @return Company
181208
*/
@@ -217,6 +244,15 @@ public function getReviews()
217244
return $this->reviews->toArray();
218245
}
219246

247+
/**
248+
* @param MarketPlaceReview $review
249+
* @return void
250+
*/
251+
public function addReview(MarketPlaceReview $review)
252+
{
253+
$this->reviews->add($review);
254+
}
255+
220256
/**
221257
* @return MarketPlaceVideo[]
222258
*/
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php namespace App\Models\Foundation\Marketplace\Factories;
2+
3+
/**
4+
* Copyright 2026 OpenStack Foundation
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
**/
15+
16+
use App\Models\Foundation\Marketplace\MarketPlaceReview;
17+
use Illuminate\Support\Facades\App;
18+
use models\oauth2\IResourceServerContext;
19+
20+
/**
21+
* Class ReviewFactory
22+
* @package App\Models\Foundation\Marketplace\Factories
23+
*/
24+
final class ReviewFactory
25+
{
26+
/**
27+
* @param array $data
28+
* @return MarketPlaceReview
29+
*/
30+
public static function build(array $data):MarketPlaceReview {
31+
return self::populate(new MarketPlaceReview, $data);
32+
}
33+
34+
/**
35+
* @param MarketPlaceReview $review
36+
* @param array $data
37+
* @return MarketPlaceReview
38+
*/
39+
public static function populate(MarketPlaceReview $review, array $data):MarketPlaceReview {
40+
41+
if(isset($data['title']))
42+
$review->setTitle(trim($data['title']));
43+
44+
if(isset($data['comment']))
45+
$review->setComment(trim($data['comment']));
46+
47+
if(isset($data['rating']))
48+
$review->setRating(floatval($data['rating']));
49+
$resource_server_ctx = App::make(IResourceServerContext::class);
50+
51+
$review->setAuthor($resource_server_ctx->getCurrentUser(false, false));
52+
53+
return $review;
54+
}
55+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php namespace App\Models\Foundation\Marketplace;
2+
/*
3+
* Copyright 2026 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
15+
use models\utils\IBaseRepository;
16+
17+
/**
18+
* Interface ICompanyServiceRepository
19+
* @package App\Models\Foundation\Marketplace
20+
*/
21+
interface IReviewRepository extends IBaseRepository
22+
{
23+
24+
}

0 commit comments

Comments
 (0)