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();
|
2024-05-14 23:08:28 +04:00
|
|
|
|
using var cmd = new NpgsqlCommand("INSERT INTO cheques (cheque_id, client_id, master_id, service_id, price, date_reception)" +
|
|
|
|
|
" VALUES (nextval('seq_cheque'), @ClientId, @master_id, @service_id, @Price, @DateReception)", conn);
|
2024-05-02 19:58:22 +04:00
|
|
|
|
cmd.Parameters.AddWithValue("@ClientId", cheque.ClientId);
|
2024-05-14 23:08:28 +04:00
|
|
|
|
cmd.Parameters.AddWithValue("@master_id", cheque.MasterId);
|
|
|
|
|
cmd.Parameters.AddWithValue("@service_id", cheque.ServiceId);
|
2024-05-08 17:43:52 +04:00
|
|
|
|
cmd.Parameters.AddWithValue("@Price", cheque.Price);
|
2024-05-14 23:08:28 +04:00
|
|
|
|
cmd.Parameters.AddWithValue("@DateReception", cheque.DateReception);
|
2024-05-02 19:58:22 +04:00
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Cheque? GetObject(int id)
|
|
|
|
|
{
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
2024-05-07 21:59:08 +04:00
|
|
|
|
using var cmd = new NpgsqlCommand("SELECT * FROM cheques WHERE cheque_id = @id", conn);
|
|
|
|
|
cmd.Parameters.AddWithValue("@id", id);
|
2024-05-02 19:58:22 +04:00
|
|
|
|
using var reader = cmd.ExecuteReader();
|
|
|
|
|
if (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
return new Cheque
|
|
|
|
|
{
|
|
|
|
|
ChequeId = reader.GetInt32(0),
|
|
|
|
|
ClientId = reader.GetInt32(1),
|
2024-05-14 23:08:28 +04:00
|
|
|
|
MasterId = reader.GetInt32(2),
|
|
|
|
|
ServiceId = reader.GetInt32(3),
|
|
|
|
|
Price = reader.GetDouble(4),
|
|
|
|
|
DateReception = reader.GetDateTime(5)
|
2024-05-02 19:58:22 +04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
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),
|
2024-05-14 23:08:28 +04:00
|
|
|
|
MasterId = reader.GetInt32(2),
|
|
|
|
|
ServiceId = reader.GetInt32(3),
|
|
|
|
|
Price = reader.GetDouble(4),
|
|
|
|
|
DateReception = reader.GetDateTime(5)
|
2024-05-02 19:58:22 +04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return cheques;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Cheque? Remove(int id)
|
|
|
|
|
{
|
|
|
|
|
var cheque = GetObject(id);
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
2024-05-07 21:59:08 +04:00
|
|
|
|
using var cmd = new NpgsqlCommand("delete from cheques where cheque_id = @id", conn);
|
|
|
|
|
cmd.Parameters.AddWithValue("@id", id);
|
2024-05-02 19:58:22 +04:00
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
return cheque;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Update(Cheque cheque)
|
|
|
|
|
{
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
2024-05-08 17:43:52 +04:00
|
|
|
|
using var cmd = new NpgsqlCommand("update cheques set client_id = @ClientId, " +
|
2024-05-14 23:08:28 +04:00
|
|
|
|
"master_id = @MasterId, service_id = @ServiceId, price = @Price, date_reception = @DateReception where cheque_id = @ChequeId", conn);
|
2024-05-07 21:59:08 +04:00
|
|
|
|
cmd.Parameters.AddWithValue("@ClientId", cheque.ClientId);
|
2024-05-14 23:08:28 +04:00
|
|
|
|
cmd.Parameters.AddWithValue("@MasterId", cheque.MasterId);
|
|
|
|
|
cmd.Parameters.AddWithValue("@ServiceId", cheque.ServiceId);
|
2024-05-07 21:59:08 +04:00
|
|
|
|
cmd.Parameters.AddWithValue("@ChequeId", cheque.ChequeId);
|
2024-05-08 17:43:52 +04:00
|
|
|
|
cmd.Parameters.AddWithValue("@Price", cheque.Price);
|
2024-05-14 23:08:28 +04:00
|
|
|
|
cmd.Parameters.AddWithValue("@DateReception", cheque.DateReception);
|
2024-05-02 19:58:22 +04:00
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
2024-05-08 22:57:40 +04:00
|
|
|
|
|
|
|
|
|
public string GetClientFIO(Cheque cheque)
|
|
|
|
|
{
|
|
|
|
|
ClientDatabase db = new ClientDatabase();
|
|
|
|
|
|
|
|
|
|
int clientId = cheque.ClientId;
|
|
|
|
|
|
|
|
|
|
List<Client> clients = db.GetObjects();
|
|
|
|
|
|
|
|
|
|
foreach (Client c in clients)
|
|
|
|
|
{
|
|
|
|
|
if (c.ClientId == clientId) return c.FIO.ToString();
|
|
|
|
|
}
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
2024-05-14 23:08:28 +04:00
|
|
|
|
|
|
|
|
|
public string GetMasterFIO(Cheque cheque)
|
|
|
|
|
{
|
|
|
|
|
MasterDatabase db = new MasterDatabase();
|
|
|
|
|
|
|
|
|
|
int masterId = cheque.MasterId;
|
|
|
|
|
|
|
|
|
|
List<Master> masters = db.GetObjects();
|
|
|
|
|
|
|
|
|
|
foreach (Master c in masters)
|
|
|
|
|
{
|
|
|
|
|
if (c.MasterId == masterId) return c.FIO.ToString();
|
|
|
|
|
}
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetServiceName(Cheque cheque)
|
|
|
|
|
{
|
|
|
|
|
ServiceDatabase db = new ServiceDatabase();
|
|
|
|
|
|
|
|
|
|
int serviceId = cheque.ServiceId;
|
|
|
|
|
|
|
|
|
|
List<Service> service = db.GetObjects();
|
|
|
|
|
|
|
|
|
|
foreach (Service s in service)
|
|
|
|
|
{
|
|
|
|
|
if (s.ServiceId == serviceId) return s.ServiceName.ToString();
|
|
|
|
|
}
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
2024-05-02 19:58:22 +04:00
|
|
|
|
}
|
|
|
|
|
}
|