feat: add a server function for completing a task
This commit is contained in:
		| @@ -1,12 +1,14 @@ | ||||
| use chrono::{Datelike, Days, Months, NaiveDate}; | ||||
| use crate::errors::error::Error; | ||||
| use crate::errors::error_vec::ErrorVec; | ||||
| use crate::models::task::{NewTask, Task}; | ||||
| use crate::server::database_connection::establish_database_connection; | ||||
| use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl, SelectableHelper}; | ||||
| use diesel::{ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl, SelectableHelper}; | ||||
| use dioxus::prelude::*; | ||||
| use time::util::days_in_year_month; | ||||
| use validator::Validate; | ||||
| use crate::errors::task_error::TaskError; | ||||
| use crate::models::category::Category; | ||||
| use crate::models::category::{Category, ReoccurrenceInterval}; | ||||
|  | ||||
| #[server] | ||||
| pub(crate) async fn create_task(new_task: NewTask) | ||||
| @@ -30,6 +32,24 @@ pub(crate) async fn create_task(new_task: NewTask) | ||||
|     Ok(new_task) | ||||
| } | ||||
|  | ||||
| #[server] | ||||
| pub(crate) async fn get_task(task_id: i32) -> Result<Task, ServerFnError<ErrorVec<Error>>> { | ||||
|     use crate::schema::tasks::dsl::*; | ||||
|  | ||||
|     let mut connection = establish_database_connection() | ||||
|         .map_err::<ErrorVec<Error>, _>(|_| vec![Error::ServerInternal].into())?; | ||||
|  | ||||
|     let task = tasks | ||||
|         .find(task_id) | ||||
|         .select(Task::as_select()) | ||||
|         .first(&mut connection) | ||||
|         .optional() | ||||
|         .map_err::<ErrorVec<Error>, _>(|_| vec![Error::ServerInternal].into())?; | ||||
|  | ||||
|     // TODO: Handle not finding the task. | ||||
|     Ok(task.unwrap()) | ||||
| } | ||||
|  | ||||
| #[server] | ||||
| pub(crate) async fn get_tasks_in_category(filtered_category: Category) | ||||
|                                           -> Result<Vec<Task>, ServerFnError<ErrorVec<Error>>> { | ||||
| @@ -78,3 +98,41 @@ pub(crate) async fn edit_task(task_id: i32, new_task: NewTask) | ||||
|  | ||||
|     Ok(updated_task) | ||||
| } | ||||
|  | ||||
| #[server] | ||||
| pub(crate) async fn complete_task(task_id: i32) -> Result<Task, ServerFnError<ErrorVec<Error>>> { | ||||
|     let task = get_task(task_id).await?; | ||||
|     let mut new_task = NewTask::from(task); | ||||
|  | ||||
|     if let Category::Calendar { | ||||
|         reoccurrence: Some(reoccurrence), | ||||
|         date, | ||||
|         .. | ||||
|     } = &mut new_task.category { | ||||
|         match reoccurrence.interval() { | ||||
|             ReoccurrenceInterval::Day => *date = *date + Days::new(reoccurrence.length() as u64), | ||||
|             ReoccurrenceInterval::Month | ReoccurrenceInterval::Year => { | ||||
|                 *date = *date + Months::new( | ||||
|                     reoccurrence.length() * | ||||
|                         if *(reoccurrence.interval()) == ReoccurrenceInterval::Year | ||||
|                         { 12 } else { 1 } | ||||
|                 ); | ||||
|                 *date = NaiveDate::from_ymd_opt( | ||||
|                     date.year(), | ||||
|                     date.month(), | ||||
|                     reoccurrence.start_date().day().min(days_in_year_month( | ||||
|                         date.year(), | ||||
|                         (date.month() as u8).try_into().unwrap(), | ||||
|                     ) as u32), | ||||
|                 ).unwrap() | ||||
|             } | ||||
|         } | ||||
|     } else { | ||||
|         new_task.category = Category::Done; | ||||
|     } | ||||
|  | ||||
|     let updated_task = edit_task(task_id, new_task).await | ||||
|         .map_err::<ErrorVec<Error>, _>(|_| vec![Error::ServerInternal].into())?; | ||||
|  | ||||
|     Ok(updated_task) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user