Complete insert and update on create. Basically first complete working version
This commit is contained in:
64
src/main.rs
64
src/main.rs
@@ -1,6 +1,4 @@
|
||||
use diesel::prelude::*;
|
||||
use std::env;
|
||||
use std::sync::Arc;
|
||||
use anyhow::{anyhow, Result, Context};
|
||||
use askama_axum::Template;
|
||||
use axum::{
|
||||
@@ -8,11 +6,11 @@ use axum::{
|
||||
http::{Method, header, StatusCode, Request, Uri},
|
||||
response::{IntoResponse, Response, Html},
|
||||
routing::{get, post},
|
||||
Json, Router,
|
||||
Json, Router, Form
|
||||
};
|
||||
use axum::http::HeaderValue;
|
||||
use deadpool_diesel::sqlite::{Runtime, Manager, Pool};
|
||||
use clap::Parser;
|
||||
use tokio::sync::Mutex;
|
||||
use tower::{ServiceBuilder};
|
||||
use tower_http::{
|
||||
cors::{Any, CorsLayer},
|
||||
@@ -20,6 +18,7 @@ use tower_http::{
|
||||
services::ServeDir,
|
||||
LatencyUnit
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use tracing::{
|
||||
info, error, debug, info_span, enabled,
|
||||
instrument, Level, Span
|
||||
@@ -44,7 +43,6 @@ struct Config {
|
||||
struct AppContext {
|
||||
config: Config,
|
||||
pool: Pool,
|
||||
counter: Arc<Mutex<i32>>
|
||||
}
|
||||
|
||||
async fn establish_connection(database_url: &str) -> Pool {
|
||||
@@ -71,7 +69,6 @@ async fn main() -> Result<()> {
|
||||
let context = AppContext {
|
||||
config: config.clone(),
|
||||
pool: pool.clone(),
|
||||
counter: Arc::new(Mutex::new(0)),
|
||||
};
|
||||
// Prepare Middlewares
|
||||
let cors = CorsLayer::very_permissive();
|
||||
@@ -99,6 +96,7 @@ async fn main() -> Result<()> {
|
||||
let app = Router::new()
|
||||
.nest_service("/assets", ServeDir::new("assets"))
|
||||
.route("/", get(index))
|
||||
.route("/feed/", get(get_list_feed).post(post_feed))
|
||||
.route("/feed/:id/", get(get_feed))
|
||||
.layer(middlewares)
|
||||
.with_state(context.clone());
|
||||
@@ -144,15 +142,42 @@ async fn get_feed(
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
struct IndexTemplate {
|
||||
feeds: Vec<RssFeed>,
|
||||
#[derive(Deserialize)]
|
||||
struct PostForm{
|
||||
name: String,
|
||||
feed_url: String,
|
||||
}
|
||||
|
||||
async fn index(
|
||||
async fn post_feed(
|
||||
State(ctx): State<AppContext>,
|
||||
) -> IndexTemplate {
|
||||
Form(post): Form<PostForm>,
|
||||
) -> impl IntoResponse {
|
||||
let conn = ctx.pool.get().await.unwrap();
|
||||
use self::schema::rss_feeds::dsl::*;
|
||||
let result = conn.interact(move |conn| {
|
||||
let new_feed = NewRssFeed{name: post.name.as_str(), feed_url: post.feed_url.as_str() };
|
||||
diesel::insert_into(rss_feeds)
|
||||
.values(&new_feed)
|
||||
.returning(RssFeed::as_select())
|
||||
.get_result(conn)
|
||||
.expect("Error saving new feed")
|
||||
}).await.unwrap();
|
||||
let mut res = FeedTemplate{ feed: result }.into_response();
|
||||
// Trigger HTMX Update event
|
||||
res.headers_mut().insert("HX-Trigger", HeaderValue::from_static("newFeed"));
|
||||
res
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "feeds.html")]
|
||||
struct FeedsTemplate {
|
||||
feeds: Vec<RssFeed>
|
||||
}
|
||||
|
||||
async fn get_list_feed(
|
||||
State(ctx): State<AppContext>,
|
||||
) -> FeedsTemplate {
|
||||
let conn = ctx.pool.get().await.unwrap();
|
||||
use self::schema::rss_feeds::dsl::*;
|
||||
let result = conn.interact(|conn| {
|
||||
@@ -161,5 +186,18 @@ async fn index(
|
||||
.load(conn)
|
||||
.expect("Error loading feeds")
|
||||
}).await.unwrap();
|
||||
IndexTemplate { feeds: result }
|
||||
FeedsTemplate{ feeds: result }
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
struct IndexTemplate {
|
||||
feeds: FeedsTemplate
|
||||
}
|
||||
|
||||
async fn index(
|
||||
State(ctx): State<AppContext>,
|
||||
) -> IndexTemplate {
|
||||
let feeds = get_list_feed(State(ctx)).await;
|
||||
IndexTemplate { feeds }
|
||||
}
|
||||
|
||||
@@ -8,4 +8,12 @@ pub struct RssFeed {
|
||||
pub name: String,
|
||||
pub feed_url: String,
|
||||
pub last_pub_date: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = crate::schema::rss_feeds)]
|
||||
pub struct NewRssFeed<'a> {
|
||||
pub name: &'a str,
|
||||
pub feed_url: &'a str,
|
||||
}
|
||||
Reference in New Issue
Block a user