more refactoring
This commit is contained in:
parent
13e17770ca
commit
7268d49b4a
12 changed files with 319 additions and 189 deletions
|
|
@ -1,7 +1,6 @@
|
|||
use axum::{
|
||||
Json,
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
};
|
||||
use sea_orm::{EntityTrait, QueryOrder, QuerySelect};
|
||||
use std::sync::Arc;
|
||||
|
|
@ -13,16 +12,17 @@ use crate::domain::tasks::{
|
|||
self, CreateTaskRequest, RecentRunResponse, TaskResponse, UpdateTaskRequest,
|
||||
};
|
||||
|
||||
use crate::error::AppResult;
|
||||
|
||||
pub async fn list_tasks(
|
||||
_user: AuthenticatedUser,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Json<Vec<TaskResponse>>, (StatusCode, String)> {
|
||||
) -> AppResult<Json<Vec<TaskResponse>>> {
|
||||
let tasks = crate::entities::task::Entity::find()
|
||||
.find_with_related(crate::entities::task_run::Entity)
|
||||
.order_by_desc(crate::entities::task::Column::CreatedAt)
|
||||
.all(&state.db)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
.await?;
|
||||
|
||||
let response = tasks
|
||||
.into_iter()
|
||||
|
|
@ -54,7 +54,7 @@ pub async fn create_task(
|
|||
_user: AuthenticatedUser,
|
||||
State(state): State<Arc<AppState>>,
|
||||
Json(payload): Json<CreateTaskRequest>,
|
||||
) -> Result<Json<TaskResponse>, (StatusCode, String)> {
|
||||
) -> AppResult<Json<TaskResponse>> {
|
||||
let task_id = Uuid::new_v4();
|
||||
|
||||
let new_task = crate::entities::task::ActiveModel {
|
||||
|
|
@ -65,10 +65,7 @@ pub async fn create_task(
|
|||
};
|
||||
|
||||
use sea_orm::ActiveModelTrait;
|
||||
new_task
|
||||
.insert(&state.db)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
new_task.insert(&state.db).await?;
|
||||
|
||||
if let Some(cron) = &payload.cron {
|
||||
let _ = state.scheduler.add_task_job(task_id, cron).await;
|
||||
|
|
@ -79,19 +76,18 @@ pub async fn create_task(
|
|||
tasks::get_task_inner(task_id, &state.db)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
|
||||
.map_err(|e| crate::error::AppError::Internal(e.to_string()))
|
||||
}
|
||||
|
||||
pub async fn rerun_task(
|
||||
_user: AuthenticatedUser,
|
||||
State(state): State<Arc<AppState>>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<TaskResponse>, (StatusCode, String)> {
|
||||
) -> AppResult<Json<TaskResponse>> {
|
||||
let task = crate::entities::task::Entity::find_by_id(id)
|
||||
.one(&state.db)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
|
||||
.ok_or((StatusCode::NOT_FOUND, "Task not found".to_string()))?;
|
||||
.await?
|
||||
.ok_or_else(|| crate::error::AppError::NotFound("Task not found".to_string()))?;
|
||||
|
||||
tasks::execute_agent_run(
|
||||
&state.db,
|
||||
|
|
@ -102,7 +98,7 @@ pub async fn rerun_task(
|
|||
)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
|
||||
.map_err(|e| crate::error::AppError::Internal(e.to_string()))
|
||||
}
|
||||
|
||||
pub async fn update_task(
|
||||
|
|
@ -110,22 +106,19 @@ pub async fn update_task(
|
|||
State(state): State<Arc<AppState>>,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(payload): Json<UpdateTaskRequest>,
|
||||
) -> Result<Json<TaskResponse>, (StatusCode, String)> {
|
||||
let mut task: crate::entities::task::ActiveModel =
|
||||
crate::entities::task::Entity::find_by_id(id)
|
||||
.one(&state.db)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
|
||||
.ok_or((StatusCode::NOT_FOUND, "Task not found".to_string()))?
|
||||
.into();
|
||||
) -> AppResult<Json<TaskResponse>> {
|
||||
let task: crate::entities::task::ActiveModel = crate::entities::task::Entity::find_by_id(id)
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.ok_or_else(|| crate::error::AppError::NotFound("Task not found".to_string()))?
|
||||
.into();
|
||||
|
||||
let mut task = task;
|
||||
task.goal = sea_orm::Set(payload.goal.clone());
|
||||
task.cron = sea_orm::Set(payload.cron.clone());
|
||||
|
||||
use sea_orm::ActiveModelTrait;
|
||||
task.update(&state.db)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
task.update(&state.db).await?;
|
||||
|
||||
if let Some(cron) = &payload.cron {
|
||||
let _ = state.scheduler.add_task_job(id, cron).await;
|
||||
|
|
@ -136,31 +129,30 @@ pub async fn update_task(
|
|||
tasks::get_task_inner(id, &state.db)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
|
||||
.map_err(|e| crate::error::AppError::Internal(e.to_string()))
|
||||
}
|
||||
|
||||
pub async fn get_task(
|
||||
_user: AuthenticatedUser,
|
||||
Path(id): Path<Uuid>,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Json<TaskResponse>, (StatusCode, String)> {
|
||||
) -> AppResult<Json<TaskResponse>> {
|
||||
tasks::get_task_inner(id, &state.db)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
|
||||
.map_err(|e| crate::error::AppError::Internal(e.to_string()))
|
||||
}
|
||||
|
||||
pub async fn get_recent_runs(
|
||||
_user: AuthenticatedUser,
|
||||
State(state): State<Arc<AppState>>,
|
||||
) -> Result<Json<Vec<RecentRunResponse>>, (StatusCode, String)> {
|
||||
) -> AppResult<Json<Vec<RecentRunResponse>>> {
|
||||
let results = crate::entities::task_run::Entity::find()
|
||||
.find_also_related(crate::entities::task::Entity)
|
||||
.order_by_desc(crate::entities::task_run::Column::CreatedAt)
|
||||
.limit(50)
|
||||
.all(&state.db)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
.await?;
|
||||
|
||||
let response = results
|
||||
.into_iter()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue