Add new post
This commit is contained in:
parent
5d7a9ca782
commit
f97c081c2a
@ -9,6 +9,7 @@ use actix_web::{
|
||||
error, get, middleware, post, web, App, Error, HttpRequest, HttpResponse, HttpServer, Result,
|
||||
};
|
||||
use listenfd::ListenFd;
|
||||
use sea_orm::entity::*;
|
||||
use sea_orm::query::*;
|
||||
use sea_orm::DatabaseConnection;
|
||||
use sea_orm::EntityTrait;
|
||||
@ -20,7 +21,7 @@ mod post;
|
||||
pub use post::Entity as Post;
|
||||
mod setup;
|
||||
|
||||
const DEFAULT_POSTS_PER_PAGE: usize = 4;
|
||||
const DEFAULT_POSTS_PER_PAGE: usize = 25;
|
||||
|
||||
struct AppState {
|
||||
db_url: String,
|
||||
@ -62,6 +63,37 @@ async fn list(req: HttpRequest, data: web::Data<AppState>) -> Result<HttpRespons
|
||||
Ok(HttpResponse::Ok().content_type("text/html").body(body))
|
||||
}
|
||||
|
||||
#[get("/new")]
|
||||
async fn new(data: web::Data<AppState>) -> Result<HttpResponse, Error> {
|
||||
let template = &data.templates;
|
||||
let ctx = tera::Context::new();
|
||||
let body = template
|
||||
.render("new.html.tera", &ctx)
|
||||
.map_err(|_| error::ErrorInternalServerError("Template error"))?;
|
||||
Ok(HttpResponse::Ok().content_type("text/html").body(body))
|
||||
}
|
||||
|
||||
#[post("/")]
|
||||
async fn create(
|
||||
data: web::Data<AppState>,
|
||||
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 {
|
||||
title: Set(form.title.to_owned()),
|
||||
text: Set(form.text.to_owned()),
|
||||
..Default::default()
|
||||
}
|
||||
.save(&conn)
|
||||
.await
|
||||
.expect("could not insert post");
|
||||
|
||||
Ok(HttpResponse::Found().header("location", "/").finish())
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
@ -104,4 +136,6 @@ async fn main() -> std::io::Result<()> {
|
||||
|
||||
pub fn init(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(list);
|
||||
cfg.service(new);
|
||||
cfg.service(create);
|
||||
}
|
||||
|
38
examples/actix_example/templates/new.html.tera
Normal file
38
examples/actix_example/templates/new.html.tera
Normal file
@ -0,0 +1,38 @@
|
||||
{% extends "layout.html.tera" %} {% block content %}
|
||||
<div class="row">
|
||||
<h4>New Post</h4>
|
||||
<form action="/" method="post">
|
||||
<div class="twelve columns">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="enter title"
|
||||
name="title"
|
||||
id="title"
|
||||
value=""
|
||||
autofocus
|
||||
class="u-full-width"
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="enter content"
|
||||
name="text"
|
||||
id="text"
|
||||
value=""
|
||||
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>
|
||||
{% endblock content %}
|
@ -29,7 +29,7 @@ pub use post::Entity as Post;
|
||||
const DEFAULT_POSTS_PER_PAGE: usize = 25;
|
||||
|
||||
#[get("/new")]
|
||||
fn new() -> Template {
|
||||
async fn new() -> Template {
|
||||
Template::render("new", &Context::default())
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user