Skip to content

Commit 32b7dc1

Browse files
committed
Refactor note creation to use NewNote struct
Replaces positional arguments in note creation with a NewNote struct for improved clarity and maintainability. Updates all usages of store_new_note to use the new struct, adjusts related logic, and modernizes variable declarations and type usage throughout affected files. Also updates unit tests and related code to match the new interface. #1046
1 parent 8453b51 commit 32b7dc1

File tree

9 files changed

+537
-250
lines changed

9 files changed

+537
-250
lines changed

database/notes.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -523,41 +523,38 @@ std::string Database_Notes::assemble_contents (int identifier, std::string conte
523523

524524

525525
// Store a new consultation note into the database and in JSON.
526-
// bible: The notes's Bible.
527-
// book, chapter, verse: The note's passage.
528-
// summary: The note's summary.
529-
// contents: The note's contents.
530-
// raw: Import contents as it is.
531526
// It returns the identifier of this new note.
532-
int Database_Notes::store_new_note (const std::string& bible, int book, int chapter, int verse, std::string summary, std::string contents, bool raw)
527+
int Database_Notes::store_new_note (const NewNote& new_note)
533528
{
534529
// Create a new identifier.
535-
int identifier = get_new_unique_identifier ();
530+
const int identifier = get_new_unique_identifier ();
536531

537532
// Passage.
538-
std::string passage = encode_passage (book, chapter, verse);
533+
const std::string passage = encode_passage (new_note.book, new_note.chapter, new_note.verse);
539534

540-
std::string status = "New";
541-
int severity = 2;
535+
const std::string status = "New";
536+
constexpr int severity = static_cast<int>(SeveritySelector::normal);
542537

543538
// If the summary is not given, take the first line of the contents as the summary.
544-
if (summary == "") {
545-
// The notes editor does not put new lines at each line, but instead <div>s. Handle these also.
546-
summary = filter::strings::replace ("<", "\n", contents);
547-
std::vector <std::string> bits = filter::strings::explode (summary, '\n');
548-
if (!bits.empty ()) summary = bits [0];
539+
std::string summary {new_note.summary};
540+
if (summary.empty()) {
541+
// The notes editor does not put new lines at each line, but instead puts <div> elements. Handle these also.
542+
summary = filter::strings::replace ("<", "\n", new_note.contents);
543+
const std::vector<std::string> bits = filter::strings::explode (summary, '\n');
544+
if (!bits.empty ()) summary = bits.at(0);
549545
}
550546

551547
// Assemble contents.
552-
if (!raw) contents = assemble_contents (identifier, contents);
548+
std::string contents {new_note.contents};
549+
if (!new_note.raw) contents = assemble_contents (identifier, contents);
553550
if ((contents.empty()) && (summary.empty())) return 0;
554551

555552
// Store the JSON representation of the note in the file system.
556553
std::string path = note_file (identifier);
557554
std::string folder = filter_url_dirname (path);
558555
filter_url_mkdir (folder);
559556
jsonxx::Object note;
560-
note << bible_key () << bible;
557+
note << bible_key () << new_note.bible;
561558
note << passage_key () << passage;
562559
note << status_key () << status;
563560
note << severity_key () << std::to_string (severity);
@@ -572,7 +569,7 @@ int Database_Notes::store_new_note (const std::string& bible, int book, int chap
572569
sql.add ("INSERT INTO notes (identifier, modified, assigned, subscriptions, bible, passage, status, severity, summary, contents) VALUES (");
573570
sql.add (identifier);
574571
sql.add (", 0, '', '',");
575-
sql.add (bible);
572+
sql.add (new_note.bible);
576573
sql.add (",");
577574
sql.add (passage);
578575
sql.add (",");
@@ -765,7 +762,7 @@ std::vector<int> Database_Notes::select_notes(const Selector& selector)
765762
query.append (";");
766763

767764
SqliteDatabase sql (database_notes);
768-
sql.set_sql (query); // Todo can this be used to build the string?
765+
sql.set_sql (query);
769766
const std::vector <std::string> result = sql.query () ["identifier"];
770767
for (const auto& id : result) {
771768
identifiers.push_back (filter::strings::convert_to_int (id));

database/notes.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,21 @@ class Database_Notes
6565

6666
public:
6767
std::vector <int> get_identifiers ();
68-
int store_new_note (const std::string& bible, int book, int chapter, int verse, std::string summary, std::string contents, bool raw);
68+
struct NewNote {
69+
// The notes's Bible.
70+
const std::string bible{};
71+
// The note's passage.
72+
const int book{0};
73+
const int chapter{0};
74+
const int verse{0};
75+
// The note's summary.
76+
const std::string summary{};
77+
// The note's contents.
78+
const std::string contents{};
79+
// Import contents as it is.
80+
const bool raw {false};
81+
};
82+
int store_new_note (const NewNote& new_note);
6983
private:
7084
int get_new_unique_identifier ();
7185

demo/logic.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,15 @@ void demo_create_sample_notes (Webserver_Request& webserver_request)
342342
std::vector <int> identifiers = database_notes.get_identifiers ();
343343
if (identifiers.size () < 10) {
344344
for (int i = 1; i <= 10; i++) {
345-
database_notes.store_new_note (demo_sample_bible_name (), i, i, i, "Sample Note " + std::to_string (i), "Sample Contents for note " + std::to_string (i), false);
345+
Database_Notes::NewNote new_note {
346+
.bible = demo_sample_bible_name (),
347+
.book = i,
348+
.chapter = i,
349+
.verse = i,
350+
.summary = "Sample Note " + std::to_string (i),
351+
.contents = "Sample Contents for note " + std::to_string (i),
352+
};
353+
database_notes.store_new_note (new_note);
346354
}
347355
}
348356
}

notes/index.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,36 +59,36 @@ std::string notes_index (Webserver_Request& webserver_request)
5959
// Presets for notes selectors.
6060
// This is for the daily statistics and the workspace.
6161
if (webserver_request.query.count ("presetselection")) {
62-
webserver_request.database_config_user()->set_consultation_notes_passage_selector (3);
63-
webserver_request.database_config_user()->set_consultation_notes_edit_selector (0);
64-
webserver_request.database_config_user()->set_consultation_notes_non_edit_selector (0);
62+
webserver_request.database_config_user()->set_consultation_notes_passage_selector (static_cast<int>(Database_Notes::PassageSelector::any_passage));
63+
webserver_request.database_config_user()->set_consultation_notes_edit_selector (static_cast<int>(Database_Notes::EditSelector::at_any_time));
64+
webserver_request.database_config_user()->set_consultation_notes_non_edit_selector (static_cast<int>(Database_Notes::NonEditSelector::any_time));
6565
webserver_request.database_config_user()->set_consultation_notes_status_selectors ({});
66-
webserver_request.database_config_user()->set_consultation_notes_bible_selector ("");
67-
webserver_request.database_config_user()->set_consultation_notes_assignment_selector ("");
68-
webserver_request.database_config_user()->set_consultation_notes_subscription_selector (0);
69-
webserver_request.database_config_user()->set_consultation_notes_severity_selector (-1);
70-
webserver_request.database_config_user()->set_consultation_notes_text_selector (0);
66+
webserver_request.database_config_user()->set_consultation_notes_bible_selector (std::string());
67+
webserver_request.database_config_user()->set_consultation_notes_assignment_selector (std::string());
68+
webserver_request.database_config_user()->set_consultation_notes_subscription_selector (false);
69+
webserver_request.database_config_user()->set_consultation_notes_severity_selector (static_cast<int>(Database_Notes::SeveritySelector::any));
70+
webserver_request.database_config_user()->set_consultation_notes_text_selector (false);
7171
std::string preset_selector = webserver_request.query ["presetselection"];
7272
if (preset_selector == "assigned") {
7373
webserver_request.database_config_user()->set_consultation_notes_assignment_selector (webserver_request.session_logic ()->get_username ());
7474
}
7575
if (preset_selector == "subscribed") {
76-
webserver_request.database_config_user()->set_consultation_notes_subscription_selector (1);
76+
webserver_request.database_config_user()->set_consultation_notes_subscription_selector (true);
7777
}
7878
if (preset_selector == "subscribeddayidle") {
79-
webserver_request.database_config_user()->set_consultation_notes_subscription_selector (1);
80-
webserver_request.database_config_user()->set_consultation_notes_non_edit_selector (1);
79+
webserver_request.database_config_user()->set_consultation_notes_subscription_selector (true);
80+
webserver_request.database_config_user()->set_consultation_notes_non_edit_selector (static_cast<int>(Database_Notes::NonEditSelector::a_day));
8181
}
8282
if (preset_selector == "subscribedweekidle") {
83-
webserver_request.database_config_user()->set_consultation_notes_subscription_selector (1);
84-
webserver_request.database_config_user()->set_consultation_notes_non_edit_selector (3);
83+
webserver_request.database_config_user()->set_consultation_notes_subscription_selector (true);
84+
webserver_request.database_config_user()->set_consultation_notes_non_edit_selector (static_cast<int>(Database_Notes::NonEditSelector::a_week));
8585
}
8686
if (preset_selector == "forverse") {
87-
webserver_request.database_config_user()->set_consultation_notes_passage_selector (0);
87+
webserver_request.database_config_user()->set_consultation_notes_passage_selector (static_cast<int>(Database_Notes::PassageSelector::current_verse));
8888
}
8989
}
9090

91-
int level = webserver_request.session_logic ()->get_level ();
91+
const int level = webserver_request.session_logic ()->get_level ();
9292
// Manager roles and higher can do mass updates on the notes.
9393
if (level >= roles::manager) {
9494
// No mass updates in basic mode.

notes/logic.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,16 @@ int Notes_Logic::createNote (std::string bible, int book, int chapter, int verse
5454
{
5555
summary = filter::strings::replace ("\n", "", summary);
5656
Database_Notes database_notes (m_webserver_request);
57-
int note_id = database_notes.store_new_note (bible, book, chapter, verse, summary, contents, raw);
57+
Database_Notes::NewNote new_note {
58+
.bible = bible,
59+
.book = book,
60+
.chapter = chapter,
61+
.verse = verse,
62+
.summary = summary,
63+
.contents = contents,
64+
.raw = raw
65+
};
66+
const int note_id = database_notes.store_new_note (new_note);
5867
if (client_logic_client_enabled ()) {
5968
// Client: record the action in the database.
6069
Database_NoteActions database_noteactions;
@@ -656,7 +665,15 @@ bool Notes_Logic::handleEmailNew (std::string from, std::string subject, std::st
656665
m_webserver_request.session_logic()->set_username (username);
657666
Database_Notes database_notes (m_webserver_request);
658667
std::string bible = m_webserver_request.database_config_user()->get_bible ();
659-
int identifier = database_notes.store_new_note (bible, static_cast<int>(book), chapter, verse, summary, body, false);
668+
Database_Notes::NewNote new_note {
669+
.bible = bible,
670+
.book = static_cast<int>(book),
671+
.chapter = chapter,
672+
.verse = verse,
673+
.summary = summary,
674+
.contents = body,
675+
};
676+
const int identifier = database_notes.store_new_note (new_note);
660677
handlerNewNote (identifier);
661678
m_webserver_request.session_logic()->set_username (sessionuser);
662679
// Mail confirmation to the username.

notes/notes.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,32 @@ std::string notes_notes (Webserver_Request& webserver_request)
5050
Database_Notes database_notes (webserver_request);
5151

5252

53-
std::string bible = access_bible::clamp (webserver_request, webserver_request.database_config_user()->get_bible());
54-
int book = Ipc_Focus::getBook (webserver_request);
55-
int chapter = Ipc_Focus::getChapter (webserver_request);
56-
int verse = Ipc_Focus::getVerse (webserver_request);
53+
const std::string bible = access_bible::clamp (webserver_request, webserver_request.database_config_user()->get_bible());
54+
const int book = Ipc_Focus::getBook (webserver_request);
55+
const int chapter = Ipc_Focus::getChapter (webserver_request);
56+
const int verse = Ipc_Focus::getVerse (webserver_request);
5757

5858

59-
Database_Notes::PassageSelector passage_selector = static_cast<Database_Notes::PassageSelector>(webserver_request.database_config_user()->get_consultation_notes_passage_selector());
60-
int edit_selector = webserver_request.database_config_user()->get_consultation_notes_edit_selector();
61-
auto non_edit_selector = static_cast<Database_Notes::NonEditSelector>(webserver_request.database_config_user()->get_consultation_notes_non_edit_selector());
59+
const auto passage_selector = static_cast<Database_Notes::PassageSelector>(webserver_request.database_config_user()->get_consultation_notes_passage_selector());
60+
const int edit_selector = webserver_request.database_config_user()->get_consultation_notes_edit_selector();
61+
const auto non_edit_selector = static_cast<Database_Notes::NonEditSelector>(webserver_request.database_config_user()->get_consultation_notes_non_edit_selector());
6262
const std::vector<std::string> status_selectors = webserver_request.database_config_user()->get_consultation_notes_status_selectors();
63-
std::string assignment_selector = webserver_request.database_config_user()->get_consultation_notes_assignment_selector();
64-
bool subscription_selector = webserver_request.database_config_user()->get_consultation_notes_subscription_selector();
65-
auto severity_selector = static_cast<Database_Notes::SeveritySelector>(webserver_request.database_config_user()->get_consultation_notes_severity_selector());
66-
const int text_selector = webserver_request.database_config_user()->get_consultation_notes_text_selector();
63+
const std::string bible_selector = webserver_request.database_config_user()->get_consultation_notes_bible_selector();
64+
const std::string assignment_selector = webserver_request.database_config_user()->get_consultation_notes_assignment_selector();
65+
const bool subscription_selector = webserver_request.database_config_user()->get_consultation_notes_subscription_selector();
66+
const auto severity_selector = static_cast<Database_Notes::SeveritySelector>(webserver_request.database_config_user()->get_consultation_notes_severity_selector());
67+
const bool text_selector = webserver_request.database_config_user()->get_consultation_notes_text_selector();
6768
const std::string search_text = text_selector ? webserver_request.database_config_user()->get_consultation_notes_search_text() : "";
68-
int passage_inclusion_selector = webserver_request.database_config_user()->get_consultation_notes_passage_inclusion_selector();
69-
int text_inclusion_selector = webserver_request.database_config_user()->get_consultation_notes_text_inclusion_selector();
69+
const bool passage_inclusion_selector = webserver_request.database_config_user()->get_consultation_notes_passage_inclusion_selector();
70+
const bool text_inclusion_selector = webserver_request.database_config_user()->get_consultation_notes_text_inclusion_selector();
7071

7172

7273
// The Bibles the current user has access to or else the Bible selector.
73-
std::vector <std::string> bibles = {webserver_request.database_config_user()->get_consultation_notes_bible_selector()};
74-
if (bibles.empty())
74+
std::vector<std::string> bibles{};
75+
if (bible_selector.empty())
7576
bibles = access_bible::bibles (webserver_request, webserver_request.session_logic ()->get_username ());
76-
77+
else
78+
bibles = {webserver_request.database_config_user()->get_consultation_notes_bible_selector()};
7779

7880

7981
// The admin disables notes selection on Bibles,

sync/notes.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,15 @@ std::string sync_notes (Webserver_Request& webserver_request)
181181
case Sync_Logic::notes_put_create_initiate:
182182
{
183183
// Create the note on the server.
184-
int server_id = database_notes.store_new_note ("", 1, 1, 1, "<empty>", "<empty>", false);
184+
Database_Notes::NewNote new_note {
185+
.bible = "",
186+
.book = 1,
187+
.chapter = 1,
188+
.verse = 1,
189+
.summary = "<empty>",
190+
.contents = "<empty>",
191+
};
192+
int server_id = database_notes.store_new_note (new_note);
185193
// Update the note identifier on the server to be same as on the client.
186194
database_notes.set_identifier (server_id, identifier);
187195
// Update search field.

unittests/memory.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,16 @@ TEST (DISABLED_memory, basic)
9090
Database_Notes database_notes (webserver_request);
9191
database_notes.create ();
9292
for (int i = 0; i < 100; i++) {
93-
database_notes.store_new_note ("bible", i, i, i, "summary", "contents", true);
93+
Database_Notes::NewNote new_note {
94+
.bible = "bible",
95+
.book = i,
96+
.chapter = i,
97+
.verse = i,
98+
.summary = "summary",
99+
.contents = "contents",
100+
.raw = true
101+
};
102+
database_notes.store_new_note(new_note);
94103
}
95104
std::thread * recorder = nullptr;
96105
uint64_t basic_usage = filter_memory_total_usage ();

0 commit comments

Comments
 (0)