Files
PaymentLogs/paymentLogs/Repositories/PaymentRepo.cs
T
2024-06-07 12:06:02 +03:00

59 lines
1.9 KiB
C#

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")
};
_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`) " +
$"VALUES ('{payment.Id}','{payment.FCs}', '{payment.CourseId}', '{payment.CoursePrice}', '{payment.Paid}') " +
$"ON DUPLICATE KEY UPDATE `fcs`='{payment.FCs}', `course_id`='{payment.CourseId}', `course_price` = '{payment.CoursePrice}', " +
$"`paid` = '{payment.Paid}'";
MySqlCommand cmd = new MySqlCommand(sql, db.getConnection());
cmd.ExecuteNonQuery();
}
db.closeConnection();
}
}
}