Skip to content

Instantly share code, notes, and snippets.

@Velsimir
Last active October 29, 2023 10:39
Show Gist options
  • Save Velsimir/89a58276e03d7b93a91ad2cd5b5ece4f to your computer and use it in GitHub Desktop.
Save Velsimir/89a58276e03d7b93a91ad2cd5b5ece4f to your computer and use it in GitHub Desktop.
using System.Text;
using System.Data.SQLite;
static void Main(string[] args)
{
}
class Model
{
public bool CanVote(DataTable dataTable)
{
if (dataTable == null)
{
throw new ArgumentNullException(nameof(dataTable));
}
if (dataTable.Rows.Count == 0)
{
throw new InvalidDataException(nameof (dataTable.Rows));
}
if (dataTable.Rows[0].ItemArray.Length < 2)
{
throw new InvalidDataException(dataTable.Rows[0].ItemArray.ToString());
}
return Convert.ToBoolean(dataTable.Rows[0].ItemArray[1]);
}
}
class Controller
{
private Model _model = new Model();
private Base _base = new Base();
private IView _view;
public Controller(IView view)
{
_view = view;
}
public void HandlePassport(string passportText)
{
try
{
if (_model.CanVote(_base.RequestData(passportText)) == false)
_view.ShowResult($"По паспорту « {passportText} » доступ к бюллетеню на дистанционном электронном голосовании ПРЕДОСТАВЛЕН");
else
_view.ShowResult($"По паспорту « {passportText} » доступ к бюллетеню на дистанционном электронном голосовании НЕ ПРЕДОСТАВЛЕН");
}
catch (InvalidDataException)
{
_view.ShowResult($"Паспорт «{passportText}» в списке участников дистанционного голосования НЕ НАЙДЕН");
}
catch (ArgumentNullException)
{
_view.ShowResult("Неверный формат серии или номера паспорта");
}
catch (SQLiteException)
{
_view.ShowMessage("Файл db.sqlite не найден. Положите файл в папку вместе с exe.");
}
}
}
class View : IView
{
private Controller _controller;
private MessageBox _messageBox;
private TextBox _resultTextBox;
private TextBox _passportTextBox;
public View()
{
_controller = new Controller(this);
}
public void OnCheckButtonClick(object sender, EventArgs e)
{
_controller.HandlePassport(_passportTextBox.Text);
}
public void ShowMessage(string message)
{
_messageBox.Show(message);
}
public void ShowResult(string result)
{
_resultTextBox.Text = result;
}
}
interface IView
{
void ShowMessage(string message);
void ShowResult(string result);
}
class Base
{
string commandText;
string connectionString = string.Format("Data Source=" + Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\db.sqlite");
public DataTable RequestData(string rawData)
{
commandText = "select * from passports where num='{0}' limit 1;" + Hash.ComputeSha256Hash(rawData);
SQLiteConnection connection = new SQLiteConnection(connectionString);
connection.Open();
SQLiteDataAdapter sqLiteDataAdapter = new SQLiteDataAdapter(new SQLiteCommand(commandText, connection));
DataTable dataTable = new DataTable();
sqLiteDataAdapter.Fill(dataTable);
return dataTable;
}
}
static class Hash
{
public static string ComputeSha256Hash(string rawData)
{
string hash = String.Empty;
using (SHA256 sha256 = SHA256.Create())
{
byte[] hashValue = sha256.ComputeHash(Encoding.UTF8.GetBytes(rawData));
foreach (byte b in hashValue)
{
hash += $"{b:X2}";
}
}
return hash;
}
}
public class TextBox
{
public string Text { get; set; }
}
public class MessageBox
{
public bool Show(string message)
{
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment