Move Task to tasks.rs
This commit is contained in:
parent
76249c3a0c
commit
5a5d668d74
10
src/main.rs
10
src/main.rs
|
@ -20,6 +20,7 @@ use serde::Deserialize;
|
|||
use sqlx::postgres::PgPoolOptions;
|
||||
use sqlx::{Pool, Postgres};
|
||||
use std::sync::Arc;
|
||||
use tasks::RawTask;
|
||||
use tera::Tera;
|
||||
use thiserror::Error as ThisError;
|
||||
use tower::ServiceBuilder;
|
||||
|
@ -171,13 +172,6 @@ macro_rules! ctx {
|
|||
}};
|
||||
}
|
||||
|
||||
#[derive(sqlx::FromRow, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Task {
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
pub status: i32,
|
||||
}
|
||||
|
||||
async fn homepage(
|
||||
session: ReadableSession,
|
||||
Extension(tera): Extension<Arc<Tera>>,
|
||||
|
@ -190,7 +184,7 @@ async fn homepage(
|
|||
.fetch_one(&*pool)
|
||||
.await?;
|
||||
|
||||
let tasks: Vec<Task> =
|
||||
let tasks: Vec<RawTask> =
|
||||
sqlx::query_as("select title,description,status from tasks where owner=$1")
|
||||
.bind(id)
|
||||
.fetch_all(&*pool)
|
||||
|
|
14
src/tasks.rs
14
src/tasks.rs
|
@ -1,7 +1,6 @@
|
|||
use crate::ctx;
|
||||
use crate::login_or_redirect;
|
||||
use crate::Error;
|
||||
use crate::Task;
|
||||
use axum::extract::Extension;
|
||||
use axum::extract::Form;
|
||||
use axum::extract::OriginalUri;
|
||||
|
@ -14,6 +13,13 @@ use sqlx::{Pool, Postgres};
|
|||
use std::sync::Arc;
|
||||
use tera::Tera;
|
||||
|
||||
#[derive(sqlx::FromRow, serde::Serialize, serde::Deserialize)]
|
||||
pub struct RawTask {
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
pub status: i32,
|
||||
}
|
||||
|
||||
pub async fn update_form(
|
||||
Path(id): Path<i32>,
|
||||
Extension(tera): Extension<Arc<Tera>>,
|
||||
|
@ -28,7 +34,7 @@ pub async fn update_form(
|
|||
.fetch_one(&*pool)
|
||||
.await?;
|
||||
|
||||
let task: Option<Task> =
|
||||
let task: Option<RawTask> =
|
||||
sqlx::query_as("select title,description,status from tasks where id=$1 and owner=$2")
|
||||
.bind(id)
|
||||
.bind(user_id)
|
||||
|
@ -69,7 +75,7 @@ pub async fn create_form(
|
|||
pub async fn update_backend(
|
||||
Path(id): Path<i32>,
|
||||
Extension(pool): Extension<Arc<Pool<Postgres>>>,
|
||||
Form(data): Form<Task>,
|
||||
Form(data): Form<RawTask>,
|
||||
) -> Result<Redirect, Error> {
|
||||
sqlx::query("update tasks set title=$1,description=$2,status=$3 where id=$4")
|
||||
.bind(&data.title)
|
||||
|
@ -84,7 +90,7 @@ pub async fn update_backend(
|
|||
|
||||
pub async fn create_backend(
|
||||
Extension(pool): Extension<Arc<Pool<Postgres>>>,
|
||||
Form(data): Form<Task>,
|
||||
Form(data): Form<RawTask>,
|
||||
session: ReadableSession,
|
||||
) -> Result<Redirect, Error> {
|
||||
let username = login_or_redirect!(session, "/login");
|
||||
|
|
Loading…
Reference in a new issue