Skip to content

Commit

Permalink
update cart func and add status for table
Browse files Browse the repository at this point in the history
  • Loading branch information
Bahamin1 committed Jun 21, 2024
1 parent ef80e7a commit 0986962
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/Dorder/Table.mo
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ module {
reserveTime : ?Time.Time;
userWantsToJoin : [Principal];
seatedCustomers : [Principal];
order : ?Cart.Order;
status : TableStatus;
order : [Cart.CartItem];
};

public type TableStatus = {
#Open;
#OnOrder;
#Finalized;
};

public type TableMap = Map.Map<Nat, Table>;
Expand All @@ -45,9 +52,10 @@ module {
capacity = capacity;
reservedBy = null;
reserveTime = null;
status = #Open;
userWantsToJoin = [];
seatedCustomers = [];
order = null;
order = [];

};

Expand All @@ -72,9 +80,10 @@ module {
capacity = table.capacity;
reservedBy = ?reservedBy;
reserveTime = ?Time.now();
status = #OnOrder;
userWantsToJoin = [];
seatedCustomers = [];
order = null;
order = [];
};
put(tables, tableId, updatedTable);
return #ok(tables);
Expand Down Expand Up @@ -102,9 +111,10 @@ module {
capacity = table.capacity;
reservedBy = null;
reserveTime = null;
status = #Open;
userWantsToJoin = [];
seatedCustomers = [];
order = null;
order = [];

};
put(tables, tableId, updatedTable);
Expand Down
32 changes: 31 additions & 1 deletion src/Dorder/main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,39 @@ shared ({ caller = manager }) actor class Dorder() = this {
createdAt = Time.now();
};
Map.set(cartMap, phash, caller, newOrder);
return newOrder;
return #ok(newOrder);

};

public shared ({ caller }) func addOrderToTable(items : [Menu.MenuItem], tableId : Nat) : async Result.Result<Text, Text> {
switch (Table.get(tableMap, tableId)) {
case (?table) {
if (table.status == #Finalized) {
return #err("Cannot add order. The table has finalized orders.");
};

let newOrder = {
id = Time.now();
items = items;
totalPrice = items.foldLeft<Nat>(0, func(acc, item) { acc + item.price });
tableId = ?tableId;
orderType = #OnTable;
orderStatus = #Pending;
orderedBy = caller;
orderedAt = Time.now();
finalized = false;
};

table.orders.add(newOrder);
Table.put(tableMap, tableId, table);

return #ok("Order added to table successfully.");
};
case (null) {
return #err("Table not found.");
};
};
};
};

// public shared ({ caller }) func addToCart(menuId : Nat, quantity : Nat) : async Result.Result<Cart.CartMap, Text> {
Expand Down

0 comments on commit 0986962

Please sign in to comment.