Skip to content

Commit

Permalink
added search functions, but search results pagination not working
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jun 11, 2023
1 parent de2db5f commit 658b4e2
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 15 deletions.
17 changes: 15 additions & 2 deletions src/Controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use MyBlog\Repositories\PostRepository;
use MyBlog\ViewModels\IndexView;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class IndexController extends BaseController
{
Expand Down Expand Up @@ -66,8 +67,20 @@ public function debug(Request $request): string


#[Route('/search', ['GET'], 'main.search')]
public function search(Request $request)
public function search(Request $request): string
{
throw new Exception($request->query->get('q', 'none'));
$posts_per_page = 5;
$current_page = intval($request->query->get('page', 1));

// Todo: refactor negative and zero values for page var
$offset = $current_page === 1 ? 0 : $posts_per_page * ($current_page - 1);

$search_term = trim($request->query->get('q', ''));
$found_posts = $this->postRepository->search($search_term, $offset, $posts_per_page);
$total_items = count($found_posts);

$paginator = new Paginator($total_items, $posts_per_page, $current_page, '&page=(:num)');

return $this->view->search($found_posts, $paginator);
}
}
2 changes: 1 addition & 1 deletion src/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function index(Request $request): string|Response
}

} else {
$vm = new PostViewModel(message: 'Cant create post', errors: $errors);
$vm = new PostViewModel(message: 'Can\'t create post', errors: $errors);
return $this->render('post/form.html.twig', ['post' => $vm->toArray()]);
}

Expand Down
44 changes: 34 additions & 10 deletions src/Repositories/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use MyBlog\Core\Db\DatabaseInterface;
use MyBlog\Core\Utils;
use MyBlog\Dtos\NewCommentRequestDto;
use MyBlog\Dtos\PostRequestDto;
use MyBlog\Models\Comment;
use MyBlog\Models\Post;
use function MyBlog\Helpers\debug;

Expand All @@ -29,16 +27,9 @@ public function update(int $id, PostRequestDto $post)
return $this->db->update($id, $post->toArray(), 'posts');
}


public function getPosts(int $limit, int $offset = 0): array|bool
{
$convertor = static function (array $post) {
$post['title'] = sprintf('#%d %s', $post['id'], $post['title']);
$post['created_at'] = Utils::formatDatetime($post['created_at']);
$post['content'] = substr($post['content'], 0, 100);
$post['vc'] = $post['vc'] ?: 0;
return $post;
};
$convertor = $this->postConvertor(...);

$sql = 'SELECT posts.*, sc.views_count as vc
FROM posts
Expand All @@ -51,6 +42,25 @@ public function getPosts(int $limit, int $offset = 0): array|bool
}


public function search(string $search_term, int $offset = 0, int $limit = 10): array
{
$sql = 'SELECT posts.*, sc.views_count as vc
FROM posts
LEFT JOIN stat_counter sc on posts.id = sc.post_id
WHERE title LIKE :search_term OR content LIKE :search_term
ORDER BY created_at DESC LIMIT :offset, :limit';

$rows = $this->db->queryMany($sql, [
':search_term' => "%$search_term%",
':offset' => $offset,
':limit' => $limit
], $this->postConvertor(...));

if(!$rows) return [];

return $rows;
}

public function getUserPosts(int $user_id): array
{
$sql = "SELECT * FROM posts WHERE user_id = :user_id";
Expand Down Expand Up @@ -78,4 +88,18 @@ public function getUsersPosts()

return $this->db->query($sql, []);
}



private function postConvertor(array $post): array
{
$_post = [];

$_post['id'] = $post['id'];
$_post['title'] = sprintf('#%d %s', $post['id'], $post['title']);
$_post['created_at'] = Utils::formatDatetime($post['created_at']);
$_post['content'] = substr($post['content'], 0, 100);
$_post['vc'] = $post['vc'] ?? 0;
return $_post;
}
}
5 changes: 5 additions & 0 deletions src/ViewModels/IndexView.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public function index(array $posts, Paginator $paginator): string
return $this->environment->render('index/index.html.twig', ['posts' => $posts, 'paginator' => $paginator]);
}

public function search(array $posts, Paginator $paginator): string
{
return $this->environment->render('index/search.html.twig', ['posts' => $posts, 'paginator' => $paginator]);
}


public function phpinfo(string $info): string
{
Expand Down
48 changes: 48 additions & 0 deletions templates/index/search.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{% extends "layout.html.twig" %}

{% from 'macroses/_pagination.html.twig' import pagination %}

{% block title %}
Найденные статьи {{ posts.length }}
{% endblock %}

{% block style %}
<style>
.card\:hover:hover {
background: var(--bs-primary);
color: white;
}
</style>
{% endblock %}


{% block content %}
<h2 class="mt-2 mb-4">Найденные статьи</h2>

<div class="row row-cols-1 g-4">
{% if posts|length > 0 %}
{% for post in posts %}
<div class="col">
<a href="/post/{{ post.id }}">
<div class="card h-100 card:hover" title="Click to open post">
<!--<img src="..." class="card-img-top" alt="...">-->
<div class="card-body">
<h5 class="card-title">{{ post.title }}</h5>
<p class="card-text">{{ post.content }}</p>
</div>
<div class="card-footer d-flex justify-content-between">
<small class="text-muted">{{ post.created_at }}</small>
<small class="text-muted">{{ post.vc }} views</small>
</div>
</div>
</a>
</div>
{% endfor %}
{% else %}
<h4>No results</h4>
{% endif %}
</div>

{% include 'parts/_paginator.html.twig' %}
{% endblock %}

4 changes: 2 additions & 2 deletions templates/parts/_header.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<li><a href="/about" class="nav-link px-2 text-white">О!</a></li>
</ul>

<form class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3">
<input type="search" class="form-control form-control-dark" placeholder="Поиск..." aria-label="Поиск">
<form class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3" action="{{ route('main.search') }}" method="get">
<input type="search" name="q" class="form-control form-control-dark" placeholder="Поиск..." aria-label="Поиск">
</form>

<div class="text-end">
Expand Down

0 comments on commit 658b4e2

Please sign in to comment.