From d4cf7d98a693a3fc619ec166775de2355689f7f2 Mon Sep 17 00:00:00 2001 From: AliasAlreadyTaken Date: Thu, 12 Sep 2024 11:41:18 +0200 Subject: [PATCH] Implements yl_survey.remove_record --- README.md | 2 +- api.lua | 27 +++++++++++++++++++++++++++ internal.lua | 9 +++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8bb9066..9e64952 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ Returns `true, s_id` if successful, `false, "errormessage` if not. Changes the properties of the survey `s_id` to the values given. `delete_responses` is boolean, optional and defaults to `false`, but if `true` it deletes all prior responses of the survey. See `create_survey` for the other values. ``` -yl_survey.remove_survey(s_id) +yl_survey.remove_record(s_id) ``` Returns `true, {survey}` if the survey was successfully removed, `false, "errormessage"` otherwise. diff --git a/api.lua b/api.lua index ab7ab59..b84617e 100644 --- a/api.lua +++ b/api.lua @@ -199,3 +199,30 @@ function yl_survey.edit_survey(id, owner, allow_names, allow_anonymous, return edit_survey(id, owner, allow_names, allow_anonymous, allow_changes, timestamp_start, timestamp_end, delete_responses) end + +-- yl_survey.remove_record +-- + +local function remove_record(id) + local data_success, data = yl_survey.get_data() + if (data_success ~= true) then + return false, "Cannot get data" + end + + local remove_success = yl_survey.remove_file(id) + if (remove_success ~= true) then + return false, yl_survey.t("Could not remove") + end + + local record = yl_survey.get_record(id) + if (record == nil) then + return false, yl_survey.t("record not found, cannot remove") + end + + data[id] = nil + return true, record +end + +function yl_survey.remove_record(id) + return remove_record(id) +end diff --git a/internal.lua b/internal.lua index 7ddc2d5..b53d49a 100644 --- a/internal.lua +++ b/internal.lua @@ -129,6 +129,15 @@ function yl_survey.save(filename, content, ...) return save_json(filename, content, ...) end +-- Remove file + +local function remove_file(UUID) + local path = get_filepath(UUID) + return os.remove(path) +end + +function yl_survey.remove_file(UUID) return remove_file(UUID) end + -- Load all data --