PIbd-23_Firsov_KA_SUBD/Hotel/HotelDatabase/Implement/ServiceCheckImplement.cs
2024-05-29 14:28:53 +04:00

100 lines
4.1 KiB
C#

using HotelAbstractions.Logic;
using HotelAbstractions.Models;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HotelDatabase.Implement
{
public class ServiceCheckImplement : IServiceCheckLogic
{
public ServiceCheck? Create(ServiceCheck serviceCheck)
{
using var con = SqlConnection.GetConnection();
con.Open();
using var cmd = new NpgsqlCommand(
"INSERT INTO service_check (fk_reservation_id, fk_service_id, count, service_date)" +
" VALUES (@FKReservation_id, @FKService_id, @Count, @Service_date)",
con
);
cmd.Parameters.AddWithValue("@FKReservation_id", serviceCheck.ReservationId == 0 ? DBNull.Value : serviceCheck.ReservationId);
cmd.Parameters.AddWithValue("@FKService_id", serviceCheck.ServiceId == 0 ? DBNull.Value : serviceCheck.ServiceId);
cmd.Parameters.AddWithValue("@Count", serviceCheck.Count);
cmd.Parameters.AddWithValue("@Service_date", serviceCheck.DateTime);
cmd.ExecuteNonQuery();
return serviceCheck;
}
public ServiceCheck? Delete(int id)
{
var element = Get(id);
using var con = SqlConnection.GetConnection();
con.Open();
using var cmd = new NpgsqlCommand($"DELETE FROM service_check WHERE service_check_id = {id}", con);
cmd.ExecuteNonQuery();
return element;
}
public ServiceCheck? Get(int id)
{
using var con = SqlConnection.GetConnection();
con.Open();
using var cmd = new NpgsqlCommand($"SELECT * FROM service_check WHERE service_check_id = {id}", con);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
return new ServiceCheck
{
Id = reader.GetInt32(0),
ReservationId = !reader.IsDBNull(1) ? reader.GetInt32(1) : 0,
ServiceId = !reader.IsDBNull(2) ? reader.GetInt32(2) : 0,
Count = reader.GetInt32(3),
DateTime = reader.GetDateTime(4),
};
}
return null;
}
public List<ServiceCheck> GetAll()
{
var serviceChecks = new List<ServiceCheck>();
using var con = SqlConnection.GetConnection();
con.Open();
using var cmd = new NpgsqlCommand("SELECT * FROM service_check order by service_check_id", con);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
serviceChecks.Add(new ServiceCheck
{
Id = reader.GetInt32(0),
ReservationId = !reader.IsDBNull(1) ? reader.GetInt32(1) : 0,
ServiceId = !reader.IsDBNull(2) ? reader.GetInt32(2) : 0,
Count = reader.GetInt32(3),
DateTime = reader.GetDateTime(4),
});
}
return serviceChecks;
}
public ServiceCheck? Update(ServiceCheck serviceCheck)
{
using var con = SqlConnection.GetConnection();
con.Open();
using var cmd = new NpgsqlCommand($"UPDATE service_check SET " +
$"fk_reservation_id = @FKReservation_Id, " +
$"fk_service_id = @FKService_Id, " +
$"count = '{serviceCheck.Count}', " +
$"service_date = '{serviceCheck.DateTime}', " +
$"WHERE service_check_id = '{serviceCheck.Id}' ", con);
cmd.Parameters.AddWithValue("@FKReservation_Id", serviceCheck.ReservationId == 0 ? DBNull.Value : serviceCheck.ReservationId);
cmd.Parameters.AddWithValue("@FKService_Id", serviceCheck.ServiceId == 0 ? DBNull.Value : serviceCheck.ServiceId);
cmd.ExecuteNonQuery();
var element = Get(serviceCheck.Id);
return element;
}
}
}