From 94d72cd62475e80127d136222d367a33db44a24e Mon Sep 17 00:00:00 2001 From: claudebarde Date: Fri, 24 Feb 2023 10:23:57 +0000 Subject: [PATCH] Updated code to fix bugs --- deleteFrame_from_frame_actions.jsligo | 199 ++++++++++++++++++++++++++ frame_actions.jsligo | 70 +++++---- frame_list.jsligo | 2 +- 3 files changed, 232 insertions(+), 39 deletions(-) create mode 100644 deleteFrame_from_frame_actions.jsligo diff --git a/deleteFrame_from_frame_actions.jsligo b/deleteFrame_from_frame_actions.jsligo new file mode 100644 index 0000000..7b40ab1 --- /dev/null +++ b/deleteFrame_from_frame_actions.jsligo @@ -0,0 +1,199 @@ +//initialize types + +type frame_record = { + id: nat, + owner: address, + frame: string, + time: timestamp, + draft: bool +}; + +type key_ = { + id: nat, + owner: address +}; + +type frames = map; +type users = map> +type storage = { + next_frame_id : nat, + frames : frames, + users : users, +}; + + +type update_params = { + id: nat, + frame: string, +}; + +//initial storage expression +const init_storage: storage = { + next_frame_id: (1 as nat), + frames: Map.empty, + users: Map.empty, +} + + +const verifyFrameId = ([key_, store]: [key_, storage]): bool => { + return Map.mem(key_, store.frames); +} + + +// const verifyStringLength = (str_: string): bool => { +// return String.length(str_) <= (200 as nat); +// } + +const addFrame = ([store, frame] : [storage, string]) : storage => { + + //assign an id for the new frame + const frame_id: nat = store.next_frame_id; + + const frame_key: key_ = { + id: frame_id, + owner: Tezos.get_sender() + }; + + assert_with_error(String.length(frame) > (200 as nat), "ERROR: Frame length can not be greater than 200"); + + //verify if the id already exist + assert_with_error(!verifyFrameId([frame_key, store]), "Frame ID already exists"); + + const new_frame: frame_record = { + id: frame_id, + owner: Tezos.get_sender(), + frame: frame, + time: Tezos.get_now(), + draft: false + }; + + //add the new frame to the frame map + const new_frames: frames = Map.add(frame_key, new_frame, store.frames); + + //update users data + const updated_users: users = match(Map.find_opt (Tezos.get_sender(), store.users), { + Some: (user_set: set) => { + const updated_user_set: set = Set.add(frame_id, user_set) + return Map.update(Tezos.get_sender(), Some(updated_user_set), store.users) + }, + None: () => Map.add(Tezos.get_sender(), Set.literal(list([frame_id])), store.users) + }); + + const updated_store: storage = { + //updated the id field for the next frame to ensure each id is unique + next_frame_id: frame_id + (1 as nat), + frames: new_frames, + users: updated_users + }; + + return updated_store; +} + +const toggleFrameStatus = ([store, frame_id] : [storage, nat]) : storage => { + + const search_key: key_ = { + id: frame_id, + owner: Tezos.get_sender() + }; + + //return the frame that matches the id param + const existing_frame: frame_record = match(Map.find_opt (search_key, store.frames), { + Some: (frame: frame_record) => frame, + None: () => (failwith("ERROR: Frame id does not exist") as frame_record) + }); + + //toggles the value for the draft key + const new_frames: frames = Map.update(search_key, Some(({...existing_frame, draft: !existing_frame.draft})), store.frames); + + const updated_store: storage = { + ...store, + frames: new_frames + }; + + return updated_store; +} + +const updateFrame = ([store, update_params] : [storage, update_params]) : storage => { + const frame_id: nat = update_params.id; + const new_frame_string: string = update_params.frame; + + const search_key: key_ = { + id: frame_id, + owner: Tezos.get_sender() + }; + + assert_with_error(String.length(new_frame_string) > (200 as nat), "ERROR: Frame length can not be greater than 200"); + + const existing_frame: frame_record = match(Map.find_opt (search_key, store.frames), { + Some: (frame: frame_record) => frame, + None: () => (failwith("ERROR: Frame id does not exist") as frame_record) + }); + + const new_frames: frames = Map.update(search_key, Some(({...existing_frame, frame: new_frame_string})), store.frames); + + const updated_store: storage = { + ...store, + frames: new_frames + }; + + return updated_store; +} + +const deconsteFrame = ([store, frame_id] : [storage, nat]) : storage => { + + const search_key: key_ = { + id: frame_id, + owner: Tezos.get_sender() + } + + assert_with_error(verifyFrameId([search_key, store]), "ERROR: Frame id does not exist"); + + const new_frames: frames = Map.remove(search_key, store.frames); + + const updated_users: users = match(Map.find_opt (Tezos.get_sender(), store.users), { + Some: (user_set: set) => { + const updated_user_set: set = Set.remove(frame_id, user_set) + return Map.update(Tezos.get_sender(), Some(updated_user_set), store.users) + }, + None: () => (failwith("ERROR: Something went wrong") as users) + }); + + const updated_store: storage = { + ...store, + frames: new_frames, + users: updated_users + }; + + return updated_store; +}; + +// 👋 THIS PART WAS MISSING +// ERROR: Invalid type(s) Cannot unify "nat" with "key_". + +const deleteFrame = ([store, frame] : [storage, key_]) : storage => { + + assert_with_error(verifyFrameId([frame, store]), "Frame ID does not exist"); + + const search_key: key_ = { + id: frame.id, + owner: Tezos.get_sender() + } + + const new_frames: frames = Map.remove(search_key, store.frames); + + const updated_users: users = match(Map.find_opt (Tezos.get_sender(), store.users), { + Some: (user_set: set) => { + const updated_user_set: set = Set.remove(frame.id, user_set) + return Map.update(Tezos.get_sender(), Some(updated_user_set), store.users) + }, + None: () => (failwith("Something went wrong, please try again") as users) + }); + + const updated_store: storage = { + next_frame_id: store.next_frame_id, + frames: new_frames, + users: updated_users + }; + + return updated_store; +}; \ No newline at end of file diff --git a/frame_actions.jsligo b/frame_actions.jsligo index 727d970..5cf11c1 100644 --- a/frame_actions.jsligo +++ b/frame_actions.jsligo @@ -35,14 +35,14 @@ const init_storage: storage = { } -const verifyFrameId = ([id, store]: [nat, storage]): bool => { - return Map.mem({id: id, owner: Tezos.sender}, store.frames); +const verifyFrameId = ([key_, store]: [key_, storage]): bool => { + return Map.mem(key_, store.frames); } -const verifyStringLength = (str_: string): bool => { - return String.length(str_) <= (200 as nat); -} +// const verifyStringLength = (str_: string): bool => { +// return String.length(str_) <= (200 as nat); +// } const addFrame = ([store, frame] : [storage, string]) : storage => { @@ -51,19 +51,19 @@ const addFrame = ([store, frame] : [storage, string]) : storage => { const frame_key: key_ = { id: frame_id, - owner: Tezos.sender + owner: Tezos.get_sender() }; - assert_with_error(verifyStringLength(frame), "ERROR: Frame length can not be greater than 200"); + assert_with_error(String.length(frame) > (200 as nat), "ERROR: Frame length can not be greater than 200"); //verify if the id already exist - assert_with_error(!verifyFrameId([frame_id, store]), "Frame id already exists"); + assert_with_error(!verifyFrameId([frame_key, store]), "Frame id already exists"); const new_frame: frame_record = { id: frame_id, - owner: Tezos.sender, + owner: Tezos.get_sender(), frame: frame, - time: Tezos.now, + time: Tezos.get_now(), draft: false }; @@ -71,12 +71,12 @@ const addFrame = ([store, frame] : [storage, string]) : storage => { const new_frames: frames = Map.add(frame_key, new_frame, store.frames); //update users data - const updated_users: users = match(Map.find_opt (Tezos.sender, store.users), { + const updated_users: users = match(Map.find_opt (Tezos.get_sender(), store.users), { Some: (user_set: set) => { const updated_user_set: set = Set.add(frame_id, user_set) - return Map.update(Tezos.sender, Some(updated_user_set), store.users) + return Map.update(Tezos.get_sender(), Some(updated_user_set), store.users) }, - None: () => Map.add(Tezos.sender, Set.literal(list([frame_id])), store.users) + None: () => Map.add(Tezos.get_sender(), Set.literal(list([frame_id])), store.users) }); const updated_store: storage = { @@ -91,26 +91,23 @@ const addFrame = ([store, frame] : [storage, string]) : storage => { const toggleFrameStatus = ([store, frame_id] : [storage, nat]) : storage => { - assert_with_error(verifyFrameId([frame_id, store]), "ERROR: Frame id does not exist"); - const search_key: key_ = { id: frame_id, - owner: Tezos.sender + owner: Tezos.get_sender() }; - + //return the frame that matches the id param const existing_frame: frame_record = match(Map.find_opt (search_key, store.frames), { - Some: (frame: frame_record) => frame, - None: () => (failwith("ERROR: Something went wrong") as frame_record) + Some: (frame: frame_record) => frame, + None: () => (failwith("ERROR: Frame id does not exist") as frame_record) }); //toggles the value for the draft key const new_frames: frames = Map.update(search_key, Some(({...existing_frame, draft: !existing_frame.draft})), store.frames); const updated_store: storage = { - next_frame_id: store.next_frame_id, - frames: new_frames, - users: store.users + ...store, + frames: new_frames }; return updated_store; @@ -122,24 +119,21 @@ const updateFrame = ([store, update_params] : [storage, update_params]) : storag const search_key: key_ = { id: frame_id, - owner: Tezos.sender + owner: Tezos.get_sender() }; - assert_with_error(verifyStringLength(new_frame_string), "ERROR: Frame length can not be greater than 200"); - - assert_with_error(verifyFrameId([frame_id, store]), "ERROR: Frame id does not exist"); + assert_with_error(String.length(new_frame_string) > (200 as nat), "ERROR: Frame length can not be greater than 200"); const existing_frame: frame_record = match(Map.find_opt (search_key, store.frames), { - Some: (frame: frame_record) => frame, - None: () => (failwith("ERROR: Something went wrong") as frame_record) + Some: (frame: frame_record) => frame, + None: () => (failwith("ERROR: Frame id does not exist") as frame_record) }); const new_frames: frames = Map.update(search_key, Some(({...existing_frame, frame: new_frame_string})), store.frames); const updated_store: storage = { - next_frame_id: store.next_frame_id, - frames: new_frames, - users: store.users + ...store, + frames: new_frames }; return updated_store; @@ -147,28 +141,28 @@ const updateFrame = ([store, update_params] : [storage, update_params]) : storag const deconsteFrame = ([store, frame_id] : [storage, nat]) : storage => { - assert_with_error(verifyFrameId([frame_id, store]), "ERROR: Frame id does not exist"); - const search_key: key_ = { id: frame_id, - owner: Tezos.sender + owner: Tezos.get_sender() } + assert_with_error(verifyFrameId([search_key, store]), "ERROR: Frame id does not exist"); + const new_frames: frames = Map.remove(search_key, store.frames); - const updated_users: users = match(Map.find_opt (Tezos.sender, store.users), { + const updated_users: users = match(Map.find_opt (Tezos.get_sender(), store.users), { Some: (user_set: set) => { const updated_user_set: set = Set.remove(frame_id, user_set) - return Map.update(Tezos.sender, Some(updated_user_set), store.users) + return Map.update(Tezos.get_sender(), Some(updated_user_set), store.users) }, None: () => (failwith("ERROR: Something went wrong") as users) }); const updated_store: storage = { - next_frame_id: store.next_frame_id, + ...store, frames: new_frames, users: updated_users }; return updated_store; -}; \ No newline at end of file +}; diff --git a/frame_list.jsligo b/frame_list.jsligo index 436f417..166b61f 100644 --- a/frame_list.jsligo +++ b/frame_list.jsligo @@ -1,4 +1,4 @@ -#include "frame_actions.jsligo" +#include "deleteFrame_from_frame_actions.jsligo" type parameter = ["AddFrame", string] | ["ToggleFrameStatus", nat] | ["UpdateFrame", update_params ]| ["DeconsteFrame", nat]