finalize db refactor
This commit is contained in:
338
rust/src/main.rs
338
rust/src/main.rs
@@ -710,26 +710,16 @@ async fn trip_type_remove(
|
||||
State(state): State<AppState>,
|
||||
Path((trip_id, type_id)): Path<(Uuid, Uuid)>,
|
||||
) -> Result<Redirect, (StatusCode, Markup)> {
|
||||
let trip_id = trip_id.to_string();
|
||||
let type_id = type_id.to_string();
|
||||
let results = query!(
|
||||
"DELETE FROM trips_to_trips_types AS ttt
|
||||
WHERE ttt.trip_id = ?
|
||||
AND ttt.trip_type_id = ?
|
||||
",
|
||||
trip_id,
|
||||
type_id
|
||||
)
|
||||
.execute(&state.database_pool)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
(
|
||||
StatusCode::BAD_REQUEST,
|
||||
components::ErrorPage::build(&e.to_string()),
|
||||
)
|
||||
})?;
|
||||
let found = models::Trip::trip_type_remove(&state.database_pool, trip_id, type_id)
|
||||
.map_err(|error| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
components::ErrorPage::build(&error.to_string()),
|
||||
)
|
||||
})
|
||||
.await?;
|
||||
|
||||
if results.rows_affected() == 0 {
|
||||
if !found {
|
||||
Err((
|
||||
StatusCode::NOT_FOUND,
|
||||
components::ErrorPage::build(&format!(
|
||||
@@ -745,66 +735,22 @@ async fn trip_type_add(
|
||||
State(state): State<AppState>,
|
||||
Path((trip_id, type_id)): Path<(Uuid, Uuid)>,
|
||||
) -> Result<Redirect, (StatusCode, Markup)> {
|
||||
let trip_id = trip_id.to_string();
|
||||
let type_id = type_id.to_string();
|
||||
query!(
|
||||
"INSERT INTO trips_to_trips_types
|
||||
(trip_id, trip_type_id) VALUES (?, ?)",
|
||||
trip_id,
|
||||
type_id
|
||||
)
|
||||
.execute(&state.database_pool)
|
||||
.await
|
||||
.map_err(|e| match e {
|
||||
sqlx::Error::Database(ref error) => {
|
||||
let sqlite_error = error.downcast_ref::<SqliteError>();
|
||||
if let Some(code) = sqlite_error.code() {
|
||||
match &*code {
|
||||
"787" => {
|
||||
// SQLITE_CONSTRAINT_FOREIGNKEY
|
||||
(
|
||||
StatusCode::BAD_REQUEST,
|
||||
// TODO: this is not perfect, as both foreign keys
|
||||
// may be responsible for the error. how can we tell
|
||||
// which one?
|
||||
components::ErrorPage::build(&format!(
|
||||
"invalid id: {}",
|
||||
code.to_string()
|
||||
)),
|
||||
)
|
||||
}
|
||||
"2067" => {
|
||||
// SQLITE_CONSTRAINT_UNIQUE
|
||||
(
|
||||
StatusCode::BAD_REQUEST,
|
||||
components::ErrorPage::build(&format!(
|
||||
"type {type_id} is already active for trip {trip_id}"
|
||||
)),
|
||||
)
|
||||
}
|
||||
_ => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
components::ErrorPage::build(&format!(
|
||||
"got error with unknown code: {}",
|
||||
sqlite_error.to_string()
|
||||
)),
|
||||
),
|
||||
}
|
||||
} else {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
components::ErrorPage::build(&format!(
|
||||
"got error without code: {}",
|
||||
sqlite_error.to_string()
|
||||
)),
|
||||
)
|
||||
}
|
||||
}
|
||||
_ => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
components::ErrorPage::build(&format!("got unknown error: {}", e.to_string())),
|
||||
),
|
||||
})?;
|
||||
models::Trip::trip_type_add(&state.database_pool, trip_id, type_id)
|
||||
.map_err(|error| match error {
|
||||
models::Error::ReferenceNotFound { description } => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
components::ErrorPage::build(&description),
|
||||
),
|
||||
models::Error::Duplicate { description } => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
components::ErrorPage::build(&description),
|
||||
),
|
||||
_ => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
components::ErrorPage::build(&error.to_string()),
|
||||
),
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(Redirect::to(&format!("/trips/{trip_id}/")))
|
||||
}
|
||||
@@ -820,24 +766,17 @@ async fn trip_comment_set(
|
||||
Path(trip_id): Path<Uuid>,
|
||||
Form(comment_update): Form<CommentUpdate>,
|
||||
) -> Result<Redirect, (StatusCode, Markup)> {
|
||||
let trip_id = trip_id.to_string();
|
||||
let result = query!(
|
||||
"UPDATE trips
|
||||
SET comment = ?
|
||||
WHERE id = ?",
|
||||
comment_update.new_comment,
|
||||
trip_id,
|
||||
)
|
||||
.execute(&state.database_pool)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
(
|
||||
StatusCode::BAD_REQUEST,
|
||||
components::ErrorPage::build(&e.to_string()),
|
||||
)
|
||||
})?;
|
||||
let found =
|
||||
models::Trip::set_comment(&state.database_pool, trip_id, &comment_update.new_comment)
|
||||
.map_err(|error| {
|
||||
(
|
||||
StatusCode::BAD_REQUEST,
|
||||
components::ErrorPage::build(&error.to_string()),
|
||||
)
|
||||
})
|
||||
.await?;
|
||||
|
||||
if result.rows_affected() == 0 {
|
||||
if !found {
|
||||
Err((
|
||||
StatusCode::NOT_FOUND,
|
||||
components::ErrorPage::build(&format!("trip with id {id} not found", id = trip_id)),
|
||||
@@ -866,31 +805,25 @@ async fn trip_edit_attribute(
|
||||
));
|
||||
}
|
||||
}
|
||||
let result = query(&format!(
|
||||
"UPDATE trips
|
||||
SET {attribute} = ?
|
||||
WHERE id = ?",
|
||||
attribute = to_variant_name(&attribute).unwrap()
|
||||
))
|
||||
.bind(trip_update.new_value)
|
||||
.bind(trip_id.to_string())
|
||||
.execute(&state.database_pool)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
(
|
||||
StatusCode::BAD_REQUEST,
|
||||
components::ErrorPage::build(&e.to_string()),
|
||||
)
|
||||
})?;
|
||||
|
||||
if result.rows_affected() == 0 {
|
||||
Err((
|
||||
models::Trip::set_attribute(
|
||||
&state.database_pool,
|
||||
trip_id,
|
||||
attribute,
|
||||
&trip_update.new_value,
|
||||
)
|
||||
.map_err(|error| match error {
|
||||
models::Error::NotFound { description } => (
|
||||
StatusCode::NOT_FOUND,
|
||||
components::ErrorPage::build(&format!("trip with id {id} not found", id = trip_id)),
|
||||
))
|
||||
} else {
|
||||
Ok(Redirect::to(&format!("/trips/{trip_id}/")))
|
||||
}
|
||||
components::ErrorPage::build(&description),
|
||||
),
|
||||
_ => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
components::ErrorPage::build(&error.to_string()),
|
||||
),
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(Redirect::to(&format!("/trips/{trip_id}/")))
|
||||
}
|
||||
|
||||
async fn trip_item_set_state(
|
||||
@@ -900,35 +833,20 @@ async fn trip_item_set_state(
|
||||
key: models::TripItemStateKey,
|
||||
value: bool,
|
||||
) -> Result<(), (StatusCode, Markup)> {
|
||||
let result = query(&format!(
|
||||
"UPDATE trips_items
|
||||
SET {key} = ?
|
||||
WHERE trip_id = ?
|
||||
AND item_id = ?",
|
||||
key = to_variant_name(&key).unwrap()
|
||||
))
|
||||
.bind(value)
|
||||
.bind(trip_id.to_string())
|
||||
.bind(item_id.to_string())
|
||||
.execute(&state.database_pool)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
(
|
||||
StatusCode::BAD_REQUEST,
|
||||
components::ErrorPage::build(&e.to_string()),
|
||||
)
|
||||
})?;
|
||||
models::TripItem::set_state(&state.database_pool, trip_id, item_id, key, value)
|
||||
.map_err(|error| match error {
|
||||
models::Error::NotFound { description } => (
|
||||
StatusCode::NOT_FOUND,
|
||||
components::ErrorPage::build(&description),
|
||||
),
|
||||
_ => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
components::ErrorPage::build(&error.to_string()),
|
||||
),
|
||||
})
|
||||
.await?;
|
||||
|
||||
if result.rows_affected() == 0 {
|
||||
Err((
|
||||
StatusCode::NOT_FOUND,
|
||||
components::ErrorPage::build(&format!(
|
||||
"trip with id {trip_id} or item with id {item_id} not found"
|
||||
)),
|
||||
))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn trip_row(
|
||||
@@ -1267,56 +1185,18 @@ async fn inventory_category_create(
|
||||
));
|
||||
}
|
||||
|
||||
let id = Uuid::new_v4();
|
||||
let id_param = id.to_string();
|
||||
query!(
|
||||
"INSERT INTO inventory_items_categories
|
||||
(id, name)
|
||||
VALUES
|
||||
(?, ?)",
|
||||
id_param,
|
||||
new_category.name
|
||||
)
|
||||
.execute(&state.database_pool)
|
||||
.map_err(|e| match e {
|
||||
sqlx::Error::Database(ref error) => {
|
||||
let sqlite_error = error.downcast_ref::<SqliteError>();
|
||||
if let Some(code) = sqlite_error.code() {
|
||||
match &*code {
|
||||
"2067" => {
|
||||
// SQLITE_CONSTRAINT_UNIQUE
|
||||
(
|
||||
StatusCode::BAD_REQUEST,
|
||||
components::ErrorPage::build(&format!(
|
||||
"category with name \"{name}\" already exists",
|
||||
name = new_category.name
|
||||
)),
|
||||
)
|
||||
}
|
||||
_ => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
components::ErrorPage::build(&format!(
|
||||
"got error with unknown code: {}",
|
||||
sqlite_error.to_string()
|
||||
)),
|
||||
),
|
||||
}
|
||||
} else {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
components::ErrorPage::build(&format!(
|
||||
"got error without code: {}",
|
||||
sqlite_error.to_string()
|
||||
)),
|
||||
)
|
||||
}
|
||||
}
|
||||
_ => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
components::ErrorPage::build(&format!("got unknown error: {}", e.to_string())),
|
||||
),
|
||||
})
|
||||
.await?;
|
||||
let _new_id = models::Category::save(&state.database_pool, &new_category.name)
|
||||
.map_err(|error| match error {
|
||||
models::Error::Duplicate { description } => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
components::ErrorPage::build(&description),
|
||||
),
|
||||
_ => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
components::ErrorPage::build(&error.to_string()),
|
||||
),
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(Redirect::to("/inventory/"))
|
||||
}
|
||||
@@ -1398,58 +1278,26 @@ struct NewTripType {
|
||||
async fn trip_type_create(
|
||||
State(state): State<AppState>,
|
||||
Form(new_trip_type): Form<NewTripType>,
|
||||
) -> Result<Redirect, (StatusCode, String)> {
|
||||
) -> Result<Redirect, (StatusCode, Markup)> {
|
||||
if new_trip_type.name.is_empty() {
|
||||
return Err((
|
||||
StatusCode::UNPROCESSABLE_ENTITY,
|
||||
"name cannot be empty".to_string(),
|
||||
components::ErrorPage::build("name cannot be empty"),
|
||||
));
|
||||
}
|
||||
|
||||
let id = Uuid::new_v4();
|
||||
let id_param = id.to_string();
|
||||
query!(
|
||||
"INSERT INTO trips_types
|
||||
(id, name)
|
||||
VALUES
|
||||
(?, ?)",
|
||||
id_param,
|
||||
new_trip_type.name,
|
||||
)
|
||||
.execute(&state.database_pool)
|
||||
.await
|
||||
.map_err(|e| match e {
|
||||
sqlx::Error::Database(ref error) => {
|
||||
let sqlite_error = error.downcast_ref::<SqliteError>();
|
||||
if let Some(code) = sqlite_error.code() {
|
||||
match &*code {
|
||||
"2067" => {
|
||||
// SQLITE_CONSTRAINT_UNIQUE
|
||||
(
|
||||
StatusCode::BAD_REQUEST,
|
||||
format!(
|
||||
"trip type with name \"{name}\" already exists",
|
||||
name = new_trip_type.name,
|
||||
),
|
||||
)
|
||||
}
|
||||
_ => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("got error with unknown code: {}", sqlite_error.to_string()),
|
||||
),
|
||||
}
|
||||
} else {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("got error without code: {}", sqlite_error.to_string()),
|
||||
)
|
||||
}
|
||||
}
|
||||
_ => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("got unknown error: {}", e.to_string()),
|
||||
),
|
||||
})?;
|
||||
let _new_id = models::TripsType::save(&state.database_pool, &new_trip_type.name)
|
||||
.map_err(|error| match error {
|
||||
models::Error::Duplicate { description } => (
|
||||
StatusCode::BAD_REQUEST,
|
||||
components::ErrorPage::build(&description),
|
||||
),
|
||||
_ => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
components::ErrorPage::build(&error.to_string()),
|
||||
),
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(Redirect::to("/trips/types/"))
|
||||
}
|
||||
@@ -1644,7 +1492,7 @@ async fn trip_packagelist(
|
||||
StatusCode::OK,
|
||||
components::Root::build(
|
||||
&components::trip::packagelist::TripPackageList::build(&trip),
|
||||
&components::TopLevelPage::None,
|
||||
&components::TopLevelPage::Trips,
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_variant::to_variant_name;
|
||||
use sqlx::{
|
||||
database::Database,
|
||||
database::HasValueRef,
|
||||
@@ -28,6 +29,9 @@ pub enum Error {
|
||||
Int { description: String },
|
||||
Constraint { description: String },
|
||||
TimeParse { description: String },
|
||||
Duplicate { description: String },
|
||||
NotFound { description: String },
|
||||
ReferenceNotFound { description: String },
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
@@ -51,6 +55,15 @@ impl fmt::Display for Error {
|
||||
Self::Constraint { description } => {
|
||||
write!(f, "SQL constraint error: {description}")
|
||||
}
|
||||
Self::Duplicate { description } => {
|
||||
write!(f, "Duplicate data entry: {description}")
|
||||
}
|
||||
Self::NotFound { description } => {
|
||||
write!(f, "not found: {description}")
|
||||
}
|
||||
Self::ReferenceNotFound { description } => {
|
||||
write!(f, "SQL foreign key reference was not found: {description}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -395,6 +408,36 @@ impl TripItem {
|
||||
Ok(v) => Ok(Some(v?)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn set_state(
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
trip_id: Uuid,
|
||||
item_id: Uuid,
|
||||
key: TripItemStateKey,
|
||||
value: bool,
|
||||
) -> Result<(), Error> {
|
||||
let result = sqlx::query(&format!(
|
||||
"UPDATE trips_items
|
||||
SET {key} = ?
|
||||
WHERE trip_id = ?
|
||||
AND item_id = ?",
|
||||
key = to_variant_name(&key).unwrap()
|
||||
))
|
||||
.bind(value)
|
||||
.bind(trip_id.to_string())
|
||||
.bind(item_id.to_string())
|
||||
.execute(pool)
|
||||
.map_err(|error| Error::Sql {
|
||||
description: error.to_string(),
|
||||
})
|
||||
.await?;
|
||||
|
||||
(result.rows_affected() != 0)
|
||||
.then_some(())
|
||||
.ok_or_else(|| Error::NotFound {
|
||||
description: format!("item {item_id} not found for trip {trip_id}"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
struct DbTripRow {
|
||||
@@ -463,7 +506,7 @@ struct DbTripWeightRow {
|
||||
pub total_weight: Option<i32>,
|
||||
}
|
||||
|
||||
impl<'a> Trip {
|
||||
impl Trip {
|
||||
pub async fn all(pool: &sqlx::Pool<sqlx::Sqlite>) -> Result<Vec<Trip>, Error> {
|
||||
sqlx::query_as!(
|
||||
DbTripRow,
|
||||
@@ -521,6 +564,89 @@ impl<'a> Trip {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn trip_type_remove(
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
id: Uuid,
|
||||
type_id: Uuid,
|
||||
) -> Result<bool, Error> {
|
||||
let id_param = id.to_string();
|
||||
let type_id_param = type_id.to_string();
|
||||
|
||||
let results = sqlx::query!(
|
||||
"DELETE FROM trips_to_trips_types AS ttt
|
||||
WHERE ttt.trip_id = ?
|
||||
AND ttt.trip_type_id = ?
|
||||
",
|
||||
id_param,
|
||||
type_id_param,
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
Ok(results.rows_affected() != 0)
|
||||
}
|
||||
|
||||
pub async fn trip_type_add(
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
id: Uuid,
|
||||
type_id: Uuid,
|
||||
) -> Result<(), Error> {
|
||||
let trip_id_param = id.to_string();
|
||||
let type_id_param = type_id.to_string();
|
||||
sqlx::query!(
|
||||
"INSERT INTO trips_to_trips_types
|
||||
(trip_id, trip_type_id)
|
||||
VALUES
|
||||
(?, ?)
|
||||
",
|
||||
trip_id_param,
|
||||
type_id_param,
|
||||
)
|
||||
.execute(pool)
|
||||
.map_err(|error| match error {
|
||||
sqlx::Error::Database(ref error) => {
|
||||
let sqlite_error = error.downcast_ref::<sqlx::sqlite::SqliteError>();
|
||||
if let Some(code) = sqlite_error.code() {
|
||||
match &*code {
|
||||
"787" => {
|
||||
// SQLITE_CONSTRAINT_FOREIGNKEY
|
||||
Error::ReferenceNotFound {
|
||||
description: format!("invalid id: {}", code.to_string()),
|
||||
}
|
||||
}
|
||||
"2067" => {
|
||||
// SQLITE_CONSTRAINT_UNIQUE
|
||||
Error::Duplicate {
|
||||
description: format!(
|
||||
"type {type_id} is already active for trip {id}"
|
||||
),
|
||||
}
|
||||
}
|
||||
_ => Error::Sql {
|
||||
description: format!(
|
||||
"got error with unknown code: {}",
|
||||
sqlite_error.to_string()
|
||||
),
|
||||
},
|
||||
}
|
||||
} else {
|
||||
Error::Sql {
|
||||
description: format!(
|
||||
"got error without code: {}",
|
||||
sqlite_error.to_string()
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => Error::Sql {
|
||||
description: format!("got unknown error: {}", error.to_string()),
|
||||
},
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn set_state(
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
id: Uuid,
|
||||
@@ -540,6 +666,52 @@ impl<'a> Trip {
|
||||
Ok(result.rows_affected() != 0)
|
||||
}
|
||||
|
||||
pub async fn set_comment(
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
id: Uuid,
|
||||
new_comment: &str,
|
||||
) -> Result<bool, Error> {
|
||||
let trip_id_param = id.to_string();
|
||||
let result = sqlx::query!(
|
||||
"UPDATE trips
|
||||
SET comment = ?
|
||||
WHERE id = ?",
|
||||
new_comment,
|
||||
trip_id_param,
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
Ok(result.rows_affected() != 0)
|
||||
}
|
||||
|
||||
pub async fn set_attribute(
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
trip_id: Uuid,
|
||||
attribute: TripAttribute,
|
||||
value: &str,
|
||||
) -> Result<(), Error> {
|
||||
let result = sqlx::query(&format!(
|
||||
"UPDATE trips
|
||||
SET {attribute} = ?
|
||||
WHERE id = ?",
|
||||
attribute = to_variant_name(&attribute).unwrap()
|
||||
))
|
||||
.bind(value)
|
||||
.bind(trip_id.to_string())
|
||||
.execute(pool)
|
||||
.map_err(|error| Error::Sql {
|
||||
description: error.to_string(),
|
||||
})
|
||||
.await?;
|
||||
|
||||
(result.rows_affected() != 0)
|
||||
.then_some(())
|
||||
.ok_or_else(|| Error::NotFound {
|
||||
description: format!("trip {trip_id} not found"),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn save(
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
name: &str,
|
||||
@@ -645,13 +817,13 @@ impl<'a> Trip {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn types(&'a self) -> &Vec<TripType> {
|
||||
pub fn types(&self) -> &Vec<TripType> {
|
||||
self.types
|
||||
.as_ref()
|
||||
.expect("you need to call load_trips_types()")
|
||||
}
|
||||
|
||||
pub fn categories(&'a self) -> &Vec<TripCategory> {
|
||||
pub fn categories(&self) -> &Vec<TripCategory> {
|
||||
self.categories
|
||||
.as_ref()
|
||||
.expect("you need to call load_trips_types()")
|
||||
@@ -672,10 +844,7 @@ impl<'a> Trip {
|
||||
.sum::<i64>()
|
||||
}
|
||||
|
||||
pub async fn load_trips_types(
|
||||
&'a mut self,
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
) -> Result<(), Error> {
|
||||
pub async fn load_trips_types(&mut self, pool: &sqlx::Pool<sqlx::Sqlite>) -> Result<(), Error> {
|
||||
let id = self.id.to_string();
|
||||
let types = sqlx::query!(
|
||||
"
|
||||
@@ -719,7 +888,7 @@ impl<'a> Trip {
|
||||
}
|
||||
|
||||
pub async fn sync_trip_items_with_inventory(
|
||||
&'a mut self,
|
||||
&mut self,
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
) -> Result<(), Error> {
|
||||
// we need to get all items that are part of the inventory but not
|
||||
@@ -791,10 +960,7 @@ impl<'a> Trip {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn load_categories(
|
||||
&'a mut self,
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
) -> Result<(), Error> {
|
||||
pub async fn load_categories(&mut self, pool: &sqlx::Pool<sqlx::Sqlite>) -> Result<(), Error> {
|
||||
let mut categories: Vec<TripCategory> = vec![];
|
||||
// we can ignore the return type as we collect into `categories`
|
||||
// in the `map_ok()` closure
|
||||
@@ -922,6 +1088,56 @@ impl TripsType {
|
||||
.collect::<Result<Vec<Self>, Error>>()
|
||||
}
|
||||
|
||||
pub async fn save(pool: &sqlx::Pool<sqlx::Sqlite>, name: &str) -> Result<Uuid, Error> {
|
||||
let id = Uuid::new_v4();
|
||||
let id_param = id.to_string();
|
||||
sqlx::query!(
|
||||
"INSERT INTO trips_types
|
||||
(id, name)
|
||||
VALUES
|
||||
(?, ?)",
|
||||
id_param,
|
||||
name,
|
||||
)
|
||||
.execute(pool)
|
||||
.map_err(|error| match error {
|
||||
sqlx::Error::Database(ref error) => {
|
||||
let sqlite_error = error.downcast_ref::<sqlx::sqlite::SqliteError>();
|
||||
if let Some(code) = sqlite_error.code() {
|
||||
match &*code {
|
||||
"2067" => {
|
||||
// SQLITE_CONSTRAINT_UNIQUE
|
||||
Error::Duplicate {
|
||||
description: format!(
|
||||
"trip type with name \"{name}\" already exists"
|
||||
),
|
||||
}
|
||||
}
|
||||
_ => Error::Sql {
|
||||
description: format!(
|
||||
"got error with unknown code: {}",
|
||||
sqlite_error.to_string()
|
||||
),
|
||||
},
|
||||
}
|
||||
} else {
|
||||
Error::Sql {
|
||||
description: format!(
|
||||
"got error without code: {}",
|
||||
sqlite_error.to_string()
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => Error::Sql {
|
||||
description: format!("got unknown error: {}", error.to_string()),
|
||||
},
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub async fn set_name(
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
id: Uuid,
|
||||
@@ -978,7 +1194,7 @@ struct DbInventoryItemsRow {
|
||||
category_id: String,
|
||||
}
|
||||
|
||||
impl<'a> Category {
|
||||
impl Category {
|
||||
pub async fn _find(
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
id: Uuid,
|
||||
@@ -1007,7 +1223,57 @@ impl<'a> Category {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn items(&'a self) -> &'a Vec<Item> {
|
||||
pub async fn save(pool: &sqlx::Pool<sqlx::Sqlite>, name: &str) -> Result<Uuid, Error> {
|
||||
let id = Uuid::new_v4();
|
||||
let id_param = id.to_string();
|
||||
sqlx::query!(
|
||||
"INSERT INTO inventory_items_categories
|
||||
(id, name)
|
||||
VALUES
|
||||
(?, ?)",
|
||||
id_param,
|
||||
name,
|
||||
)
|
||||
.execute(pool)
|
||||
.map_err(|error| match error {
|
||||
sqlx::Error::Database(ref error) => {
|
||||
let sqlite_error = error.downcast_ref::<sqlx::sqlite::SqliteError>();
|
||||
if let Some(code) = sqlite_error.code() {
|
||||
match &*code {
|
||||
"2067" => {
|
||||
// SQLITE_CONSTRAINT_UNIQUE
|
||||
Error::Duplicate {
|
||||
description: format!(
|
||||
"inventory item category with name \"{name}\" already exists"
|
||||
),
|
||||
}
|
||||
}
|
||||
_ => Error::Sql {
|
||||
description: format!(
|
||||
"got error with unknown code: {}",
|
||||
sqlite_error.to_string()
|
||||
),
|
||||
},
|
||||
}
|
||||
} else {
|
||||
Error::Sql {
|
||||
description: format!(
|
||||
"got error without code: {}",
|
||||
sqlite_error.to_string()
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => Error::Sql {
|
||||
description: format!("got unknown error: {}", error.to_string()),
|
||||
},
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
pub fn items(&self) -> &Vec<Item> {
|
||||
self.items
|
||||
.as_ref()
|
||||
.expect("you need to call populate_items()")
|
||||
@@ -1017,10 +1283,7 @@ impl<'a> Category {
|
||||
self.items().iter().map(|item| item.weight).sum()
|
||||
}
|
||||
|
||||
pub async fn populate_items(
|
||||
&'a mut self,
|
||||
pool: &sqlx::Pool<sqlx::Sqlite>,
|
||||
) -> Result<(), Error> {
|
||||
pub async fn populate_items(&mut self, pool: &sqlx::Pool<sqlx::Sqlite>) -> Result<(), Error> {
|
||||
let id = self.id.to_string();
|
||||
let items = sqlx::query_as!(
|
||||
DbInventoryItemsRow,
|
||||
|
||||
Reference in New Issue
Block a user