Add ability to create tasks
This commit is contained in:
parent
36baf3a8d8
commit
4717946fa3
|
@ -128,7 +128,9 @@ async fn main() -> Result<(), Error> {
|
|||
|
||||
let task_routes = Router::new()
|
||||
.route("/update/:id", get(tasks::update_form))
|
||||
.route("/update/:id", post(tasks::update_backend));
|
||||
.route("/update/:id", post(tasks::update_backend))
|
||||
.route("/create", get(tasks::create_form))
|
||||
.route("/create", post(tasks::create_backend));
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(homepage))
|
||||
|
@ -207,5 +209,5 @@ async fn homepage(
|
|||
},
|
||||
)?;
|
||||
|
||||
return Ok(Html(rendered));
|
||||
Ok(Html(rendered))
|
||||
}
|
||||
|
|
44
src/tasks.rs
44
src/tasks.rs
|
@ -38,11 +38,10 @@ pub async fn update_form(
|
|||
if let Some(task) = task {
|
||||
let c = ctx! {
|
||||
"task" => task,
|
||||
"update" => true,
|
||||
"url" => uri.to_string()
|
||||
};
|
||||
|
||||
let rendered = tera.render("tasks/create.html", &c)?;
|
||||
let rendered = tera.render("tasks/update.html", &c)?;
|
||||
|
||||
#[allow(clippy::needless_return)] // it's a compile error, clippy
|
||||
return Ok(Html(rendered));
|
||||
|
@ -51,6 +50,22 @@ pub async fn update_form(
|
|||
Err(StatusCode::NOT_FOUND.into())
|
||||
}
|
||||
|
||||
pub async fn create_form(
|
||||
Extension(tera): Extension<Arc<Tera>>,
|
||||
OriginalUri(uri): OriginalUri,
|
||||
session: ReadableSession,
|
||||
) -> Result<Html<String>, Error> {
|
||||
let _username = login_or_redirect!(session, "/login");
|
||||
|
||||
let c = ctx! {
|
||||
"url" => uri.to_string()
|
||||
};
|
||||
|
||||
let rendered = tera.render("tasks/create.html", &c)?;
|
||||
|
||||
Ok(Html(rendered))
|
||||
}
|
||||
|
||||
pub async fn update_backend(
|
||||
Path(id): Path<i32>,
|
||||
Extension(pool): Extension<Arc<Pool<Postgres>>>,
|
||||
|
@ -59,10 +74,33 @@ pub async fn update_backend(
|
|||
sqlx::query("update tasks set title=$1,description=$2,status=$3 where id=$4")
|
||||
.bind(&data.title)
|
||||
.bind(&data.description)
|
||||
.bind(&data.status)
|
||||
.bind(data.status)
|
||||
.bind(id)
|
||||
.execute(&*pool)
|
||||
.await?;
|
||||
|
||||
Ok(Redirect::to("/"))
|
||||
}
|
||||
|
||||
pub async fn create_backend(
|
||||
Extension(pool): Extension<Arc<Pool<Postgres>>>,
|
||||
Form(data): Form<Task>,
|
||||
session: ReadableSession,
|
||||
) -> Result<Redirect, Error> {
|
||||
let username = login_or_redirect!(session, "/login");
|
||||
|
||||
let (user_id,): (i32,) = sqlx::query_as("select id from users where username=$1")
|
||||
.bind(&username)
|
||||
.fetch_one(&*pool)
|
||||
.await?;
|
||||
|
||||
sqlx::query("insert into tasks (owner,title, description, status) values ($1, $2, $3, $4)")
|
||||
.bind(user_id)
|
||||
.bind(&data.title)
|
||||
.bind(&data.description)
|
||||
.bind(data.status)
|
||||
.execute(&*pool)
|
||||
.await?;
|
||||
|
||||
Ok(Redirect::to("/"))
|
||||
}
|
||||
|
|
|
@ -2,17 +2,18 @@
|
|||
{% block content %}
|
||||
<form action="{{url}}" method="POST">
|
||||
<label for="title">Title:</label>
|
||||
<input type="text" name="title" value="{{task.title}}">
|
||||
<input type="text" name="title">
|
||||
<br>
|
||||
<label for="description">Description:</label>
|
||||
<input type="text" name="description" value="{{task.description}}">
|
||||
<input type="text" name="description">
|
||||
<br>
|
||||
<select name="status">
|
||||
<option value="0" {% if task.status == 0 %}selected{% endif %}>To Do</option>
|
||||
<option value="1" {% if task.status == 1 %}selected{% endif %}>In Progress</option>
|
||||
<option value="2" {% if task.status == 2 %}selected{% endif %}>Done</option>
|
||||
<option value="0">To Do</option>
|
||||
<option value="1">In Progress</option>
|
||||
<option value="2">Done</option>
|
||||
</select>
|
||||
<input type="Submit" value="{% if update %}Update Task{% else %}Create Task{% endif %}">
|
||||
<br>
|
||||
<input type="Submit" value="Create Task">
|
||||
<br>
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
|
19
templates/tasks/update.html
Normal file
19
templates/tasks/update.html
Normal file
|
@ -0,0 +1,19 @@
|
|||
{% extends 'base.html' %}
|
||||
{% block content %}
|
||||
<form action="{{url}}" method="POST">
|
||||
<label for="title">Title:</label>
|
||||
<input type="text" name="title" value="{{task.title}}">
|
||||
<br>
|
||||
<label for="description">Description:</label>
|
||||
<input type="text" name="description" value="{{task.description}}">
|
||||
<br>
|
||||
<select name="status">
|
||||
<option value="0" {% if task.status == 0 %}selected{% endif %}>To Do</option>
|
||||
<option value="1" {% if task.status == 1 %}selected{% endif %}>In Progress</option>
|
||||
<option value="2" {% if task.status == 2 %}selected{% endif %}>Done</option>
|
||||
</select>
|
||||
<br>
|
||||
<input type="Submit" value="Update Task">
|
||||
<br>
|
||||
</form>
|
||||
{% endblock %}
|
Loading…
Reference in a new issue