-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathDatabaseService.cs
More file actions
256 lines (203 loc) · 8.49 KB
/
Copy pathDatabaseService.cs
File metadata and controls
256 lines (203 loc) · 8.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System.Data;
using Dapper;
using iGotify_Notification_Assist.Models;
using Microsoft.Data.Sqlite;
namespace iGotify_Notification_Assist.Services;
public static class DatabaseService
{
/// <summary>
/// Create date if not exists
/// </summary>
/// <param name="path">db path</param>
/// <returns></returns>
public static bool CreateDatebase(string path)
{
//Create Database File
var pathToDb = Path.Combine(path, "users.db");
var isDbFileExists = File.Exists(pathToDb);
if (isDbFileExists) return true;
var connectionString = GetConnectionString.UsersDb(pathToDb);
using var dbConnection = new SqliteConnection(connectionString);
dbConnection.Open();
// Create a sample table
const string createTableQuery =
"create table if not exists Users (Uid integer primary key, ClientToken text not null, DeviceToken text not null, GotifyUrl text not null, Headers text not null);";
dbConnection.Execute(createTableQuery);
// Perform other database operations as needed
// Close the connection when done
dbConnection.Close();
return true;
}
/// <summary>
/// Update database
/// </summary>
/// <param name="path">db path</param>
/// <param name="tableName">table which you will update</param>
/// <param name="columnName">column that will be added</param>
/// <param name="columnDefinition">column values e.g. text not null default ''</param>
/// <returns></returns>
public static bool UpdateDatebase(string path, string tableName, string columnName, string columnDefinition)
{
var pathToDb = Path.Combine(path, "users.db");
var isDbFileExists = File.Exists(pathToDb);
var isExists = false;
if (!isDbFileExists) return isExists;
using var dbConnection = new SqliteConnection(GetConnectionString.UsersDb(pathToDb));
dbConnection.Open();
// 1. Spalteninformationen abfragen
var columns = dbConnection.Query<PragmaTableInfo>(
$"PRAGMA table_info({tableName});");
bool exists = columns.Any(c => string.Equals(c.Name, columnName, StringComparison.OrdinalIgnoreCase));
// 2. Falls Spalte fehlt → hinzufügen
if (!exists)
{
string sql = $"ALTER TABLE {tableName} ADD COLUMN {columnName} {columnDefinition};";
dbConnection.Execute(sql);
}
return true;
}
/// <summary>
/// check if user or instance exists
/// </summary>
/// <param name="dm"></param>
/// <returns></returns>
public static async Task<bool> CheckIfUserExists(DeviceModel dm)
{
var path = $"{GetLocationsOf.App}/data";
//Create Database File
var pathToDb = Path.Combine(path, "users.db");
var isDbFileExists = File.Exists(pathToDb);
var isExists = false;
if (!isDbFileExists) return isExists;
await using var dbConnection = new SqliteConnection(GetConnectionString.UsersDb(pathToDb));
dbConnection.Open();
// Create a sample table
var selectQuery = $"select count(*) from Users u where u.ClientToken = '{dm.ClientToken}';";
var id = await dbConnection.ExecuteScalarAsync<int>(selectQuery);
isExists = id > 0;
// Perform other database operations as needed
// Close the connection when done
dbConnection.Close();
return isExists;
}
/// <summary>
/// Delete user or instance by token
/// </summary>
/// <param name="clientToken">Clienttoken for verify the correct entry</param>
/// <returns></returns>
public static async Task<bool> DeleteUser(string clientToken)
{
var path = $"{GetLocationsOf.App}/data";
//Create Database File
var pathToDb = Path.Combine(path, "users.db");
var isDbFileExists = File.Exists(pathToDb);
var isDeleted = false;
if (!isDbFileExists) return isDeleted;
await using var dbConnection = new SqliteConnection(GetConnectionString.UsersDb(pathToDb));
dbConnection.Open();
// Create a sample table
var deleteQuery = $"delete from Users where ClientToken = '{clientToken}';";
var rows = await dbConnection.ExecuteAsync(deleteQuery);
isDeleted = rows > 0;
// Perform other database operations as needed
// Close the connection when done
dbConnection.Close();
return isDeleted;
}
/// <summary>
/// Update User to add custom header
/// </summary>
/// <param name="usr"></param>
/// <returns></returns>
public static async Task<bool> UpdateUser(Users usr)
{
var path = $"{GetLocationsOf.App}/data";
//Create Database File
var pathToDb = Path.Combine(path, "users.db");
var isDbFileExists = File.Exists(pathToDb);
var isUpdated = false;
if (!isDbFileExists) return isUpdated;
await using var dbConnection = new SqliteConnection(GetConnectionString.UsersDb(pathToDb));
dbConnection.Open();
// Create a sample table
var updateQuery = $"UPDATE Users SET Headers = '{usr.Headers}' WHERE Uid = {usr.Uid};";
var rows = await dbConnection.ExecuteAsync(updateQuery);
isUpdated = rows > 0;
// Perform other database operations as needed
// Close the connection when done
dbConnection.Close();
return isUpdated;
}
/// <summary>
/// Add a new entry
/// </summary>
/// <param name="dm"></param>
/// <returns></returns>
public static async Task<bool> InsertUser(DeviceModel dm)
{
var path = $"{GetLocationsOf.App}/data";
//Create Database File
var pathToDb = Path.Combine(path, "users.db");
var isDbFileExists = File.Exists(pathToDb);
var inserted = false;
if (!isDbFileExists) return inserted;
await using var dbConnection = new SqliteConnection(GetConnectionString.UsersDb(pathToDb));
dbConnection.Open();
// Create a sample table
var insertQuery =
$"insert into Users (ClientToken, DeviceToken, GotifyUrl, Headers) values ('{dm.ClientToken}', '{dm.DeviceToken}', '{dm.GotifyUrl}', '');";
var id = await dbConnection.ExecuteAsync(insertQuery);
inserted = id > 0;
// Perform other database operations as needed
// Close the connection when done
dbConnection.Close();
return inserted;
}
/// <summary>
/// Get single User or instance by token
/// </summary>
/// <param name="clientToken">Clienttoken for verify the correct entry</param>
/// <returns></returns>
public static async Task<Users> GetUser(string clientToken)
{
var usr = new Users();
var path = $"{GetLocationsOf.App}/data";
//Create Database File
var pathToDb = Path.Combine(path, "users.db");
var isDbFileExists = File.Exists(pathToDb);
if (!isDbFileExists) return usr;
await using var dbConnection = new SqliteConnection(GetConnectionString.UsersDb(pathToDb));
dbConnection.Open();
// Create a sample table
var selectUserQuery = $"SELECT * FROM Users u where u.ClientToken = '{clientToken}';";
if (clientToken.Contains("NTFY-DEVICE-"))
selectUserQuery = $"SELECT * FROM Users u where u.DeviceToken = '{clientToken}';";
usr = (await dbConnection.QueryAsync<Users>(selectUserQuery)).ToList().FirstOrDefault() ?? usr;
// Perform other database operations as needed
// Close the connection when done
dbConnection.Close();
return usr;
}
/// <summary>
/// Get all Users or instances in the database
/// </summary>
/// <returns></returns>
public static async Task<List<Users>> GetUsers()
{
var userList = new List<Users>();
var path = $"{GetLocationsOf.App}/data";
//Create Database File
var pathToDb = Path.Combine(path, "users.db");
var isDbFileExists = File.Exists(pathToDb);
if (!isDbFileExists) return userList;
await using var dbConnection = new SqliteConnection(GetConnectionString.UsersDb(pathToDb));
dbConnection.Open();
// Create a sample table
const string selectAllQuery = "SELECT * FROM Users u;";
userList = (await dbConnection.QueryAsync<Users>(selectAllQuery)).ToList();
// Perform other database operations as needed
// Close the connection when done
dbConnection.Close();
return userList;
}
}