refractor database ops into controllers
This commit is contained in:
parent
10c061d75f
commit
747e4bc1e6
@ -1,3 +1,42 @@
|
|||||||
pub(crate) struct UserController;
|
use sea_orm::{DatabaseConnection};
|
||||||
|
|
||||||
impl UserController {}
|
use super::model::user::{Model, Column};
|
||||||
|
|
||||||
|
use sea_orm::prelude::*;
|
||||||
|
use sea_orm::query::*;
|
||||||
|
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
use super::model::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub(crate) struct UserController {
|
||||||
|
database: DatabaseConnection
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UserController {
|
||||||
|
pub fn new(database: DatabaseConnection) -> Self {
|
||||||
|
Self { database: database }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn validate_password(&self, username: String, password: String) -> Result<bool, sea_orm::DbErr> {
|
||||||
|
let maybe_user = User::find()
|
||||||
|
.filter(Column::Name.eq(username))
|
||||||
|
.one(&self.database)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
match maybe_user {
|
||||||
|
Some(user) => todo!(),
|
||||||
|
None => Ok(false),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn list_all(&self, page: usize, count_per_page: usize) -> Result<Vec<Model>, sea_orm::DbErr> {
|
||||||
|
let users = User::find()
|
||||||
|
.paginate(&self.database, count_per_page)
|
||||||
|
.fetch_page(page)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(users)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -3,6 +3,8 @@ use sea_orm::DatabaseConnection;
|
|||||||
|
|
||||||
use crate::Opts;
|
use crate::Opts;
|
||||||
|
|
||||||
|
use self::controller::UserController;
|
||||||
|
|
||||||
mod urls;
|
mod urls;
|
||||||
|
|
||||||
mod controller;
|
mod controller;
|
||||||
@ -11,14 +13,14 @@ mod view;
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
database: DatabaseConnection,
|
user: UserController,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn run(
|
pub(crate) async fn run(
|
||||||
database: DatabaseConnection,
|
database: DatabaseConnection,
|
||||||
opts: Opts,
|
opts: Opts,
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let data = actix_web::web::Data::new(AppState { database: database });
|
let data = actix_web::web::Data::new(AppState { user: UserController::new(database.clone()) });
|
||||||
|
|
||||||
let server =
|
let server =
|
||||||
HttpServer::new(move || App::new().configure(urls::configure).app_data(data.clone()))
|
HttpServer::new(move || App::new().configure(urls::configure).app_data(data.clone()))
|
||||||
|
@ -19,10 +19,7 @@ async fn index_page(
|
|||||||
) -> Result<impl Responder, Box<dyn std::error::Error>> {
|
) -> Result<impl Responder, Box<dyn std::error::Error>> {
|
||||||
let params = actix_web::web::Query::<Params>::from_query(req.query_string()).unwrap();
|
let params = actix_web::web::Query::<Params>::from_query(req.query_string()).unwrap();
|
||||||
|
|
||||||
let users = User::find()
|
let users = data.user.list_all(params.page.unwrap_or(0), params.posts_per_page.unwrap_or(5)).await?;
|
||||||
.paginate(&data.database, params.posts_per_page.unwrap_or(10))
|
|
||||||
.fetch_page(params.page.unwrap_or(0))
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(actix_web::web::Json(users))
|
Ok(actix_web::web::Json(users))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user