Skip to content

Commit

Permalink
add squad members.
Browse files Browse the repository at this point in the history
  • Loading branch information
wouerner committed Feb 28, 2024
1 parent 5e0e2c4 commit 294d5e6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
46 changes: 41 additions & 5 deletions app/Controller/SquadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace App\Controller;

use App\Model\Squad;
use App\Model\Product;
use Ramsey\Uuid\Uuid;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
Expand All @@ -22,7 +23,6 @@ class SquadController extends AbstractController
{
public function index($productUuid = false): Psr7ResponseInterface
{
var_dump($productUuid);
if (empty($productUuid) === false) {
$squad = Squad::where('product_uuid', $productUuid)->get();
} else {
Expand Down Expand Up @@ -59,14 +59,33 @@ public function create(): Psr7ResponseInterface

public function update($uuid): Psr7ResponseInterface
{
$squad = Squad::find($uuid);
$user = $this->container->get('user');

$squad = Squad::where('uuid', $uuid)->first();

if (! $squad) {
return $this->response->json(['error' => 'Squad not found'], 404);
}

$product = Product::where('uuid', $squad->product_uuid)->first();

if (! $product) {
return $this->response->json(['error' => 'Product not found'], 404);
}

if ($user->uuid !== $product->owner_uuid) {
return $this->response->json(
[
'error' => 'Você não tem permissão para autalizar este produto.',
],
403
);
}

$data = $this->request->getParsedBody();
$squad->fill($data);

$squad->name = $data['name'];
$squad->description = $data['description'];
$squad->save();

return $this->response->json([
Expand All @@ -75,14 +94,31 @@ public function update($uuid): Psr7ResponseInterface
]);
}

public function del()
public function delete($uuid)
{
$squad = Squad::find($id);
$user = $this->container->get('user');

$squad = Squad::where('uuid', $uuid)->first();

if (! $squad) {
return $this->response->json(['error' => 'Squad not found'], 404);
}

$product = Product::where('uuid', $squad->product_uuid)->first();

if (! $product) {
return $this->response->json(['error' => 'Product not found'], 404);
}

if ($user->uuid !== $product->owner_uuid) {
return $this->response->json(
[
'error' => 'Você não tem permissão para autalizar este produto.',
],
403
);
}

$squad->delete();

return $this->response->json(['message' => 'Squad deleted successfully!']);
Expand Down
13 changes: 2 additions & 11 deletions config/routes.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

declare(strict_types=1);

use Hyperf\HttpServer\Router\Router;
Expand Down Expand Up @@ -36,9 +27,9 @@ function () {
Router::addRoute(['GET'], '/squad/{uuid}', 'App\Controller\SquadController@show');
Router::addRoute(['POST'], '/squad', 'App\Controller\SquadController@create');
Router::addRoute(['PUT'], '/squad/{uuid}', 'App\Controller\SquadController@update');
Router::addRoute(['DELETE'], '/squad', 'App\Controller\SquadController@delete');
Router::addRoute(['DELETE'], '/squad/{uuid}', 'App\Controller\SquadController@delete');

// Squad
// Member
Router::addRoute(['GET'], '/squad/{uuid}/members', 'App\Controller\Member@index');
Router::addRoute(['GET'], '/squad/{uuid}/member/{memberUuid}', 'App\Controller\Member@show');
Router::addRoute(['POST'], '/squad/{uuid}/member', 'App\Controller\Member@create');
Expand Down

0 comments on commit 294d5e6

Please sign in to comment.