111 lines
4.5 KiB
C#
111 lines
4.5 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 ReservationImplement : IReservationLogic
|
|
{
|
|
public Reservation? Create(Reservation reservation)
|
|
{
|
|
using var con = SqlConnection.GetConnection();
|
|
con.Open();
|
|
using var cmd = new NpgsqlCommand(
|
|
"INSERT INTO reservation (fk_room_id, fk_guest_id, arrival_date, departure_date, price)" +
|
|
" VALUES (@FKRoom_id, @FKGuest_id, @Arrival_date, @Departure_date, @Price)",
|
|
con
|
|
);
|
|
cmd.Parameters.AddWithValue("@FKRoom_id", reservation.RoomId == 0 ? DBNull.Value : reservation.RoomId);
|
|
cmd.Parameters.AddWithValue("@FKGuest_id", reservation.GuestId == 0 ? DBNull.Value : reservation.GuestId);
|
|
cmd.Parameters.AddWithValue("@Arrival_date", reservation.ArrivalDate);
|
|
cmd.Parameters.AddWithValue("@Departure_date", reservation.DepartureDate);
|
|
cmd.Parameters.AddWithValue("@Price", reservation.Price);
|
|
cmd.ExecuteNonQuery();
|
|
return reservation;
|
|
}
|
|
|
|
public Reservation? Delete(int id)
|
|
{
|
|
var element = Get(id);
|
|
using var con = SqlConnection.GetConnection();
|
|
con.Open();
|
|
using var cmd = new NpgsqlCommand($"DELETE FROM reservation WHERE reservation_id = {id}", con);
|
|
cmd.ExecuteNonQuery();
|
|
return element;
|
|
}
|
|
|
|
public Reservation? Get(int id)
|
|
{
|
|
using var con = SqlConnection.GetConnection();
|
|
con.Open();
|
|
using var cmd = new NpgsqlCommand($"SELECT * FROM reservation WHERE reservation_id = {id}", con);
|
|
using var reader = cmd.ExecuteReader();
|
|
if (reader.Read())
|
|
{
|
|
return new Reservation
|
|
{
|
|
Id = reader.GetInt32(0),
|
|
RoomId = !reader.IsDBNull(1) ? reader.GetInt32(1) : 0,
|
|
GuestId = !reader.IsDBNull(2) ? reader.GetInt32(2) : 0,
|
|
ArrivalDate = ToDateOnly(reader.GetDateTime(3)),
|
|
DepartureDate = ToDateOnly(reader.GetDateTime(4)),
|
|
Price = reader.GetDouble(5),
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public List<Reservation> GetAll()
|
|
{
|
|
var reservations = new List<Reservation>();
|
|
using var con = SqlConnection.GetConnection();
|
|
con.Open();
|
|
using var cmd = new NpgsqlCommand("SELECT * FROM reservation order by reservation_id", con);
|
|
using var reader = cmd.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
reservations.Add(new Reservation
|
|
{
|
|
Id = reader.GetInt32(0),
|
|
RoomId = !reader.IsDBNull(1) ? reader.GetInt32(1) : 0,
|
|
GuestId = !reader.IsDBNull(2) ? reader.GetInt32(2) : 0,
|
|
ArrivalDate = ToDateOnly(reader.GetDateTime(3)),
|
|
DepartureDate = ToDateOnly(reader.GetDateTime(4)),
|
|
Price = reader.GetDouble(5),
|
|
});
|
|
}
|
|
return reservations;
|
|
}
|
|
|
|
public Reservation? Update(Reservation reservation)
|
|
{
|
|
using var con = SqlConnection.GetConnection();
|
|
con.Open();
|
|
using var cmd = new NpgsqlCommand($"UPDATE reservation SET " +
|
|
$"fk_room_id = @FKHotel_Id, " +
|
|
$"fk_guest_id = @FKGuest_Id, " +
|
|
$"arrival_date = '{reservation.ArrivalDate}', " +
|
|
$"departure_date = '{reservation.DepartureDate}', " +
|
|
$"price = '{reservation.Price}' " +
|
|
$"WHERE reservation_id = {reservation.Id}", con);
|
|
cmd.Parameters.AddWithValue("@FKRoom_Id", reservation.RoomId == 0 ? DBNull.Value : reservation.RoomId);
|
|
cmd.Parameters.AddWithValue("@FKGuest_Id", reservation.GuestId == 0 ? DBNull.Value : reservation.GuestId);
|
|
cmd.ExecuteNonQuery();
|
|
var element = Get(reservation.Id);
|
|
return element;
|
|
}
|
|
|
|
public DateOnly ToDateOnly(DateTime dateTime)
|
|
{
|
|
var DateTime = dateTime;
|
|
var Date = new DateOnly(DateTime.Year, DateTime.Month, DateTime.Day);
|
|
return Date;
|
|
}
|
|
}
|
|
}
|