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 { 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"), Cost = reader.GetInt32("cost"), StudentsCount = reader.GetInt32("students_count") }; _data[course.Id] = course; } db.closeConnection(); } public override void Save() { DataBase db = new DataBase(); db.openConnection(); foreach (Course course in All()) { string sql = $" INSERT INTO `courses` (`id`, `name`, `cost`, `students_count`) VALUES ('{course.Id}'," + $"'{course.Name}', '{course.Cost}', '{course.StudentsCount}') " + $"ON DUPLICATE KEY UPDATE `name`='{course.Name}', `cost`='{course.Cost}'," + $"`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(); } } }