2024-05-02 19:58:22 +04:00
|
|
|
|
using BeautySalonDBModels.Models;
|
|
|
|
|
using Npgsql;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BeautySalonDBModels.Implements
|
|
|
|
|
{
|
2024-05-06 23:40:35 +04:00
|
|
|
|
public class ChequeDatabase : AbstractWorkWithStorage<Cheque>
|
2024-05-02 19:58:22 +04:00
|
|
|
|
{
|
|
|
|
|
public override void Add(Cheque cheque)
|
|
|
|
|
{
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
|
|
|
|
using var cmd = new NpgsqlCommand("INSERT INTO cheques (client_id, reception_id) VALUES (@ClientId, @ReceptionId)", conn);
|
|
|
|
|
cmd.Parameters.AddWithValue("@ClientId", cheque.ClientId);
|
|
|
|
|
cmd.Parameters.AddWithValue("@ReceptionId", cheque.ReceptionId);
|
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Cheque? GetObject(int id)
|
|
|
|
|
{
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
|
|
|
|
using var cmd = new NpgsqlCommand("SELECT * FROM cheques WHERE cheque_id = {id}", conn);
|
|
|
|
|
using var reader = cmd.ExecuteReader();
|
|
|
|
|
if (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
return new Cheque
|
|
|
|
|
{
|
|
|
|
|
ChequeId = reader.GetInt32(0),
|
|
|
|
|
ClientId = reader.GetInt32(1),
|
|
|
|
|
ReceptionId = reader.GetInt32(2)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override List<Cheque> GetObjects()
|
|
|
|
|
{
|
|
|
|
|
var cheques = new List<Cheque>();
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
|
|
|
|
using var cmd = new NpgsqlCommand("SELECT * FROM cheques order by cheque_id", conn);
|
|
|
|
|
using var reader = cmd.ExecuteReader();
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
cheques.Add(new Cheque
|
|
|
|
|
{
|
|
|
|
|
ChequeId = reader.GetInt32(0),
|
|
|
|
|
ClientId = reader.GetInt32(1),
|
|
|
|
|
ReceptionId = reader.GetInt32(2)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return cheques;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Cheque? Remove(int id)
|
|
|
|
|
{
|
|
|
|
|
var cheque = GetObject(id);
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
|
|
|
|
using var cmd = new NpgsqlCommand("delete from cheques where cheque_id = {id}", conn);
|
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
return cheque;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Update(Cheque cheque)
|
|
|
|
|
{
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
|
|
|
|
using var cmd = new NpgsqlCommand("update cheque set client_id = {cheque.ClientId}, " +
|
|
|
|
|
"reception_id = {cheque.ReceptionId} where cheque_id = {cheque.ChequeId}", conn);
|
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|