Edit post
This commit is contained in:
parent
f97c081c2a
commit
c427e4e89b
@ -94,6 +94,47 @@ async fn create(
|
|||||||
Ok(HttpResponse::Found().header("location", "/").finish())
|
Ok(HttpResponse::Found().header("location", "/").finish())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[get("/{id}")]
|
||||||
|
async fn edit(data: web::Data<AppState>, id: web::Path<i32>) -> Result<HttpResponse, Error> {
|
||||||
|
let conn = sea_orm::Database::connect(&data.db_url).await.unwrap();
|
||||||
|
let template = &data.templates;
|
||||||
|
|
||||||
|
let post: post::Model = Post::find_by_id(id.into_inner())
|
||||||
|
.one(&conn)
|
||||||
|
.await
|
||||||
|
.expect("could not find post")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut ctx = tera::Context::new();
|
||||||
|
ctx.insert("post", &post);
|
||||||
|
|
||||||
|
let body = template
|
||||||
|
.render("edit.html.tera", &ctx)
|
||||||
|
.map_err(|_| error::ErrorInternalServerError("Template error"))?;
|
||||||
|
Ok(HttpResponse::Ok().content_type("text/html").body(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/{id}")]
|
||||||
|
async fn update(
|
||||||
|
data: web::Data<AppState>,
|
||||||
|
id: web::Path<i32>,
|
||||||
|
post_form: web::Form<post::Model>,
|
||||||
|
) -> Result<HttpResponse, Error> {
|
||||||
|
let conn = sea_orm::Database::connect(&data.db_url).await.unwrap();
|
||||||
|
let form = post_form.into_inner();
|
||||||
|
|
||||||
|
post::ActiveModel {
|
||||||
|
id: Set(Some(id.into_inner())),
|
||||||
|
title: Set(form.title.to_owned()),
|
||||||
|
text: Set(form.text.to_owned()),
|
||||||
|
}
|
||||||
|
.save(&conn)
|
||||||
|
.await
|
||||||
|
.expect("could not edit post");
|
||||||
|
|
||||||
|
Ok(HttpResponse::Found().header("location", "/").finish())
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
@ -138,4 +179,6 @@ pub fn init(cfg: &mut web::ServiceConfig) {
|
|||||||
cfg.service(list);
|
cfg.service(list);
|
||||||
cfg.service(new);
|
cfg.service(new);
|
||||||
cfg.service(create);
|
cfg.service(create);
|
||||||
|
cfg.service(edit);
|
||||||
|
cfg.service(update);
|
||||||
}
|
}
|
||||||
|
50
examples/actix_example/templates/edit.html.tera
Normal file
50
examples/actix_example/templates/edit.html.tera
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
{% extends "layout.html.tera" %} {% block content %}
|
||||||
|
<div class="row">
|
||||||
|
<h4>Edit Post</h4>
|
||||||
|
<div class="twelve columns">
|
||||||
|
<div class="ten columns">
|
||||||
|
<form action="/{{ post.id }}" method="post">
|
||||||
|
<div class="twelve columns">
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="title"
|
||||||
|
name="title"
|
||||||
|
id="title"
|
||||||
|
value="{{ post.title }}"
|
||||||
|
autofocus
|
||||||
|
class="u-full-width"
|
||||||
|
/>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="content"
|
||||||
|
name="text"
|
||||||
|
id="text"
|
||||||
|
value="{{ post.text }}"
|
||||||
|
autofocus
|
||||||
|
class="u-full-width"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="twelve columns">
|
||||||
|
<div class="two columns">
|
||||||
|
<a href="/">
|
||||||
|
<input type="button" value="cancel" />
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
<div class="eight columns"></div>
|
||||||
|
<div class="two columns">
|
||||||
|
<input type="submit" value="save post" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div class="two columns">
|
||||||
|
<form action="/{{ post.id }}" method="post">
|
||||||
|
<div class="two columns">
|
||||||
|
<input type="hidden" name="_method" value="delete" />
|
||||||
|
<input id="delete-button" type="submit" value="delete post" />
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock content %}
|
Loading…
x
Reference in New Issue
Block a user