| 1 | using Microsoft.Data.Sqlite; |
| 2 | |
| 3 | namespace SimPas2_Windows.Managers |
| 4 | { |
| 5 | public class NoteManager |
| 6 | { |
| 7 | private readonly string mConnectionString; |
| 8 | private readonly string mCulture; |
| 9 | |
| 10 | private string mJpLang; |
| 11 | |
| 12 | public NoteManager(string databasePath, string culture) |
| 13 | { |
| 14 | mConnectionString = $"Data Source={databasePath}"; |
| 15 | mCulture = culture; |
| 16 | |
| 17 | mJpLang = "ja-JP"; |
| 18 | } |
| 19 | |
| 20 | private bool AlreadyExists(string name, int? excludeId = null) |
| 21 | { |
| 22 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 23 | { |
| 24 | conn.Open(); |
| 25 | SqliteCommand com = conn.CreateCommand(); |
| 26 | com.CommandText = @" |
| 27 | SELECT COUNT(*) FROM Notes |
| 28 | WHERE UPPER(Name) = UPPER(@name)"; |
| 29 | com.Parameters.AddWithValue("@name", name); |
| 30 | |
| 31 | if (excludeId.HasValue) |
| 32 | { |
| 33 | com.CommandText += " AND Id != @excludeId"; |
| 34 | com.Parameters.AddWithValue("@excludeId", excludeId.Value); |
| 35 | } |
| 36 | |
| 37 | return Convert.ToInt32(com.ExecuteScalar()) > 0; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | public void AddNote(string name) |
| 42 | { |
| 43 | if (string.IsNullOrWhiteSpace(name)) |
| 44 | { |
| 45 | string err = mCulture == mJpLang |
| 46 | ? "ファイル名を御入力下さい。" |
| 47 | : "Please fill in the filename."; |
| 48 | throw new ArgumentException(err); |
| 49 | } |
| 50 | |
| 51 | if (AlreadyExists(name)) |
| 52 | { |
| 53 | string err = mCulture == mJpLang |
| 54 | ? $"ファイル名「{name}」付きメモは既に存在します。" |
| 55 | : $"A note with the filename '{name}' already exists."; |
| 56 | throw new ArgumentException(err); |
| 57 | } |
| 58 | |
| 59 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 60 | { |
| 61 | conn.Open(); |
| 62 | SqliteCommand com = conn.CreateCommand(); |
| 63 | com.CommandText = @" |
| 64 | INSERT INTO Notes (Name) |
| 65 | VALUES ($name)"; |
| 66 | com.Parameters.AddWithValue("$name", name); |
| 67 | com.ExecuteNonQuery(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public bool EditNote(int id, string name) |
| 72 | { |
| 73 | if (string.IsNullOrWhiteSpace(name)) |
| 74 | { |
| 75 | string err = mCulture == mJpLang |
| 76 | ? "ファイル名を御入力下さい。" : |
| 77 | "Please fill in the filename."; |
| 78 | throw new ArgumentException(err); |
| 79 | } |
| 80 | |
| 81 | if (AlreadyExists(name, id)) |
| 82 | { |
| 83 | string err = mCulture == mJpLang |
| 84 | ? $"ファイル名「{name}」付きメモは既に存在します。" |
| 85 | : $"A note with the filename '{name}' already exists."; |
| 86 | throw new ArgumentException(err); |
| 87 | } |
| 88 | |
| 89 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 90 | { |
| 91 | conn.Open(); |
| 92 | SqliteCommand com = conn.CreateCommand(); |
| 93 | com.CommandText = @" |
| 94 | UPDATE Notes |
| 95 | SET Name = $name |
| 96 | WHERE Id = $id"; |
| 97 | com.Parameters.AddWithValue("id", id); |
| 98 | com.Parameters.AddWithValue("$name", name); |
| 99 | |
| 100 | return com.ExecuteNonQuery() > 0; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | public bool SaveNote(int id, string text) |
| 105 | { |
| 106 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 107 | { |
| 108 | conn.Open(); |
| 109 | SqliteCommand com = conn.CreateCommand(); |
| 110 | com.CommandText = @" |
| 111 | UPDATE Notes |
| 112 | SET Text = @text |
| 113 | WHERE Id = @id"; |
| 114 | com.Parameters.AddWithValue("@id", id); |
| 115 | com.Parameters.AddWithValue("@text", text ?? (object)DBNull.Value); |
| 116 | |
| 117 | return com.ExecuteNonQuery() > 0; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | public bool DeleteNote(int id) |
| 122 | { |
| 123 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 124 | { |
| 125 | conn.Open(); |
| 126 | SqliteCommand com = conn.CreateCommand(); |
| 127 | com.CommandText = "DELETE FROM Notes WHERE Id = $id"; |
| 128 | com.Parameters.AddWithValue("$id", id); |
| 129 | |
| 130 | return com.ExecuteNonQuery() > 0; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | public List<(int Id, string Name, string Text)> GetAll(string keyword = "") |
| 135 | { |
| 136 | var notes = new List<(int, string, string)>(); |
| 137 | using (SqliteConnection conn = new SqliteConnection(mConnectionString)) |
| 138 | { |
| 139 | conn.Open(); |
| 140 | SqliteCommand com = conn.CreateCommand(); |
| 141 | if (string.IsNullOrWhiteSpace(keyword)) |
| 142 | { |
| 143 | com.CommandText = @" |
| 144 | SELECT Id, Name, Text FROM Notes |
| 145 | ORDER BY Name ASC"; |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | com.CommandText = @" |
| 150 | SELECT Id, Name, Text FROM Notes |
| 151 | WHERE Name LIKE @keyword |
| 152 | ORDER BY Name ASC"; |
| 153 | com.Parameters.AddWithValue("@keyword", $"%{keyword}%"); |
| 154 | } |
| 155 | |
| 156 | using (SqliteDataReader reader = com.ExecuteReader()) |
| 157 | { |
| 158 | while (reader.Read()) |
| 159 | { |
| 160 | notes.Add(( |
| 161 | reader.GetInt32(0), |
| 162 | reader.GetString(1), |
| 163 | reader.IsDBNull(2) ? string.Empty : reader.GetString(2) |
| 164 | )); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return notes; |
| 170 | } |
| 171 | } |
| 172 | } |