Пример #1
0
 public function create($data)
 {
     $currentUser = parent::authenticateUser();
     $spending = new Spending();
     if (isset($data->quantity) && isset($data->name) && isset($data->date)) {
         $spending->setDate($data->date);
         $spending->setQuantity($data->quantity);
         $spending->setName($data->name);
         $spending->setOwner($currentUser->getLogin());
         try {
             $idSpending = $this->spendingDAO->save($spending);
             foreach ($data->types as $type_loop) {
                 $type = $this->typeDAO->findById($type_loop->idType);
                 if ($type == NULL) {
                     $this->spendingDAO->delete($idSpending);
                     header($this->server->getServerProtocol() . ' 400 Bad request');
                     echo "Type with id " . $type_loop->idType . " not found";
                     return;
                 }
                 $typeSpending = new TypeSpending();
                 $typeSpending->setSpending($idSpending);
                 $typeSpending->setType($type);
                 $this->typeSpendingDAO->save($typeSpending);
             }
             header($this->server->getServerProtocol() . ' 201 Created');
             header('Location: ' . $this->server->getRequestUri() . "/" . $idSpending);
             header('Content-Type: application/json');
         } catch (ValidationException $e) {
             header($this->server->getServerProtocol() . ' 400 Bad request');
             echo json_encode($e->getErrors());
         }
     }
 }