Last active
April 16, 2024 20:19
-
-
Save Diablo9818/554b03b85b9faf4305605c54fc5d7f02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Data; | |
using System.Reflection; | |
using System.Runtime.Serialization; | |
public interface IView | |
{ | |
public void SetResult(string result); | |
public void ShowMessage(string message); | |
} | |
public class View : IView | |
{ | |
private string _textResult; | |
private Presenter _presenter; | |
public View(Presenter presenter) | |
{ | |
_presenter = presenter ?? throw new NullReferenceException(); | |
} | |
public void SetResult(string result) | |
{ | |
_textResult = result; | |
} | |
public void ShowMessage(string message) | |
{ | |
MessageBox.Show(message); | |
} | |
private void CheckButtonClick(object sender, EventArgs e) | |
{ | |
_presenter.HandlePassport(_passportTextbox.Text); | |
} | |
} | |
public class Presenter | |
{ | |
private IView _view; | |
private CitizenService _citizenService; | |
public Presenter(IView view, CitizenService citizenService) | |
{ | |
_view = view ?? throw new NullReferenceException(); | |
_citizenService = citizenService ?? throw new ArgumentNullException(); | |
} | |
public void HandlePassport(string passportNumber) | |
{ | |
try | |
{ | |
Passport passport = new Passport(passportNumber); | |
Citizen citizen = _citizenService.FindBy(passport); | |
if (citizen.IsVoted) | |
{ | |
_view.SetResult("По паспорту«" + citizen.Passport.SerialNumber + "» доступ к бюллетеню на дистанционном электронном голосовании ПРЕДОСТАВЛЕН"); | |
} | |
else | |
{ | |
_view.SetResult("По паспорт «" + citizen.Passport.SerialNumber + "» доступ к бюллетеню на дистанционном электронном голосовании НЕ ПРЕДОСТАВЛЯЛСЯ"); | |
} | |
} | |
catch (InvalidOperationException exception) | |
{ | |
_view.SetResult("Неверный формат серии или номера паспорта"); | |
} | |
catch (PassportFindExeption) | |
{ | |
_view.SetResult("Паспорт «" + passportNumber + "» в списке участников дистанционного голосования НЕ НАЙДЕН"); | |
} | |
catch (ArgumentException exception) | |
{ | |
_view.ShowMessage("Введите серию и номер паспорта"); | |
} | |
catch (SQLiteException exception) | |
{ | |
if (exception.ErrorCode != 1) | |
{ | |
return; | |
} | |
_view.ShowMessage("Файл db.sqlite не найден. Положите файл в папку вместе с exe."); | |
} | |
} | |
} | |
[Serializable] | |
internal class SQLiteException : Exception | |
{ | |
public SQLiteException() | |
{ | |
} | |
public SQLiteException(string? message) : base(message) | |
{ | |
} | |
public SQLiteException(string? message, Exception? innerException) : base(message, innerException) | |
{ | |
} | |
protected SQLiteException(SerializationInfo info, StreamingContext context) : base(info, context) | |
{ | |
} | |
} | |
public class Citizen | |
{ | |
public Citizen(Passport passport, DataTable dataTable) | |
{ | |
Passport = passport ?? throw new ArgumentNullException(); | |
IsVoted = Convert.ToBoolean(dataTable.Rows[0].ItemArray[1]); | |
} | |
public bool IsVoted { get; private set; } | |
public Passport Passport { get; } | |
} | |
public class CitizenService | |
{ | |
private DataBaseRepository _dataBaseRepository; | |
public CitizenService(DataBaseRepository dataBaseRepository) | |
{ | |
_dataBaseRepository = dataBaseRepository ?? throw new ArgumentNullException(); | |
} | |
public Citizen FindBy(Passport passport) | |
{ | |
DataTable dataTable = _dataBaseRepository.SendRequest(passport.SerialNumber); | |
return new Citizen(passport, dataTable); | |
} | |
} | |
public class Passport | |
{ | |
private int maxLengthRawData = 10; | |
public Passport(string serialNumber) | |
{ | |
if (string.IsNullOrWhiteSpace(serialNumber.Trim())) | |
{ | |
throw new ArgumentException("Invalid operation"); | |
} | |
SerialNumber = serialNumber.Trim().Replace(" ", string.Empty); | |
if (SerialNumber.Length < maxLengthRawData) | |
{ | |
throw new InvalidOperationException("Wrong format!"); | |
} | |
} | |
public string SerialNumber { get; } | |
} | |
public class DataBaseRepository | |
{ | |
public DataTable SendRequest(string passportNumber) | |
{ | |
string commandText = string.Format("select * from passports where num='{0}' limit 1;", (object)Form1.ComputeSha256Hash(passportNumber)); | |
string connectionString = string.Format("Data Source=" + Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\db.sqlite"); | |
string passportText = passportNumber; | |
SQLiteConnection connection = new SQLiteConnection(connectionString); | |
connection.Open(); | |
SQLiteDataAdapter sqLiteDataAdapter = new SQLiteDataAdapter(new SQLiteCommand(commandText, connection)); | |
DataTable dataTable = new DataTable(); | |
sqLiteDataAdapter.Fill(dataTable); | |
connection.Close(); | |
return dataTable; | |
} | |
} | |
public class TextExeption: ArgumentException { } | |
public class PassportFindExeption: TextExeption { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment