Skip to content

Commit

Permalink
update table system
Browse files Browse the repository at this point in the history
  • Loading branch information
Bahamin1 committed Jun 19, 2024
1 parent fffdc20 commit 73fc5de
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 58 deletions.
63 changes: 19 additions & 44 deletions src/Dorder/Table.mo
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ module {
seatedCustomers = table.seatedCustomers;
order = table.order;
};
put(tableMap, tableId, updatedTable);
return;

};
Expand All @@ -221,55 +222,29 @@ module {
switch (get(tableMap, tableId)) {
case null { return [] };
case (?table) {
switch (table.userWantsToJoin) {
case (users) {
let removedPrincipal = removeElementFromArray(users, p);

let updatedUserWantsToJoin : Table = {
id = tableId;
capacity = table.capacity;
reservedBy = table.reservedBy;
reserveTime = table.reserveTime;
userWantsToJoin = removedPrincipal;
seatedCustomers = table.seatedCustomers;
order = table.order;
};
put(tableMap, tableId, updatedUserWantsToJoin);
Debug.print("user successfully removed from wants to join");
};
};

switch (table.seatedCustomers) {
case (seat) {
let newSeated = Array.append<Principal>(seat, [p]);

let updatedSeatedCustomers : Table = {
id = tableId;
capacity = table.capacity;
reservedBy = table.reservedBy;
reserveTime = table.reserveTime;
userWantsToJoin = table.userWantsToJoin;
seatedCustomers = newSeated;
order = table.order;
};
put(tableMap, tableId, updatedSeatedCustomers);
Debug.print("user successfully add to seated customer");

return newSeated;
};
let removedPrincipal = Array.filter<Principal>(
table.userWantsToJoin,
func(x) {
x != p;
},
);
let newSeated = Array.append<Principal>(table.seatedCustomers, [p]);

let updatedSeatedCustomers : Table = {
id = tableId;
capacity = table.capacity;
reservedBy = table.reservedBy;
reserveTime = table.reserveTime;
userWantsToJoin = removedPrincipal;
seatedCustomers = newSeated;
order = table.order;
};
put(tableMap, tableId, updatedSeatedCustomers);

return removedPrincipal;
};
};
};

func removeElementFromArray(arr : [Principal], valueToRemove : Principal) : [Principal] {
return Array.filter<Principal>(
arr,
func(x : Principal) : Bool {
x != valueToRemove;
},
);
};

};
30 changes: 16 additions & 14 deletions src/Dorder/main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ shared ({ caller = manager }) actor class Dorder() = this {
// When this function is called and results in an error, the employee must be notified.
// The table you're trying to access is reserved.
// Do you want to request to join this table? This will call the SeatOnTable function.
public shared ({ caller }) func reserveTableNew(tableId : Nat) : async Result.Result<Text, Text> {
public shared ({ caller }) func reserveTable(tableId : Nat) : async Result.Result<Text, Text> {
if (User.userCanPerform(userMap, caller, #ReserveTable) != true) {
return #err("The caller " #Principal.toText(caller) # " dose not have permission Reserve Table!");
};
Expand All @@ -157,17 +157,7 @@ shared ({ caller = manager }) actor class Dorder() = this {
};
};

public shared ({ caller }) func seatOnTable(tableId : Nat) : async Result.Result<Text, Text> {
if (Table.isReserved(tableMap, tableId) != true) {
return #err("This table already open for Reserve");
};

Table.requestToJoinTable(tableMap, tableId, caller);
return #ok("requst sent ! wait for Reserver response");

};

public shared ({ caller }) func unreserveTableNew(tableId : Nat) : async Result.Result<Text, Text> {
public shared ({ caller }) func unreserveTable(tableId : Nat) : async Result.Result<Text, Text> {
if (Table.canUnreserveTable(employeeMap, tableMap, caller, tableId) != true) {
return #err("can't unreserve table becuse caller didn't reserve any table !");
};
Expand All @@ -183,6 +173,16 @@ shared ({ caller = manager }) actor class Dorder() = this {
};
};

public shared ({ caller }) func seatOnTable(tableId : Nat) : async Result.Result<Text, Text> {
if (Table.isReserved(tableMap, tableId) != true) {
return #err("This table already open for Reserve");
};

Table.requestToJoinTable(tableMap, tableId, caller);
return #ok("requst sent ! wait for Reserver response");

};

public shared query ({ caller }) func getRequstesJoinToTable(tableId : Nat) : async Result.Result<[Principal], Text> {
if (Table.canUnreserveTable(employeeMap, tableMap, caller, tableId) != true) {
return #err("this member " #Principal.toText(caller) # " didnt Reserve table " #Nat.toText(tableId) # "!");
Expand All @@ -203,7 +203,7 @@ shared ({ caller = manager }) actor class Dorder() = this {
};
};

public shared ({ caller }) func addguestTotable(tableId : Nat, p : Principal, yesOrNo : Bool) : async Result.Result<[Principal], Text> {
public shared ({ caller }) func addGuestTotable(tableId : Nat, p : Principal, yesOrNo : Bool) : async Result.Result<[Principal], Text> {
if (Table.canUnreserveTable(employeeMap, tableMap, caller, tableId) != true) {
return #err("this member " #Principal.toText(caller) # " didnt Reserve the table " #Nat.toText(tableId) # "!");
};
Expand Down Expand Up @@ -269,7 +269,7 @@ shared ({ caller = manager }) actor class Dorder() = this {

//--------------------------- Review Functions ----------------------------\\

public shared ({ caller }) func addOrUpdateItemScore(menuId : Nat, star : Review.Star, suggest : Bool, comment : ?Text, image : ?[Blob]) : async Result.Result<Text, Text> {
public shared ({ caller }) func addOrUpdateMenuItemScore(menuId : Nat, star : Review.Star, suggest : Bool, comment : ?Text, image : ?[Blob]) : async Result.Result<Text, Text> {
if (User.userCanPerform(userMap, caller, #ModifyMenuItemPoint) != true) {
return #err("The caller " #Principal.toText(caller) # " dose not have permission to Review an item create a account first or login to your account!");
};
Expand Down Expand Up @@ -390,6 +390,7 @@ shared ({ caller = manager }) actor class Dorder() = this {
};

let allMembers = Map.vals<Principal, User.Employee>(employeeMap);

for (member in allMembers) {
let newPoint : Review.EmployeeReview = {
pointBy = caller;
Expand Down Expand Up @@ -418,6 +419,7 @@ shared ({ caller = manager }) actor class Dorder() = this {

return #ok();
};

};

// //member
Expand Down

0 comments on commit 73fc5de

Please sign in to comment.