Skip to content

Commit

Permalink
fix: Modification of newly created ingredient not saved in shopping l…
Browse files Browse the repository at this point in the history
…ist (#402)
  • Loading branch information
TomBursch authored Feb 21, 2024
1 parent 83cf559 commit 71d5b9d
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 16 deletions.
24 changes: 23 additions & 1 deletion backend/app/controller/item/item_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import app.util.description_splitter as description_splitter
from flask_jwt_extended import jwt_required
from app.models import Item, RecipeItems, Recipe, Category
from .schemas import SearchByNameRequest, UpdateItem
from .schemas import SearchByNameRequest, UpdateItem, AddItem

item = Blueprint("item", __name__)
itemHousehold = Blueprint("item", __name__)
Expand Down Expand Up @@ -69,6 +69,28 @@ def searchItemByName(args, household_id):
]
)

@itemHousehold.route("", methods=["POST"])
@jwt_required()
@authorize_household()
@validate_args(AddItem)
def addItem(args, household_id):
name: str = args["name"].strip()
if Item.find_by_name(household_id, name):
raise InvalidUsage()

item = Item(household_id=household_id, name=name)
if "category" in args:
if not args["category"]:
item.category = None
elif "id" in args["category"]:
item.category = Category.find_by_id(args["category"]["id"])
else:
raise InvalidUsage()
if "icon" in args:
item.icon = args["icon"]
item.save()

return jsonify(item.obj_to_dict())

@item.route("/<int:id>", methods=["POST"])
@jwt_required()
Expand Down
23 changes: 23 additions & 0 deletions backend/app/controller/item/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,26 @@ class Meta:
validate=lambda a: a > 0,
allow_none=True,
)


class AddItem(Schema):
class Meta:
unknown = EXCLUDE

class Category(Schema):
class Meta:
unknown = EXCLUDE

id = fields.Integer(required=True, validate=lambda a: a > 0)
name = fields.String(validate=lambda a: not a or a and not a.isspace())

category = fields.Nested(Category(), allow_none=True)
icon = fields.String(
validate=lambda a: not a or not a.isspace(),
allow_none=True,
)
name = fields.String(
validate=lambda a: not a or not a.isspace(),
allow_none=False,
required=True,
)
18 changes: 13 additions & 5 deletions kitchenowl/lib/cubits/item_edit_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,19 @@ class ItemEditCubit<T extends Item> extends Cubit<ItemEditState> {
description: state.description,
));
}
if (_item.id != null && state.hasChangedItem(_item)) {
await TransactionHandler.getInstance()
.runTransaction(TransactionItemUpdate(
item: item,
));
if (state.hasChangedItem(_item)) {
if (item.id != null) {
await TransactionHandler.getInstance()
.runTransaction(TransactionItemUpdate(
item: item,
));
} else if (household != null) {
await TransactionHandler.getInstance()
.runTransaction(TransactionItemAdd(
household: household!,
item: item,
));
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion kitchenowl/lib/pages/household_page/shoppinglist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class _ShoppinglistPageState extends State<ShoppinglistPage> {
items: state.result,
categories: state.categories,
shoppingList: state.selectedShoppinglist,
onRefresh: () => cubit.refresh(query: ""),
onRefresh: () => cubit.refresh(),
selected: (item) =>
item is ShoppinglistItem &&
(App.settings.shoppingListTapToRemove ||
Expand Down
2 changes: 1 addition & 1 deletion kitchenowl/lib/pages/item_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class _ItemPageState<T extends Item> extends State<ItemPage<T>> {
builder: (context, state) => Text(state.name),
),
actions: [
if (!App.isOffline && cubit.item.id != null)
if (!App.isOffline)
ItemPopupMenuButton(
item: cubit.item,
household: context.read<HouseholdCubit>().state.household,
Expand Down
7 changes: 7 additions & 0 deletions kitchenowl/lib/services/api/item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ extension ItemApi on ApiService {
return res.statusCode == 200;
}

Future<bool> addItem(Household household, Item item) async {
final res = await post('${householdPath(household)}$baseRoute',
jsonEncode(item.toJsonWithId()));

return res.statusCode == 200;
}

Future<bool> mergeItems(Item item, Item other) async {
final res = await post(
'$baseRoute/${item.id}',
Expand Down
21 changes: 21 additions & 0 deletions kitchenowl/lib/services/transactions/item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ class TransactionItemUpdate extends Transaction<bool> {
}
}

class TransactionItemAdd extends Transaction<bool> {
final Household household;
final Item item;

TransactionItemAdd({
required this.household,
required this.item,
DateTime? timestamp,
}) : super.internal(timestamp ?? DateTime.now(), "TransactionItemAdd");

@override
Future<bool> runLocal() async {
return false;
}

@override
Future<bool?> runOnline() async {
return await ApiService.getInstance().addItem(household, item);
}
}

class TransactionItemGetRecipes extends Transaction<List<Recipe>> {
final Household? household;
final Item item;
Expand Down
17 changes: 9 additions & 8 deletions kitchenowl/lib/widgets/item_popup_menu_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,23 @@ class ItemPopupMenuButton extends StatelessWidget {
Widget build(BuildContext context) {
return PopupMenuButton(
itemBuilder: (BuildContext context) => <PopupMenuEntry<_ItemAction>>[
PopupMenuItem<_ItemAction>(
value: _ItemAction.changeIcon,
child: Text(AppLocalizations.of(context)!.changeIcon),
),
if (item is! RecipeItem)
if (household != null)
PopupMenuItem<_ItemAction>(
value: _ItemAction.changeIcon,
child: Text(AppLocalizations.of(context)!.changeIcon),
),
if (item is! RecipeItem && item.id != null)
PopupMenuItem<_ItemAction>(
value: _ItemAction.rename,
child: Text(AppLocalizations.of(context)!.rename),
),
if (item is! RecipeItem) const PopupMenuDivider(),
if (household != null && item is! RecipeItem)
if (item is! RecipeItem && item.id != null) const PopupMenuDivider(),
if (household != null && item.id != null && item is! RecipeItem)
PopupMenuItem<_ItemAction>(
value: _ItemAction.merge,
child: Text(AppLocalizations.of(context)!.merge),
),
if (item is! RecipeItem)
if (item is! RecipeItem && item.id != null)
PopupMenuItem<_ItemAction>(
value: _ItemAction.delete,
child: Text(AppLocalizations.of(context)!.delete),
Expand Down

0 comments on commit 71d5b9d

Please sign in to comment.