more refactoring
This commit is contained in:
parent
7268d49b4a
commit
ce5d2c9fb4
10 changed files with 251 additions and 39 deletions
|
|
@ -55,11 +55,22 @@ pub async fn create_task(
|
|||
State(state): State<Arc<AppState>>,
|
||||
Json(payload): Json<CreateTaskRequest>,
|
||||
) -> AppResult<Json<TaskResponse>> {
|
||||
let task_id = Uuid::new_v4();
|
||||
if payload.goal.trim().is_empty() {
|
||||
return Err(crate::error::AppError::InvalidRequest(
|
||||
"Goal cannot be empty".into(),
|
||||
));
|
||||
}
|
||||
|
||||
if payload.goal.trim().len() < 5 {
|
||||
return Err(crate::error::AppError::InvalidRequest(
|
||||
"Goal is too short (min 5 characters)".into(),
|
||||
));
|
||||
}
|
||||
|
||||
let task_id = Uuid::new_v4();
|
||||
let new_task = crate::entities::task::ActiveModel {
|
||||
id: sea_orm::Set(task_id),
|
||||
goal: sea_orm::Set(payload.goal.clone()),
|
||||
goal: sea_orm::Set(payload.goal),
|
||||
cron: sea_orm::Set(payload.cron.clone()),
|
||||
created_at: sea_orm::Set(chrono::Utc::now().into()),
|
||||
};
|
||||
|
|
@ -69,14 +80,9 @@ pub async fn create_task(
|
|||
|
||||
if let Some(cron) = &payload.cron {
|
||||
let _ = state.scheduler.add_task_job(task_id, cron).await;
|
||||
} else {
|
||||
let _ = state.scheduler.remove_task_job(task_id).await;
|
||||
}
|
||||
|
||||
tasks::get_task_inner(task_id, &state.db)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(|e| crate::error::AppError::Internal(e.to_string()))
|
||||
tasks::get_task_inner(task_id, &state.db).await.map(Json)
|
||||
}
|
||||
|
||||
pub async fn rerun_task(
|
||||
|
|
@ -107,6 +113,18 @@ pub async fn update_task(
|
|||
Path(id): Path<Uuid>,
|
||||
Json(payload): Json<UpdateTaskRequest>,
|
||||
) -> AppResult<Json<TaskResponse>> {
|
||||
if payload.goal.trim().is_empty() {
|
||||
return Err(crate::error::AppError::InvalidRequest(
|
||||
"Goal cannot be empty".into(),
|
||||
));
|
||||
}
|
||||
|
||||
if payload.goal.trim().len() < 5 {
|
||||
return Err(crate::error::AppError::InvalidRequest(
|
||||
"Goal is too short (min 5 characters)".into(),
|
||||
));
|
||||
}
|
||||
|
||||
let task: crate::entities::task::ActiveModel = crate::entities::task::Entity::find_by_id(id)
|
||||
.one(&state.db)
|
||||
.await?
|
||||
|
|
@ -114,7 +132,7 @@ pub async fn update_task(
|
|||
.into();
|
||||
|
||||
let mut task = task;
|
||||
task.goal = sea_orm::Set(payload.goal.clone());
|
||||
task.goal = sea_orm::Set(payload.goal);
|
||||
task.cron = sea_orm::Set(payload.cron.clone());
|
||||
|
||||
use sea_orm::ActiveModelTrait;
|
||||
|
|
@ -126,10 +144,7 @@ pub async fn update_task(
|
|||
let _ = state.scheduler.remove_task_job(id).await;
|
||||
}
|
||||
|
||||
tasks::get_task_inner(id, &state.db)
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(|e| crate::error::AppError::Internal(e.to_string()))
|
||||
tasks::get_task_inner(id, &state.db).await.map(Json)
|
||||
}
|
||||
|
||||
pub async fn get_task(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue