Skip to content

Commit

Permalink
Product price validation added
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoumpierre committed Mar 27, 2018
1 parent 940a9e0 commit d84b3f6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function create(Request $request): JsonResponse
{
$this->validate($request, [
'name' => 'required',
'price' => 'required|numeric'
'price' => 'required|regex:/^[0-9](\.?[0-9]+)*(\,[0-9]+)$/'
]);

return response()->json($this->productRepository->create($request->all()), Response::HTTP_CREATED);
Expand All @@ -69,7 +69,7 @@ public function update(Request $request, int $id): JsonResponse
{
$this->validate($request, [
'name' => 'required',
'price' => 'required|numeric'
'price' => 'required|regex:/^[0-9](\.?[0-9]+)*(\,[0-9]+)$/'
]);

return response()->json($this->productRepository->update($request->all(), $id));
Expand Down
16 changes: 16 additions & 0 deletions app/Repositories/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function findOneById(int $id, array $columns = ['*']): Model
*/
public function create(array $params)
{
$params['price'] = $this->fixCurrencyFormat($params['price']);

Product::query()->create($params);

return null;
Expand All @@ -56,6 +58,8 @@ public function create(array $params)
*/
public function update(array $params, int $id)
{
$params['price'] = $this->fixCurrencyFormat($params['price']);

Product::query()->findOrFail($id)->update($params);

return null;
Expand All @@ -71,4 +75,16 @@ public function delete(int $id)

return null;
}

/**
* @param float $value
* @return mixed
*/
private function fixCurrencyFormat($value)
{
$source = array('.', ',');
$replace = array('', '.');

return str_replace($source, $replace, $value);
}
}
22 changes: 18 additions & 4 deletions tests/Endpoints/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ public function testCreatingProduct()
]);
$this->assertResponseStatus(422);

// Invalid request - wrong price regex
$this->post(ProductTest::URL, [
'name' => 'Teste',
'price' => '123'
]);
$this->assertResponseStatus(422);

// Invalid request - wrong field type
$this->post(ProductTest::URL, [
'name' => 'Teste',
Expand All @@ -112,7 +119,7 @@ public function testCreatingProduct()
// Valid request
$this->post(ProductTest::URL, [
'name' => 'Teste',
'price' => '12'
'price' => '12,00'
]);
$this->assertResponseStatus(201);
}
Expand All @@ -133,7 +140,7 @@ public function testUpdatingProduct()
// Valid request
$this->put(ProductTest::URL . '1', [
'name' => 'Produto 1',
'price' => '123'
'price' => '123,00'
]);
$this->assertResponseOk();

Expand All @@ -143,17 +150,24 @@ public function testUpdatingProduct()
]);
$this->assertResponseStatus(422);

// Invalid request - wrong price regex
$this->put(ProductTest::URL . '1', [
'name' => 'Produto 2',
'price' => '123'
]);
$this->assertResponseStatus(422);

// Invalid request - no product id
$this->put(ProductTest::URL, [
'name' => 'Produto 3',
'price' => '123'
'price' => '123,00'
]);
$this->assertResponseStatus(405);

// Invalid id
$this->put(ProductTest::URL . '234324', [
'name' => '123456',
'price' => '1234'
'price' => '1234,00'
]);
$this->assertResponseStatus(404);
}
Expand Down

0 comments on commit d84b3f6

Please sign in to comment.