init feature
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
pub mod family_service;
|
||||
pub mod category_service;
|
||||
pub mod expense_service;
|
||||
pub mod shopping_item_service;
|
||||
|
||||
pub use family_service::FamilyService;
|
||||
pub use category_service::CategoryService;
|
||||
pub use expense_service::ExpenseService;
|
||||
pub use shopping_item_service::ShoppingItemService;
|
||||
|
||||
100
backend/src/services/shopping_item_service.rs
Normal file
100
backend/src/services/shopping_item_service.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
use sea_orm::*;
|
||||
use sea_orm::prelude::Expr;
|
||||
use crate::models::shopping_item::{self, Entity as ShoppingItem, Model as ShoppingItemModel};
|
||||
|
||||
pub struct ShoppingItemService;
|
||||
|
||||
impl ShoppingItemService {
|
||||
pub async fn create(
|
||||
db: &DatabaseConnection,
|
||||
family_id: i32,
|
||||
name: String,
|
||||
) -> Result<ShoppingItemModel, DbErr> {
|
||||
let shopping_item = shopping_item::ActiveModel {
|
||||
family_id: Set(family_id),
|
||||
name: Set(name),
|
||||
is_purchased: Set(false),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
shopping_item.insert(db).await
|
||||
}
|
||||
|
||||
pub async fn find_by_id(
|
||||
db: &DatabaseConnection,
|
||||
id: i32,
|
||||
) -> Result<Option<ShoppingItemModel>, DbErr> {
|
||||
ShoppingItem::find_by_id(id).one(db).await
|
||||
}
|
||||
|
||||
pub async fn find_by_family(
|
||||
db: &DatabaseConnection,
|
||||
family_id: i32,
|
||||
) -> Result<Vec<ShoppingItemModel>, DbErr> {
|
||||
ShoppingItem::find()
|
||||
.filter(shopping_item::Column::FamilyId.eq(family_id))
|
||||
.all(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn update(
|
||||
db: &DatabaseConnection,
|
||||
id: i32,
|
||||
name: String,
|
||||
) -> Result<ShoppingItemModel, DbErr> {
|
||||
let shopping_item = ShoppingItem::find_by_id(id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DbErr::RecordNotFound("Shopping item not found".to_string()))?;
|
||||
|
||||
let mut shopping_item: shopping_item::ActiveModel = shopping_item.into();
|
||||
shopping_item.name = Set(name);
|
||||
|
||||
shopping_item.update(db).await
|
||||
}
|
||||
|
||||
pub async fn delete(db: &DatabaseConnection, id: i32) -> Result<DeleteResult, DbErr> {
|
||||
let shopping_item = ShoppingItem::find_by_id(id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DbErr::RecordNotFound("Shopping item not found".to_string()))?;
|
||||
|
||||
let shopping_item: shopping_item::ActiveModel = shopping_item.into();
|
||||
shopping_item.delete(db).await
|
||||
}
|
||||
|
||||
pub async fn mark_as_purchased(
|
||||
db: &DatabaseConnection,
|
||||
id: i32,
|
||||
is_purchased: bool,
|
||||
) -> Result<ShoppingItemModel, DbErr> {
|
||||
let shopping_item = ShoppingItem::find_by_id(id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(DbErr::RecordNotFound("Shopping item not found".to_string()))?;
|
||||
|
||||
let mut shopping_item: shopping_item::ActiveModel = shopping_item.into();
|
||||
shopping_item.is_purchased = Set(is_purchased);
|
||||
|
||||
shopping_item.update(db).await
|
||||
}
|
||||
|
||||
pub async fn mark_all_as_purchased(
|
||||
db: &DatabaseConnection,
|
||||
family_id: i32,
|
||||
) -> Result<u64, DbErr> {
|
||||
ShoppingItem::update_many()
|
||||
.filter(shopping_item::Column::FamilyId.eq(family_id))
|
||||
.col_expr(shopping_item::Column::IsPurchased, Expr::value(true))
|
||||
.exec(db)
|
||||
.await
|
||||
.map(|result| result.rows_affected)
|
||||
}
|
||||
|
||||
pub async fn clear_all(db: &DatabaseConnection, family_id: i32) -> Result<DeleteResult, DbErr> {
|
||||
ShoppingItem::delete_many()
|
||||
.filter(shopping_item::Column::FamilyId.eq(family_id))
|
||||
.exec(db)
|
||||
.await
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user