Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function createCommand(Request $req, Response $resp, $args)
- {
- header("Access-Control-Allow-Origin: *");
- if ($req->getAttribute('has_errors')) {
- $errors = $req->getAttribute('errors');
- var_dump($errors);
- foreach ($errors as $key => $listerrorAttribute) {
- echo "<strong>" . $key . " : </strong><br/>";
- //echo "<br/>";
- foreach ($listerrorAttribute as $error) {
- echo $error;
- echo "<br/>";
- }
- }
- } else {
- $resp = $resp->withHeader('Content-Type', 'application/json');
- $req_body = $req->getBody()->getContents();
- if (Json::isJson($req_body)) {
- $body = json_decode($req_body, true);
- $resp = $resp->withStatus(500);
- try {
- $uuid = Uuid::uuid1();
- } catch (\Exception $e) {
- echo $e;
- }
- $commande = new Commande();
- $commande->id = $uuid->toString();
- $commande->nom = filter_var($body["nom"], FILTER_SANITIZE_STRING);
- $commande->mail = filter_var($body["mail"], FILTER_SANITIZE_STRING);
- $commande->token = bin2hex(openssl_random_pseudo_bytes(32));
- $commande->montant = 0;
- $commande->livraison = $body["livraison"]["date"] . " " . $body["livraison"]["heure"];
- if (isset($body["client_id"])) {
- $client = Client::find($body["client_id"]);
- if ($client) {
- $token = explode(" ", $req->getHeader("Authorization")[0])[1];
- $tokenDecoded = JWT::decode($token, "lul", array('HS512'));
- if ($client->id == $tokenDecoded->id)
- $commande->client_id = $body["client_id"];
- }
- }
- $total = 0;
- foreach ($body["items"] as $item) {
- $total += $commande->addItem($item);
- if (isset($body["client_id"])) {
- $client->cumul_achats += $total;
- $client->save();
- }
- }
- $commande->save();
- $resp->getBody()->write(Json::resource("commande", $commande->toArray()));
- $resp = $resp->withHeader("Location", "http://api.commande.local:19080/commands/" . $uuid->toString());
- $resp = $resp->withStatus(201);
- } else {
- $resp->getBody()->write(Json::error(500, "merci de transmettre du JSON valide"));
- }
- }
- return $resp->withHeader('Access-Control-Allow-Origin', 'http://api.commande.local');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement