added more info to admin panel
parent
27cac09a08
commit
64c2317bd7
|
@ -56,8 +56,12 @@ fn panel(mut cookies: Cookies<'_>) -> Result<Template, Redirect> {
|
||||||
match cookies.get_private("user") {
|
match cookies.get_private("user") {
|
||||||
Some(cookie) => {
|
Some(cookie) => {
|
||||||
if cookie.value() == password {
|
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))
|
Ok(Template::render("admin/panel", context))
|
||||||
} else {
|
} else {
|
||||||
Err(Redirect::to("/admin"))
|
Err(Redirect::to("/admin"))
|
||||||
|
|
|
@ -17,19 +17,23 @@ pub mod posts {
|
||||||
use crate::controllers::*;
|
use crate::controllers::*;
|
||||||
use crate::models::{NewPost, Post};
|
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::*;
|
use crate::schema::posts::dsl::*;
|
||||||
|
|
||||||
let connection = establish_connection();
|
let connection = establish_connection();
|
||||||
let visible_posts = posts
|
let visible_posts = posts.filter(published.eq(true)).order(created_at.desc());
|
||||||
.filter(published.eq(true))
|
let visible_posts = match page {
|
||||||
.order(created_at.desc())
|
Some(number_page) => visible_posts
|
||||||
.limit(MAX_POSTS_PER_PAGE as i64)
|
.limit(MAX_POSTS_PER_PAGE as i64)
|
||||||
.offset((MAX_POSTS_PER_PAGE * (page - 1)) as i64)
|
.offset((MAX_POSTS_PER_PAGE * (number_page - 1)) as i64)
|
||||||
.load::<Post>(&connection)
|
.load::<Post>(&connection)
|
||||||
.expect("Error loading posts");
|
.expect("Error loading posts"),
|
||||||
|
None => visible_posts
|
||||||
|
.load::<Post>(&connection)
|
||||||
|
.expect("Error loading posts"),
|
||||||
|
};
|
||||||
|
|
||||||
let number_of_posts: i64 = posts
|
let number_of_posts: i64 = posts
|
||||||
.filter(published.eq(true))
|
.filter(published.eq(true))
|
||||||
|
|
|
@ -32,7 +32,7 @@ fn index(page: Option<u64>) -> Template {
|
||||||
|
|
||||||
let mut context = get_context();
|
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;
|
let total_pages = (n_posts as f64 / posts::MAX_POSTS_PER_PAGE as f64).ceil() as i64;
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,60 @@
|
||||||
{% extends "base" %}
|
{% extends "base" %}
|
||||||
|
|
||||||
|
{% 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 %}
|
{% block content %}
|
||||||
<h1>Panel de administración</h1>
|
<h1>Panel de administración</h1>
|
||||||
|
|
||||||
<h3>
|
<h3>
|
||||||
<a href="/admin/add_post">Agregar artículo</a>
|
<a style="color: #444444" href="/admin/add_post">Agregar artículo</a>
|
||||||
<a href="">Editar 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>
|
</h3>
|
||||||
{% endblock content %}
|
{% endblock content %}
|
||||||
|
|
Loading…
Reference in New Issue