Files
todo-baggins/src/server/projects.rs
Matouš Volf a83b376f7b
Some checks failed
actionlint check / actionlint check (pull_request) Successful in 7s
checkov check / checkov check (pull_request) Successful in 53s
conventional commit messages check / conventional commit messages check (pull_request) Successful in 4s
conventional pull request title check / conventional pull request title check (pull_request) Successful in 2s
dotenv-linter check / dotenv-linter check (pull_request) Successful in 6s
GitLeaks check / GitLeaks check (pull_request) Successful in 7s
hadolint check / hadolint check (pull_request) Successful in 9s
htmlhint check / htmlhint check (pull_request) Successful in 9s
markdownlint check / markdownlint check (pull_request) Successful in 9s
Prettier check / Prettier check (pull_request) Successful in 10s
Rust check / Rust check (pull_request) Failing after 7m40s
ShellCheck check / ShellCheck check (pull_request) Successful in 12s
Stylelint check / Stylelint check (pull_request) Successful in 11s
yamllint check / yamllint check (pull_request) Successful in 14s
refactor: import different dependencies only in the correct target binary
2024-12-29 20:50:06 +01:00

101 lines
3.4 KiB
Rust

use crate::errors::error::Error;
use crate::errors::error_vec::ErrorVec;
use crate::errors::project_error::ProjectError;
use crate::models::project::{NewProject, Project};
#[cfg(feature = "server")]
use crate::server::database_connection::establish_database_connection;
#[cfg(feature = "server")]
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl, SelectableHelper};
use dioxus::prelude::*;
#[cfg(feature = "server")]
use validator::Validate;
#[server]
pub(crate) async fn create_project(
new_project: NewProject,
) -> Result<Project, ServerFnError<ErrorVec<ProjectError>>> {
use crate::schema::projects;
// TODO: replace with model sanitization (https://github.com/matous-volf/todo-baggins/issues/13)
let mut new_project = new_project;
new_project.title = new_project.title.trim().to_owned();
new_project
.validate()
.map_err::<ErrorVec<ProjectError>, _>(|errors| errors.into())?;
let mut connection =
establish_database_connection().map_err::<ErrorVec<ProjectError>, _>(|_| {
vec![ProjectError::Error(Error::ServerInternal)].into()
})?;
let new_project = diesel::insert_into(projects::table)
.values(&new_project)
.returning(Project::as_returning())
.get_result(&mut connection)
.map_err::<ErrorVec<ProjectError>, _>(|error| vec![error.into()].into())?;
Ok(new_project)
}
#[server]
pub(crate) async fn get_projects() -> Result<Vec<Project>, ServerFnError<ErrorVec<Error>>> {
use crate::schema::projects::dsl::*;
let mut connection = establish_database_connection()
.map_err::<ErrorVec<Error>, _>(|_| vec![Error::ServerInternal].into())?;
let results = projects
.select(Project::as_select())
.load::<Project>(&mut connection)
.map_err::<ErrorVec<Error>, _>(|_| vec![Error::ServerInternal].into())?;
Ok(results)
}
#[server]
pub(crate) async fn edit_project(
project_id: i32,
new_project: NewProject,
) -> Result<Project, ServerFnError<ErrorVec<ProjectError>>> {
use crate::schema::projects::dsl::*;
// TODO: replace with model sanitization (https://github.com/matous-volf/todo-baggins/issues/13)
let mut new_project = new_project;
new_project.title = new_project.title.trim().to_owned();
new_project
.validate()
.map_err::<ErrorVec<ProjectError>, _>(|errors| errors.into())?;
let mut connection =
establish_database_connection().map_err::<ErrorVec<ProjectError>, _>(|_| {
vec![ProjectError::Error(Error::ServerInternal)].into()
})?;
let updated_project = diesel::update(projects)
.filter(id.eq(project_id))
.set(title.eq(new_project.title))
.returning(Project::as_returning())
.get_result(&mut connection)
.map_err::<ErrorVec<ProjectError>, _>(|error| vec![error.into()].into())?;
Ok(updated_project)
}
// TODO: Get rid of this suppression.
//noinspection DuplicatedCode
#[server]
pub(crate) async fn delete_project(project_id: i32) -> Result<(), ServerFnError<ErrorVec<Error>>> {
use crate::schema::projects::dsl::*;
let mut connection = establish_database_connection()
.map_err::<ErrorVec<Error>, _>(|_| vec![Error::ServerInternal].into())?;
diesel::delete(projects.filter(id.eq(project_id)))
.execute(&mut connection)
.map_err::<ErrorVec<Error>, _>(|error| vec![error.into()].into())?;
Ok(())
}