Setup and forms

This commit is contained in:
redtea committed 2024-06-04 21:09:56 +03:00
1 parent aed55f44f9
commit eee7548fc8
47 files changed
+2837 -8138

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
using MySql.Data.MySqlClient;
using PaymentLogs.Domain;
using PaymentLogs.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
namespace PaymentLogs.Repositories
{
internal class CourseRepo : Repo<int, Course>
{
public override void Load()
{
DataBase db = new DataBase();
db.openConnection();
_data.Clear();
string sql = " SELECT * FROM courses ";
MySqlCommand cmd = new MySqlCommand(sql, db.getConnection());
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Course course = new Course() {
Id = reader.GetInt32("id"),
Name = reader.GetString("name"),
Type = reader.GetString("type"),
Cost = reader.GetInt32("cost"),
Duration = reader.GetInt32("duration"),
StudentsCount = reader.GetInt32("students_count")
};
_data[course.Id] = course;
}
db.closeConnection();
}
public override void Save()
{
foreach(Course course in All())
{
DataBase db = new DataBase();
db.openConnection();
string sql = $" INSERT INTO `courses` (`id`, `name`, `type`, `cost`, `duration`, `students_count`) VALUES ('{course.Id}'," +
$"'{course.Name}', '{course.Type}', '{course.Cost}', '{course.Duration}', '{course.StudentsCount}') " +
$"ON DUPLICATE KEY UPDATE `name`='{course.Name}', `type`='{course.Type}', `cost`='{course.Cost}', `duration`='{course.Duration}'," +
$"`students_count`='{course.StudentsCount}' ";
MySqlCommand cmd = new MySqlCommand(sql, db.getConnection());
cmd.ExecuteNonQuery();
db.closeConnection();
}
}
public Course ByName(string name)
{
foreach (var course in _data.Values)
{
if (course.Name.ToLower().Equals(name.ToLower()))
{
return course;
}
}
return _data.Values.FirstOrDefault();
}
}
}
+59
View File
@@ -0,0 +1,59 @@
using MySql.Data.MySqlClient;
using PaymentLogs.Domain;
using PaymentLogs.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PaymentLogs.Repositories
{
internal class PaymentRepo : Repo<int, Payment>
{
public override void Load()
{
DataBase db = new DataBase();
db.openConnection();
_data.Clear();
string sql = " SELECT * FROM payments ";
MySqlCommand cmd = new MySqlCommand(sql, db.getConnection());
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Payment payment = new Payment()
{
Id = reader.GetInt32("id"),
FCs = reader.GetString("fcs"),
CourseId = reader.GetInt32("course_id"),
CoursePrice = reader.GetInt32("course_price"),
Paid = reader.GetInt32("paid"),
Debt = reader.GetInt32("debt")
};
_data[payment.Id] = payment;
}
db.closeConnection();
}
public override void Save()
{
DataBase db = new DataBase();
db.openConnection();
foreach(Payment payment in _data.Values)
{
string sql = $" INSERT INTO `payments` (`id`, `fcs`, `course_id`, `course_price`, `paid`, `debt`) " +
$"VALUES ('{payment.Id}','{payment.FCs}', '{payment.CourseId}', '{payment.CoursePrice}', '{payment.Paid}', '{payment.Debt}') " +
$"ON DUPLICATE KEY UPDATE `fcs`='{payment.FCs}', `course_id`='{payment.CourseId}', `course_price` = '{payment.CoursePrice}', " +
$"`paid` = '{payment.Paid}', `debt`='{payment.Debt}'";
MySqlCommand cmd = new MySqlCommand(sql, db.getConnection());
cmd.ExecuteNonQuery();
}
db.closeConnection();
}
}
}
+26
View File
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PaymentLogs.Repositories
{
internal abstract class Repo<K, V>
{
protected Dictionary<K, V> _data = new Dictionary<K, V>();
public abstract void Load();
public virtual List<V> All()
{
return _data.Values.ToList();
}
public virtual V Get(K key) { return _data[key]; }
public virtual void Update(K key, V value)
{
_data[key] = value;
}
public abstract void Save();
}
}
+63
View File
@@ -0,0 +1,63 @@
using MySql.Data.MySqlClient;
using PaymentLogs.Domain;
using PaymentLogs.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
namespace PaymentLogs.Repositories
{
internal class StudentRepo : Repo<int, Student>
{
public override void Load()
{
DataBase db = new DataBase();
db.openConnection();
_data.Clear();
string sql = " SELECT * FROM students ";
MySqlCommand cmd = new MySqlCommand(sql, db.getConnection());
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Student student = new Student()
{
Id = reader.GetInt32("id"),
FCs = reader.GetString("fcs"),
dateOfBirth = DateTime.ParseExact(reader.GetString("date_of_birth"), "yyyy-MM-dd",null),
Address = reader.GetString("address"),
CourseId = reader.GetInt32("course_id"),
Paid = reader.GetInt32("paid")
};
_data[student.Id] = student;
}
db.closeConnection();
}
public override void Save()
{
DataBase db = new DataBase();
db.openConnection();
foreach (Student student in All())
{
string sql = $" INSERT INTO `students` (`id`, `fcs`, `date_of_birth`, `address`, `course_id`, `paid`) " +
$"VALUES ('{student.Id}','{student.FCs}','{ String.Format("{0:yyyy-MM-dd}", student.dateOfBirth) }', '{student.Address}'," +
$"'{student.CourseId}', '{student.Paid}') " +
$"ON DUPLICATE KEY UPDATE `fcs`='{student.FCs}', `date_of_birth`='{String.Format("{0:yyyy-MM-dd}", student.dateOfBirth)}'," +
$"`address`='{student.Address}', `course_id`='{student.CourseId}', `paid`='{student.Paid}'";
MySqlCommand cmd = new MySqlCommand(sql, db.getConnection());
cmd.ExecuteNonQuery();
}
db.closeConnection();
}
}
}
+69
View File
@@ -0,0 +1,69 @@
using MySql.Data.MySqlClient;
using PaymentLogs.Domain;
using PaymentLogs.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace PaymentLogs.Repositories
{
internal class UserRepo : Repo<string, User>
{
public override void Load()
{
DataBase db = new DataBase();
_data.Clear();
string sql = " SELECT * FROM usrs ";
db.openConnection();
MySqlCommand cmd = new MySqlCommand(sql, db.getConnection());
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read()) {
User user = new User() { Login = reader.GetString("login"), PasswordHash = StringUtils.ConvertToSecureString(reader.GetString("password_hash")) };
_data[user.Login] = user;
}
db.closeConnection();
}
public void RegisterUser(string _login, string _password)
{
User user = new User() { Login = _login.ToLower(), PasswordHash = StringUtils.ConvertToSecureString(StringUtils.SHA256_hash(_password)) };
Save(user);
}
public Boolean HasUser(string _login)
{
return _data.ContainsKey(_login.ToLower());
}
private void Save(User user)
{
DataBase db = new DataBase();
db.openConnection();
string hash = StringUtils.SecureStringToString(user.PasswordHash);
string sql = $" INSERT INTO `usrs` (`login`, `password_hash`) VALUES ('{user.Login}','{hash}') " +
$"ON DUPLICATE KEY UPDATE `password_hash`='{hash}'";
MySqlCommand cmd = new MySqlCommand(sql, db.getConnection());
cmd.ExecuteNonQuery();
db.closeConnection();
}
public override void Save()
{
foreach(User user in _data.Values)
{
Save(user);
}
}
}
}