редакция дб для клиента, создала реализацию для чека

This commit is contained in:
Елена Бакальская 2024-05-02 19:58:22 +04:00
parent e2035800e3
commit 022e547b87
2 changed files with 86 additions and 2 deletions

View File

@ -0,0 +1,79 @@
using BeautySalonDBModels.Models;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BeautySalonDBModels.Implements
{
public class ChequeDB : AbstractWorkWithStorage<Cheque>
{
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();
}
}
}

View File

@ -38,7 +38,7 @@ namespace BeautySalonDBModels.Implements
var clients = new List<Client>();
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("SELECT * FROM clients order by id", conn);
using var cmd = new NpgsqlCommand("SELECT * FROM clients order by client_id", conn);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
@ -58,12 +58,17 @@ namespace BeautySalonDBModels.Implements
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("delete from clients where client_id = {id}", conn);
cmd.ExecuteNonQuery();
return client;
}
public override void Update(Client client)
{
using var conn = GetConnection();
conn.Open();
using var cmd = new NpgsqlCommand("update clients set fio = {client.FIO}, " +
"age = {client.Age} where client_id = {client.ClientId}", conn);
cmd.ExecuteNonQuery();
}
}
}