91 lines
3.5 KiB
C#
91 lines
3.5 KiB
C#
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 ReceptionDatabase : AbstractWorkWithStorage<Reception>
|
|
{
|
|
public override void Add(Reception reception)
|
|
{
|
|
using var conn = GetConnection();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand("INSERT INTO receptions (reception_id, master_id, service_id, date_reception) " +
|
|
"VALUES ((nextval('seq_reception'), @MasterId, @ServiceId, @DateReception)", conn);
|
|
cmd.Parameters.AddWithValue("@MasterId", reception.MasterId);
|
|
cmd.Parameters.AddWithValue("@ServiceId", reception.ServiceId);
|
|
cmd.Parameters.AddWithValue("@DateReception", reception.DateReception);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
public override Reception? GetObject(int id)
|
|
{
|
|
using var conn = GetConnection();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand("SELECT * FROM receptions WHERE reception_id = @id", conn);
|
|
cmd.Parameters.AddWithValue("@id", id);
|
|
using var reader = cmd.ExecuteReader();
|
|
if (reader.Read())
|
|
{
|
|
return new Reception
|
|
{
|
|
ReceptionId = reader.GetInt32(0),
|
|
MasterId = reader.GetInt32(1),
|
|
ServiceId = reader.GetInt32(2),
|
|
DateReception = reader.GetDateTime(3)
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public override List<Reception> GetObjects()
|
|
{
|
|
var receptions = new List<Reception>();
|
|
using var conn = GetConnection();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand("SELECT * FROM receptions order by reception_id", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
receptions.Add(new Reception
|
|
{
|
|
ReceptionId = reader.GetInt32(0),
|
|
MasterId = reader.GetInt32(1),
|
|
ServiceId = reader.GetInt32(2),
|
|
DateReception = reader.GetDateTime(3)
|
|
});
|
|
}
|
|
return receptions;
|
|
}
|
|
|
|
public override Reception? Remove(int id)
|
|
{
|
|
var reception = GetObject(id);
|
|
using var conn = GetConnection();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand("delete from receptions where reception_id = @id", conn);
|
|
cmd.Parameters.AddWithValue("@id", id);
|
|
cmd.ExecuteNonQuery();
|
|
return reception;
|
|
}
|
|
|
|
public override void Update(Reception reception)
|
|
{
|
|
using var conn = GetConnection();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand("update reception set master_id = @MasterId, " +
|
|
"service_id = @ServiceId, date_reception = @DateReception where reception_id = @ReceptionId", conn);
|
|
cmd.Parameters.AddWithValue("@MasterId", reception.MasterId);
|
|
cmd.Parameters.AddWithValue("@ServiceId", reception.ServiceId);
|
|
cmd.Parameters.AddWithValue("@DateReception", reception.DateReception);
|
|
cmd.Parameters.AddWithValue("@ReceptionId", reception.ReceptionId);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|