Commit 240816c4550cd378bb4873adec2a4f841f28e725
Exists in
master
Adiciona restriçao de unicidade na impressora e data
Showing
2 changed files
with
13 additions
and
19 deletions
Show diff stats
Controller/ApiController.php
... | ... | @@ -99,21 +99,7 @@ class ApiController extends Controller { |
99 | 99 | |
100 | 100 | } |
101 | 101 | |
102 | - | |
103 | - $counter = $this->getDoctrine()->getManager()->getRepository('CocarBundle:PrinterCounter')->findBy(array( | |
104 | - 'printer' => $printer->getId(), | |
105 | - 'date' => $dados['counter_time'] | |
106 | - )); | |
107 | - | |
108 | - if(empty($counter)) { | |
109 | - $counter = new PrinterCounter; | |
110 | - } else { | |
111 | - $this->get('logger')->error("Entrada repetida para impressora". $printer->getId() ." e data ".$dados['counter_time']); | |
112 | - $response = new JsonResponse(); | |
113 | - $response->setStatusCode('200'); | |
114 | - | |
115 | - return $response; | |
116 | - } | |
102 | + $counter = new PrinterCounter(); | |
117 | 103 | |
118 | 104 | // Atualiza impressora sempre que alterar o serial |
119 | 105 | if (!empty($dados['model'])) { |
... | ... | @@ -131,9 +117,16 @@ class ApiController extends Controller { |
131 | 117 | $counter->setPrints($dados['counter']); |
132 | 118 | $counter->setDate($dados['counter_time']); |
133 | 119 | |
134 | - $em->persist($printer); | |
135 | - $em->persist($counter); | |
136 | - $em->flush(); | |
120 | + try { | |
121 | + $em->persist($printer); | |
122 | + $em->flush(); | |
123 | + $em->persist($counter); | |
124 | + $em->flush(); | |
125 | + } catch (\Exception $e) { | |
126 | + // Ainda assim retorna como sucesso | |
127 | + $logger->error("Entrada repetida para impressora ".$printer->getHost() . "na data ".$dados['counter_time']); | |
128 | + } | |
129 | + | |
137 | 130 | |
138 | 131 | $response = new JsonResponse(); |
139 | 132 | $response->setStatusCode('200'); | ... | ... |
Entity/PrinterCounter.php
... | ... | @@ -3,11 +3,12 @@ |
3 | 3 | namespace Swpb\Bundle\CocarBundle\Entity; |
4 | 4 | |
5 | 5 | use Doctrine\ORM\Mapping as ORM; |
6 | +use Doctrine\ORM\Mapping\UniqueConstraint; | |
6 | 7 | |
7 | 8 | /** |
8 | 9 | * PrinterCounter |
9 | 10 | * |
10 | - * @ORM\Table("tb_printer_counter") | |
11 | + * @ORM\Table("tb_printer_counter",uniqueConstraints={@UniqueConstraint(name="printer_date_unique_idx", columns={"printer_id", "date"})}) | |
11 | 12 | * @ORM\Entity(repositoryClass="Swpb\Bundle\CocarBundle\Entity\PrinterCounterRepository") |
12 | 13 | */ |
13 | 14 | class PrinterCounter | ... | ... |