Add Delete row

This commit is contained in:
2025-01-29 11:40:18 +01:00
parent 380795d054
commit 53e7d79f7c
6 changed files with 69 additions and 20 deletions

View File

@@ -1,13 +1,7 @@
use diesel::prelude::*;
use anyhow::{anyhow, Result, Context};
use askama_axum::Template;
use axum::{
extract::{Path, State},
http::{Method, header, StatusCode, Request, Uri},
response::{IntoResponse, Response, Html},
routing::{get, post},
Json, Router, Form
};
use axum::{extract::{Path, State}, http::{Method, header, StatusCode, Request, Uri}, response::{IntoResponse, Response, Html}, routing::{get, post}, Json, Router, Form, http};
use axum::http::HeaderValue;
use deadpool_diesel::sqlite::{Runtime, Manager, Pool};
use clap::Parser;
@@ -97,7 +91,7 @@ async fn main() -> Result<()> {
.nest_service("/assets", ServeDir::new("assets"))
.route("/", get(index))
.route("/feed/", get(get_list_feed).post(post_feed))
.route("/feed/:id/", get(get_feed))
.route("/feed/:id", get(get_feed).delete(delete_feed))
.layer(middlewares)
.with_state(context.clone());
// Run our app with hyper
@@ -170,7 +164,10 @@ async fn post_feed(
#[derive(Template)]
#[template(path = "feeds.html")]
#[template(source="\
{% for feed in feeds %}
{% include \"feed.html\" %}
{% endfor %}", ext="html")]
struct FeedsTemplate {
feeds: Vec<RssFeed>
}
@@ -189,6 +186,26 @@ async fn get_list_feed(
FeedsTemplate{ feeds: result }
}
async fn delete_feed(
State(ctx): State<AppContext>,
Path(feed_id): Path<i32>,
) -> impl IntoResponse {
let conn = ctx.pool.get().await.unwrap();
use self::schema::rss_feeds::dsl::*;
let num_deleted = conn.interact(move |conn| {
diesel::delete(rss_feeds.find(feed_id))
.execute(conn)
.expect("Error deleting posts")
}).await.unwrap();
if num_deleted == 0 {
(StatusCode::NOT_FOUND, "Not found")
} else {
// HTMX swaps only on 200, so we cannot use NO_CONTENT
// In fact, MSDN says that NO_CONTENT means "do nothing", but this is debated.
(StatusCode::OK, "")
}
}
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {