added more info to admin panel

pull/1/head
kirbylife 2019-12-18 18:32:45 -06:00
parent 27cac09a08
commit 64c2317bd7
4 changed files with 73 additions and 15 deletions

View File

@ -56,8 +56,12 @@ fn panel(mut cookies: Cookies<'_>) -> Result<Template, Redirect> {
match cookies.get_private("user") {
Some(cookie) => {
if cookie.value() == password {
let context = get_context();
let mut context = get_context();
let (all_posts, n_posts) = posts::get_posts(None);
context.insert("posts", &all_posts);
context.insert("n_posts", &n_posts);
Ok(Template::render("admin/panel", context))
} else {
Err(Redirect::to("/admin"))

View File

@ -17,19 +17,23 @@ pub mod posts {
use crate::controllers::*;
use crate::models::{NewPost, Post};
pub const MAX_POSTS_PER_PAGE: u64 = 20;
pub const MAX_POSTS_PER_PAGE: u64 = 10;
pub fn get_posts(page: u64) -> (Vec<Post>, i64) {
pub fn get_posts(page: Option<u64>) -> (Vec<Post>, i64) {
use crate::schema::posts::dsl::*;
let connection = establish_connection();
let visible_posts = posts
.filter(published.eq(true))
.order(created_at.desc())
.limit(MAX_POSTS_PER_PAGE as i64)
.offset((MAX_POSTS_PER_PAGE * (page - 1)) as i64)
.load::<Post>(&connection)
.expect("Error loading posts");
let visible_posts = posts.filter(published.eq(true)).order(created_at.desc());
let visible_posts = match page {
Some(number_page) => visible_posts
.limit(MAX_POSTS_PER_PAGE as i64)
.offset((MAX_POSTS_PER_PAGE * (number_page - 1)) as i64)
.load::<Post>(&connection)
.expect("Error loading posts"),
None => visible_posts
.load::<Post>(&connection)
.expect("Error loading posts"),
};
let number_of_posts: i64 = posts
.filter(published.eq(true))

View File

@ -32,7 +32,7 @@ fn index(page: Option<u64>) -> Template {
let mut context = get_context();
let (posts, n_posts) = posts::get_posts(page);
let (posts, n_posts) = posts::get_posts(Some(page));
let total_pages = (n_posts as f64 / posts::MAX_POSTS_PER_PAGE as f64).ceil() as i64;

View File

@ -1,10 +1,60 @@
{% extends "base" %}
{%block content %}
{% block extracss %}
<style>
div.table {
display: grid;
grid-template-columns: auto 1fr repeat(3, auto);
}
div.table * {
margin: 0;
padding: 5px;
}
.colored {
background-color: #dddddd;
padding-color: #dddddd;
}
div.table a {
color: #444444;
}
.center {
text-align: center;
}
</style>
{% endblock extracss %}
{% block content %}
<h1>Panel de administración</h1>
<h3>
<a href="/admin/add_post">Agregar artículo</a>
<a href="">Editar artículo</a>
<a style="color: #444444" href="/admin/add_post">Agregar artículo</a>
<div class="table">
<h3>id</h3>
<h3 class="center">Título</h3>
<h3>Visitas</h3>
<h3>Visible</h3>
<h3></h3>
{% for post in posts %}
{% if loop.index is odd %}
<p class="colored">{{ post.id }}</p>
<p class="colored"><a href="/post/{{ post.title | slugify }}-{{ post.id }}">{{ post.title }}</a></p>
<p class="colored center">0</p>
<p class="colored center">{{ post.published }}</p>
<p class="colored"><a href="#">Editar</a></p>
{% else %}
<p>{{ post.id }}</p>
<p><a href="/post/{{ post.title | slugify }}-{{ post.id }}">{{ post.title }}</a></p>
<p class="center">0</p>
<p class="center">{{ post.published }}</p>
<p><a href="#">Editar</a></p>
{% endif %}
{% endfor %}
</div>
</h3>
{%endblock content %}
{% endblock content %}