meal categories
This commit is contained in:
+48
-6
@@ -1,10 +1,12 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::domain::{DomainError, DomainResult, GroceryList, Item, Meal, SessionUser, User};
|
||||
use crate::domain::{
|
||||
DomainError, DomainResult, GroceryList, Item, Meal, MealCategory, SessionUser, User,
|
||||
};
|
||||
use crate::ports::{
|
||||
CategoryRepository, InvitationRepository, ItemRepository, ListRepository,
|
||||
MealIngredientRepository, MealRepository, NewItem, PasswordHasher, RealtimeNotifier,
|
||||
SessionRepository, TokenGenerator, UserRepository,
|
||||
MealCategoryRepository, MealIngredientRepository, MealRepository, NewItem, PasswordHasher,
|
||||
RealtimeNotifier, SessionRepository, TokenGenerator, UserRepository,
|
||||
};
|
||||
use crate::sqlite::SqliteDatabase;
|
||||
|
||||
@@ -313,6 +315,7 @@ pub struct MealService {
|
||||
db: SqliteDatabase,
|
||||
meals: Arc<dyn MealRepository>,
|
||||
ingredients: Arc<dyn MealIngredientRepository>,
|
||||
meal_categories: Arc<dyn MealCategoryRepository>,
|
||||
lists: Arc<dyn ListRepository>,
|
||||
items: Arc<dyn ItemRepository>,
|
||||
realtime: Arc<dyn RealtimeNotifier>,
|
||||
@@ -323,6 +326,7 @@ impl MealService {
|
||||
db: SqliteDatabase,
|
||||
meals: Arc<dyn MealRepository>,
|
||||
ingredients: Arc<dyn MealIngredientRepository>,
|
||||
meal_categories: Arc<dyn MealCategoryRepository>,
|
||||
lists: Arc<dyn ListRepository>,
|
||||
items: Arc<dyn ItemRepository>,
|
||||
realtime: Arc<dyn RealtimeNotifier>,
|
||||
@@ -331,17 +335,25 @@ impl MealService {
|
||||
db,
|
||||
meals,
|
||||
ingredients,
|
||||
meal_categories,
|
||||
lists,
|
||||
items,
|
||||
realtime,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_meal(&self, name: String, description: String) -> DomainResult<Meal> {
|
||||
pub async fn create_meal(
|
||||
&self,
|
||||
name: String,
|
||||
description: String,
|
||||
category_id: Option<i64>,
|
||||
) -> DomainResult<Meal> {
|
||||
let meals = Arc::clone(&self.meals);
|
||||
self.db
|
||||
.run(move |txn| {
|
||||
Box::pin(async move { meals.create_meal(txn, name, description).await })
|
||||
Box::pin(async move {
|
||||
meals.create_meal(txn, name, description, category_id).await
|
||||
})
|
||||
})
|
||||
.await
|
||||
}
|
||||
@@ -365,11 +377,41 @@ impl MealService {
|
||||
meal_id: i64,
|
||||
name: String,
|
||||
description: String,
|
||||
category_id: Option<i64>,
|
||||
) -> DomainResult<()> {
|
||||
let meals = Arc::clone(&self.meals);
|
||||
self.db
|
||||
.run(move |txn| {
|
||||
Box::pin(async move { meals.update_meal(txn, meal_id, name, description).await })
|
||||
Box::pin(async move {
|
||||
meals
|
||||
.update_meal(txn, meal_id, name, description, category_id)
|
||||
.await
|
||||
})
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn list_meal_categories(&self) -> DomainResult<Vec<MealCategory>> {
|
||||
let meal_categories = Arc::clone(&self.meal_categories);
|
||||
self.db
|
||||
.run(move |txn| Box::pin(async move { meal_categories.meal_categories(txn).await }))
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn create_meal_category(&self, name: String) -> DomainResult<i64> {
|
||||
let meal_categories = Arc::clone(&self.meal_categories);
|
||||
self.db
|
||||
.run(move |txn| {
|
||||
Box::pin(async move { meal_categories.create_meal_category(txn, name).await })
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_meal_category(&self, category_id: i64) -> DomainResult<()> {
|
||||
let meal_categories = Arc::clone(&self.meal_categories);
|
||||
self.db
|
||||
.run(move |txn| {
|
||||
Box::pin(async move { meal_categories.delete_meal_category(txn, category_id).await })
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user