initial commit
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
use std::io::{stdin, Read};
|
||||
|
||||
|
||||
use rust_diesel_blogs::*;
|
||||
|
||||
fn main() {
|
||||
let connection = &mut establecer_coneccion();
|
||||
|
||||
let mut title = String::new();
|
||||
let mut body = String::new();
|
||||
|
||||
println!("Cual es el titulo de su post: ");
|
||||
stdin().read_line(&mut title);
|
||||
let title = title.trim_end();
|
||||
|
||||
println!("Ingrese el cuerpo de {}, aprete {} para terminar de escribir", title, EOF);
|
||||
stdin().read_to_string(&mut body);
|
||||
|
||||
let post = crear_post(connection, title, &body);
|
||||
println!("Grabado post con id: {}", post.id);
|
||||
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
const EOF: &str = "CTRL+D";
|
||||
|
||||
#[cfg(windows)]
|
||||
const EOF: &str = "CTRL+Z";
|
||||
@@ -0,0 +1,22 @@
|
||||
use self::modelos::*;
|
||||
use diesel::{prelude::*, connection};
|
||||
use rust_diesel_blogs::*;
|
||||
|
||||
fn main() {
|
||||
use self::schema::posts::dsl::*;
|
||||
|
||||
let connection = &mut establecer_coneccion();
|
||||
let resultado = posts
|
||||
.filter(published.eq(true))
|
||||
.limit(10)
|
||||
.select(Post::as_select())
|
||||
.load(connection)
|
||||
.expect("Error cargando posts");
|
||||
|
||||
println!("Mostrando {} posts", resultado.len());
|
||||
for post in resultado {
|
||||
println!("{}", post.title);
|
||||
println!("- - - - - -",);
|
||||
println!("{}", post.body);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
use self::modelos::Post;
|
||||
use rust_diesel_blogs::*;
|
||||
use diesel::{prelude::*, connection};
|
||||
use std::env::args;
|
||||
|
||||
fn main() {
|
||||
use self::schema::posts::dsl::{posts, published};
|
||||
|
||||
let id = args()
|
||||
.nth(1)
|
||||
.expect("publicar requiere un id")
|
||||
.parse::<i32>()
|
||||
.expect("ID invalida");
|
||||
|
||||
let connection = &mut establecer_coneccion();
|
||||
|
||||
let post = diesel::update(posts.find(id))
|
||||
.set(published.eq(true))
|
||||
.returning(Post::as_returning())
|
||||
.get_results(connection)
|
||||
.unwrap();
|
||||
println!("Publicado post {}", post.get(0).unwrap().title);
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
pub mod modelos;
|
||||
pub mod schema;
|
||||
|
||||
use std::env;
|
||||
use diesel::{PgConnection, Connection, RunQueryDsl, SelectableHelper};
|
||||
use dotenvy::dotenv;
|
||||
|
||||
use crate::modelos::{NuevoPost, Post};
|
||||
|
||||
pub fn establecer_coneccion() -> PgConnection {
|
||||
dotenv().ok();
|
||||
|
||||
let database_url = env::var("DATABASE_URL")
|
||||
.expect("La DATABASE_URL tiene que esta definida");
|
||||
|
||||
PgConnection::establish(&database_url)
|
||||
.unwrap_or_else(|_| panic!("Error conectando a {}", database_url))
|
||||
|
||||
}
|
||||
|
||||
pub fn crear_post(conn: &mut PgConnection, title: &str, body: &str) -> Post {
|
||||
use crate::schema::posts;
|
||||
|
||||
let records = NuevoPost { title, body };
|
||||
|
||||
diesel::insert_into(posts::table)
|
||||
.values(records)
|
||||
.returning(Post::as_returning())
|
||||
.get_result(conn)
|
||||
.expect("Fallo la carga")
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
use diesel::{prelude::{Queryable, Insertable}, Selectable};
|
||||
|
||||
use crate::schema::posts;
|
||||
|
||||
#[derive(Queryable, Selectable)]
|
||||
#[diesel(table_name = crate::schema::posts)]
|
||||
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||
pub struct Post {
|
||||
pub id: i32,
|
||||
pub title: String,
|
||||
pub body: String,
|
||||
pub published: bool,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = posts)]
|
||||
pub struct NuevoPost<'a> {
|
||||
pub title: &'a str,
|
||||
pub body: &'a str,
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// @generated automatically by Diesel CLI.
|
||||
|
||||
diesel::table! {
|
||||
posts (id) {
|
||||
id -> Int4,
|
||||
title -> Varchar,
|
||||
body -> Text,
|
||||
published -> Bool,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user