Удаление заявок и улучшение работы с БД
Добавлена обработка команды удаления заявки с помощью метода `DeleteReport`, который удаляет заявку из базы данных и отправляет уведомление пользователю. В интерфейсе добавлена кнопка "❌ Удалить заявку". Изменена команда вставки новой заявки для сохранения статуса как 'ожидает'. Также реализован метод `CreateDatabaseIfNotExists` для проверки и создания базы данных при необходимости.
This commit is contained in:
parent
cc8e4bf309
commit
0902395948
38
Program.cs
38
Program.cs
@ -170,6 +170,11 @@ class Program
|
|||||||
await ShowReportDetails(botClient, chatId, reportId, messageId);
|
await ShowReportDetails(botClient, chatId, reportId, messageId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (data != null && data.StartsWith("delete_"))
|
||||||
|
{
|
||||||
|
long reportId = long.Parse(data.Substring(7));
|
||||||
|
await DeleteReport(botClient, chatId, reportId);
|
||||||
|
}
|
||||||
else if (data == "back_to_list")
|
else if (data == "back_to_list")
|
||||||
{
|
{
|
||||||
await ViewReports(botClient, chatId);
|
await ViewReports(botClient, chatId);
|
||||||
@ -273,6 +278,29 @@ class Program
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static async Task DeleteReport(ITelegramBotClient botClient, long chatId, long reportId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var connection = new SqliteConnection("Data Source=bot.db"))
|
||||||
|
{
|
||||||
|
await connection.OpenAsync();
|
||||||
|
var command = connection.CreateCommand();
|
||||||
|
command.CommandText = "DELETE FROM Reports WHERE Id = @id";
|
||||||
|
command.Parameters.AddWithValue("@id", reportId);
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
|
|
||||||
|
await botClient.SendMessage(chatId, $"Заявка #{reportId} успешно удалена.");
|
||||||
|
Log.Information($"Заявка #{reportId} удалена пользователем {chatId}.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Log.Error($"Ошибка при удалении заявки #{reportId}: {ex.Message}");
|
||||||
|
await botClient.SendMessage(chatId, $"Ошибка при удалении заявки #{reportId}.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -375,6 +403,10 @@ class Program
|
|||||||
InlineKeyboardButton.WithCallbackData("🟢 Закрыта", $"status_{reportId}_закрыта")
|
InlineKeyboardButton.WithCallbackData("🟢 Закрыта", $"status_{reportId}_закрыта")
|
||||||
},
|
},
|
||||||
new[]
|
new[]
|
||||||
|
{
|
||||||
|
InlineKeyboardButton.WithCallbackData("❌ Удалить заявку", $"delete_{reportId}")
|
||||||
|
},
|
||||||
|
new[]
|
||||||
{
|
{
|
||||||
InlineKeyboardButton.WithCallbackData("Назад", "back_to_list"),
|
InlineKeyboardButton.WithCallbackData("Назад", "back_to_list"),
|
||||||
InlineKeyboardButton.WithCallbackData("Главное меню", "main_menu")
|
InlineKeyboardButton.WithCallbackData("Главное меню", "main_menu")
|
||||||
@ -388,7 +420,6 @@ class Program
|
|||||||
text: newText,
|
text: newText,
|
||||||
replyMarkup: statusButtons
|
replyMarkup: statusButtons
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -436,8 +467,8 @@ class Program
|
|||||||
var insertCommand = connection.CreateCommand();
|
var insertCommand = connection.CreateCommand();
|
||||||
insertCommand.CommandText =
|
insertCommand.CommandText =
|
||||||
@"
|
@"
|
||||||
INSERT INTO Reports (ChatId, Description)
|
INSERT INTO Reports (ChatId, Description, Status)
|
||||||
VALUES (@ChatId, @Description);
|
VALUES (@ChatId, @Description, 'ожидает');
|
||||||
";
|
";
|
||||||
insertCommand.Parameters.AddWithValue("@ChatId", chatId);
|
insertCommand.Parameters.AddWithValue("@ChatId", chatId);
|
||||||
insertCommand.Parameters.AddWithValue("@Description", description);
|
insertCommand.Parameters.AddWithValue("@Description", description);
|
||||||
@ -452,6 +483,7 @@ class Program
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static async Task CreateDatabaseIfNotExists()
|
private static async Task CreateDatabaseIfNotExists()
|
||||||
{
|
{
|
||||||
string connectionString = "Data Source=bot.db"; // Путь к вашей базе данных
|
string connectionString = "Data Source=bot.db"; // Путь к вашей базе данных
|
||||||
|
Loading…
x
Reference in New Issue
Block a user