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 { 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(); } } }