Skip to content

Commit

Permalink
update project
Browse files Browse the repository at this point in the history
  • Loading branch information
Bosun18 committed Dec 18, 2023
1 parent 3518e12 commit 11e9c51
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,39 +94,36 @@
return $this->get('renderer')->render($response->withStatus(422), 'main.phtml', $params);
}

try {
$pdo = $this->get('pdo');
$url = mb_strtolower($urls['url']['name']);
$parsedUrl = parse_url($url);
$name = "{$parsedUrl['scheme']}://{$parsedUrl['host']}";

$createdAt = Carbon::now();

$query = "SELECT name FROM urls WHERE name = ?";
$statement = $pdo->prepare($query);
$statement->execute([$name]);
$existedUrl = $statement->fetchAll();
$pdo = $this->get('pdo');
$url = mb_strtolower($urls['url']['name']);
$parsedUrl = parse_url($url);
$name = "{$parsedUrl['scheme']}://{$parsedUrl['host']}";

if ($existedUrl) {
$query = "SELECT id FROM urls WHERE name = ?";
$statement = $pdo->prepare($query);
$statement->execute([$name]);
$existedUrlId = $statement->fetchColumn();
$createdAt = Carbon::now();

$this->get('flash')->addMessage('success', 'Страница уже существует');
return $response->withRedirect($this->get('router')->urlFor('show', ['id' => $existedUrlId]));
}
$query = "SELECT name FROM urls WHERE name = ?";
$statement = $pdo->prepare($query);
$statement->execute([$name]);
$existedUrl = $statement->fetchAll();

$query = "INSERT INTO urls (name, created_at) VALUES (?, ?)";
if ($existedUrl) {
$query = "SELECT id FROM urls WHERE name = ?";
$statement = $pdo->prepare($query);
$statement->execute([$name, $createdAt]);
$lastId = $pdo->lastInsertId();
$statement->execute([$name]);
$existedUrlId = $statement->fetchColumn();

$this->get('flash')->addMessage('success', 'Страница успешно добавлена');
return $response->withRedirect($this->get('router')->urlFor('show', ['id' => $lastId]));
} catch (PDOException $e) {
echo $e->getMessage();
$this->get('flash')->addMessage('success', 'Страница уже существует');
return $response->withRedirect($this->get('router')->urlFor('show', ['id' => $existedUrlId]));
}

$query = "INSERT INTO urls (name, created_at) VALUES (?, ?)";
$statement = $pdo->prepare($query);
$statement->execute([$name, $createdAt]);
$lastId = $pdo->lastInsertId();

$this->get('flash')->addMessage('success', 'Страница успешно добавлена');
return $response->withRedirect($this->get('router')->urlFor('show', ['id' => $lastId]));
});

$app->get('/urls/{id:[0-9]+}', function ($request, $response, $args) {
Expand Down

0 comments on commit 11e9c51

Please sign in to comment.