добавляет класс Logs
This commit is contained in:
parent
a38dfe2308
commit
7f9ca4200e
@ -79,6 +79,7 @@
|
||||
<Compile Include="Authorization.cs" />
|
||||
<Compile Include="ExtactEmailsAlgorithm.cs" />
|
||||
<Compile Include="Globals.cs" />
|
||||
<Compile Include="Logs.cs" />
|
||||
<Compile Include="MainForm.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
|
58
Globals.cs
58
Globals.cs
@ -13,6 +13,7 @@ namespace AwesomeEmailExtractor
|
||||
{
|
||||
// Getter and setter for SQLite database connection
|
||||
public static SqliteConnection db { get; set; }
|
||||
public static SqliteConnection logsDb { get; set; }
|
||||
|
||||
public static string getAppDirectory()
|
||||
{
|
||||
@ -32,7 +33,44 @@ namespace AwesomeEmailExtractor
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public static string getDefaultPathAppLogs()
|
||||
{
|
||||
return Path.Combine(
|
||||
getAppDirectory(),
|
||||
"logs.db"
|
||||
);
|
||||
}
|
||||
|
||||
public static string getPathAppLogs()
|
||||
{
|
||||
SqliteCommand command = new SqliteCommand();
|
||||
command.Connection = db;
|
||||
command.CommandText = "SELECT logs_db_path FROM app_settings LIMIT 1";
|
||||
|
||||
SqliteDataReader reader = command.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
return reader["logs_db_path"].ToString();
|
||||
}
|
||||
|
||||
return getDefaultPathAppLogs();
|
||||
}
|
||||
|
||||
public static void CreateLogsTable()
|
||||
{
|
||||
SqliteCommand command = new SqliteCommand();
|
||||
command.Connection = logsDb;
|
||||
|
||||
command.CommandText = "CREATE TABLE IF NOT EXISTS logs_actions (id INTEGER PRIMARY KEY, name TEXT NOT NULL)";
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
command.CommandText = "INSERT OR IGNORE INTO logs_actions (id, name) VALUES (0, 'Выполнение');";
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
command.CommandText = "CREATE TABLE IF NOT EXISTS logs (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL, date TEXT NOT NULL, action INTEGER NOT NULL, message TEXT NOT NULL, FOREIGN KEY(action) REFERENCES logs_actions(id));";
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
}
|
||||
|
||||
public static void CreateTables()
|
||||
{
|
||||
@ -44,7 +82,11 @@ namespace AwesomeEmailExtractor
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
// Добавить роли
|
||||
command.CommandText = "INSERT OR IGNORE INTO roles (id, name) VALUES (0, 'DEFAULT'), (1, 'ADMIN');";
|
||||
command.CommandText = "INSERT OR IGNORE INTO roles (id, name) VALUES (0, 'Обычный'), (1, 'Администратор');";
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
// Создать таблицу для хранения настроек (знаю, так плохо, но сойдет)
|
||||
command.CommandText = "CREATE TABLE IF NOT EXISTS app_settings (logs_db_path TEXT);";
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
// Создать таблицу для хранения пользователей
|
||||
@ -63,6 +105,18 @@ namespace AwesomeEmailExtractor
|
||||
command.ExecuteNonQuery();
|
||||
command.Parameters.Clear();
|
||||
}
|
||||
|
||||
command.CommandText = "SELECT COUNT(*) FROM app_settings";
|
||||
|
||||
if (Convert.ToInt32(command.ExecuteScalar()) == 0)
|
||||
{
|
||||
command.CommandText = "INSERT INTO app_settings (logs_db_path) VALUES (@logs_db_path);";
|
||||
|
||||
SqliteParameter logsDbPathParam = new SqliteParameter("@logs_db_path", getDefaultPathAppLogs());
|
||||
command.Parameters.Add(logsDbPathParam);
|
||||
command.ExecuteNonQuery();
|
||||
command.Parameters.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
109
Logs.cs
Normal file
109
Logs.cs
Normal file
@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Data.Sqlite;
|
||||
|
||||
namespace AwesomeEmailExtractor
|
||||
{
|
||||
public class Logs
|
||||
{
|
||||
public class LogData {
|
||||
public User user;
|
||||
public string date;
|
||||
public Action action;
|
||||
public string message;
|
||||
}
|
||||
|
||||
public enum Action
|
||||
{
|
||||
Execute,
|
||||
}
|
||||
|
||||
public static void Log(User user, Action action, Dictionary<string, object> options)
|
||||
{
|
||||
SqliteCommand command = new SqliteCommand();
|
||||
command.Connection = Globals.logsDb;
|
||||
|
||||
command.CommandText = "INSERT INTO logs (user_id, date, action, message) VALUES (@user_id, strftime('%Y-%m-%d %H:%M:%S', datetime('now')), @action, @message)";
|
||||
|
||||
command.Parameters.AddWithValue("@user_id", user.ID);
|
||||
command.Parameters.AddWithValue("@action", action);
|
||||
command.Parameters.AddWithValue("@message", GetLogMessage(action, options));
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public static List<LogData> GetLogs(User user)
|
||||
{
|
||||
SqliteCommand command = new SqliteCommand();
|
||||
command.Connection = Globals.logsDb;
|
||||
|
||||
command.CommandText = "SELECT date, action, message FROM logs WHERE user_id = @user_id ORDER BY date DESC";
|
||||
command.Parameters.AddWithValue("@user_id", user.ID);
|
||||
|
||||
SqliteDataReader reader = command.ExecuteReader();
|
||||
|
||||
List<LogData> logs = new List<LogData>();
|
||||
while (reader.Read())
|
||||
{
|
||||
logs.Add(new LogData()
|
||||
{
|
||||
user = user,
|
||||
date = reader.GetString(0),
|
||||
action = (Action)reader.GetInt32(1),
|
||||
message = reader.GetString(2)
|
||||
});
|
||||
}
|
||||
|
||||
return logs;
|
||||
}
|
||||
|
||||
public static List<LogData> GetLogs()
|
||||
{
|
||||
SqliteCommand command = new SqliteCommand();
|
||||
command.Connection = Globals.logsDb;
|
||||
command.CommandText = "ATTACH DATABASE @dbpath AS appDB";
|
||||
command.Parameters.AddWithValue("@dbpath", Globals.getAppDatabase());
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
command.CommandText = "SELECT user_id, appDB.users.login, appDB.users.id as role_id, date, action, message FROM logs LEFT JOIN appDB.users ON logs.user_id = appDB.users.id ORDER BY date DESC ";
|
||||
|
||||
SqliteDataReader reader = command.ExecuteReader();
|
||||
|
||||
List<LogData> logs = new List<LogData>();
|
||||
while (reader.Read())
|
||||
{
|
||||
logs.Add(new LogData()
|
||||
{
|
||||
user = new User(reader.GetInt32(0), reader.GetString(1), (UserRoles)reader.GetInt32(2)),
|
||||
date = reader.GetString(3),
|
||||
action = (Action)reader.GetInt32(4),
|
||||
message = reader.GetString(5)
|
||||
});
|
||||
}
|
||||
|
||||
return logs;
|
||||
}
|
||||
|
||||
public static string GetLogMessage(Action action, Dictionary<string, object> options)
|
||||
{
|
||||
if (action == Action.Execute)
|
||||
{
|
||||
string sourceText = (string)options["sourceText"];
|
||||
int count = (int)options["count"];
|
||||
List<string> uniqueEmails = options["uniqueEmails"] as List<string>;
|
||||
|
||||
|
||||
return $"Пользователь выполнил поиск email-ов c таким исходным текстом: [ {sourceText}. ]\n" +
|
||||
$"Найдено {count} email-ов.\n" +
|
||||
$"Список уникальных: {String.Join(", ", uniqueEmails)}.";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
23
MainForm.cs
23
MainForm.cs
@ -16,9 +16,18 @@ namespace AwesomeEmailExtractor
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
|
||||
private void executeButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
// Получаем исходный текст из sourceRichTextBox
|
||||
string sourceText = sourceRichTextBox.Text;
|
||||
|
||||
if (sourceText.Length == 0)
|
||||
{
|
||||
MessageBox.Show("Введите текст в поле исходного текста", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
// Чистим предыдущий результат
|
||||
toolStripStatusLabel.Text = "";
|
||||
resultCountLabel.Text = "";
|
||||
@ -27,9 +36,6 @@ namespace AwesomeEmailExtractor
|
||||
// Объявляем список уникальных e-mail-ов
|
||||
List<string> uniqueEmails = new List<string>();
|
||||
|
||||
// Получаем исходный текст из sourceRichTextBox
|
||||
string sourceText = sourceRichTextBox.Text;
|
||||
|
||||
// Вызываем метод для извлечения e-mail-ов
|
||||
int count = ExtactEmailsAlgorithm.Extract(sourceText, out uniqueEmails);
|
||||
|
||||
@ -37,6 +43,15 @@ namespace AwesomeEmailExtractor
|
||||
toolStripStatusLabel.Text = "Успех!";
|
||||
resultCountLabel.Text = $"Количество e-mail-ов в тексте: {count}";
|
||||
uniqueListBox.DataSource = uniqueEmails;
|
||||
|
||||
Logs.Log(
|
||||
new User(1, "", UserRoles.ADMIN),
|
||||
Logs.Action.Execute,
|
||||
new Dictionary<string, object>() {
|
||||
{ "sourceText", sourceText },
|
||||
{ "count", count },
|
||||
{ "uniqueEmails", uniqueEmails }
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
13
Program.cs
13
Program.cs
@ -18,11 +18,7 @@ namespace AwesomeEmailExtractor
|
||||
{
|
||||
preMain();
|
||||
|
||||
User user = new User(1, "admin", UserRoles.ADMIN);
|
||||
AdminUtils admin = new AdminUtils(user);
|
||||
|
||||
|
||||
|
||||
Logs.GetLogs();
|
||||
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
@ -37,11 +33,16 @@ namespace AwesomeEmailExtractor
|
||||
{
|
||||
Directory.CreateDirectory(Globals.getAppDirectory());
|
||||
}
|
||||
|
||||
|
||||
Globals.db = new SqliteConnection("Data Source=" + Globals.getAppDatabase());
|
||||
Globals.db.Open();
|
||||
|
||||
Globals.CreateTables();
|
||||
|
||||
Globals.logsDb = new SqliteConnection("Data Source=" + Globals.getPathAppLogs());
|
||||
Globals.logsDb.Open();
|
||||
|
||||
Globals.CreateLogsTable();
|
||||
}
|
||||
|
||||
static void postMain()
|
||||
|
Reference in New Issue
Block a user