Skip to content

Commit 0aa8f0b

Browse files
committed
feature: Adicionado novos campos no objeto de usuario e novas forma de retorno
1 parent d546712 commit 0aa8f0b

File tree

5 files changed

+79
-35
lines changed

5 files changed

+79
-35
lines changed

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Controller/UserController.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Walderlan Sena <senawalderlan@gmail.com>
5+
* Date: 27/07/18
6+
* Time: 19:25
7+
*/
28

39
namespace App\Controller;
410

@@ -25,14 +31,16 @@ public function __construct(UserService $userService)
2531

2632
public function getAction()
2733
{
28-
// $doctrine = $this->container->get('doctrine_mongodb')
29-
// ->getManager()
30-
// ->getRepository(User::class)->findAllOrderedByName();
31-
$doctrine = '';
3234
try {
33-
$doctrine = $this->userService->findAllOrderByName();
35+
$doctrine = $this->userService->findAllOrderBy();
3436
} catch (\Exception $exception) {
35-
37+
return new JsonResponse([
38+
'status' => false,
39+
'data' => null,
40+
'errors' => [
41+
'Não foi possível concluir a requisição Erro: '. $exception->getMessage()
42+
],
43+
]);
3644
}
3745

3846
return new JsonResponse([
@@ -50,20 +58,14 @@ public function postAction(Request $request)
5058
{
5159
$request = json_decode($request->getContent());
5260

53-
$doctrine = $this->container->get('doctrine_mongodb')->getManager();
54-
55-
$user = new User();
56-
$user->setNome($request->nome);
57-
$user->setIdade($request->idade);
58-
59-
$doctrine->persist($user);
60-
$doctrine->flush();
61+
$user = $this->userService->insertUser($request);
6162

6263
return new JsonResponse([
6364
'status' => true,
6465
'message' => 'Novo usuario cadastrado com sucesso !',
6566
'data' => [
6667
'nome' => $user->getNome(),
68+
'email' => $user->getEmail(),
6769
'idade' => $user->getIdade()
6870
],
6971
'errors' => null

src/Document/User.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/**
1414
* Class User
1515
* @package App\Document
16-
* @MongoDB\Document(repositoryClass="\App\Repository\UserRepository")
16+
* @MongoDB\Document(repositoryClass="\App\Repository\UserRepository", collection="user")
1717
*/
1818
class User
1919
{
@@ -34,6 +34,12 @@ class User
3434
*/
3535
public $idade;
3636

37+
/**
38+
* @var string
39+
* @MongoDB\Field(type="string")
40+
*/
41+
public $email;
42+
3743
/**
3844
* @return mixed
3945
*/
@@ -70,6 +76,24 @@ public function setNome(string $nome): User
7076
return $this;
7177
}
7278

79+
/**
80+
* @return string
81+
*/
82+
public function getEmail(): string
83+
{
84+
return $this->email;
85+
}
86+
87+
/**
88+
* @param string $email
89+
* @return User
90+
*/
91+
public function setEmail(string $email): User
92+
{
93+
$this->email = $email;
94+
return $this;
95+
}
96+
7397
/**
7498
* @return int
7599
*/

src/Repository/UserRepository.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,37 @@
1010

1111
use App\Document\User;
1212
use Doctrine\ODM\MongoDB\DocumentRepository;
13-
use Doctrine\ODM\MongoDB\MongoDBException;
1413

1514
class UserRepository extends DocumentRepository
1615
{
1716
/**
18-
* @return mixed
19-
* @throws \Exception
17+
* @return array
2018
*/
21-
public function findAllOrderedByName()
19+
public function findAllOrderedBy()
2220
{
23-
$response = $this->createQueryBuilder()->exclude('nome')->getQuery()->execute();
24-
// $response = $this->createQueryBuilder()
25-
// try {
26-
// $response = $this->getDocumentManager()->getRepository(User::class)->findAll();
27-
// } catch (MongoDBException $exception) {
28-
// throw new \Exception($exception->getMessage());
29-
// }
30-
21+
$response = $this->getDocumentManager()->createQueryBuilder()
22+
->find(User::class)
23+
->getQuery()->toArray();
3124
return $response;
3225
}
3326

27+
/**
28+
* @param $request
29+
* @return User
30+
*/
31+
public function insert($request)
32+
{
33+
$doctrine = $this->getDocumentManager();
34+
35+
$user = new User();
36+
$user->setNome($request->nome);
37+
$user->setEmail($request->email);
38+
$user->setIdade($request->idade);
39+
40+
$doctrine->persist($user);
41+
$doctrine->flush();
42+
43+
return $user;
44+
}
45+
3446
}

src/Service/UserService.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,21 @@ public function __construct(DocumentManager $documentManager)
2828
* @return mixed
2929
* @throws \Exception
3030
*/
31-
public function findAllOrderByName()
31+
public function findAllOrderBy()
3232
{
3333
try {
34-
$response = $this->documentManager->getRepository(User::class)
35-
->findAllOrderedByName();
34+
$response = $this->documentManager->getRepository(User::class)->findAllOrderedBy();
3635
} catch (\Exception $exception) {
3736
throw new \Exception($exception->getMessage());
3837
}
3938

39+
sort($response);
40+
4041
return $response;
4142
}
43+
44+
public function insertUser($request)
45+
{
46+
return $this->documentManager->getRepository(User::class)->insert($request);
47+
}
4248
}

0 commit comments

Comments
 (0)