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 HotelImplement : IHotelLogic { public Hotel? Create(Hotel hotel) { using var con = SqlConnection.GetConnection(); con.Open(); using var cmd = new NpgsqlCommand("INSERT INTO hotel (name, address, count_star, count_room) VALUES (@Name, @Address, @Count_star, @Count_room)", con); cmd.Parameters.AddWithValue("@Name", hotel.HotelName); cmd.Parameters.AddWithValue("@Address", hotel.Address); cmd.Parameters.AddWithValue("@Count_star", hotel.CountStar); cmd.Parameters.AddWithValue("@Count_room", hotel.CountRoom); cmd.ExecuteNonQuery(); return hotel; } public Hotel? CreateId(Hotel hotel) { using var con = SqlConnection.GetConnection(); con.Open(); using var cmd = new NpgsqlCommand("INSERT INTO hotel (hotel_id, name, address, count_star, count_room) VALUES (@Hotel_id, @Name, @Address, @Count_star, @Count_room)", con); cmd.Parameters.AddWithValue("@Hotel_id", hotel.Id); cmd.Parameters.AddWithValue("@Name", hotel.HotelName); cmd.Parameters.AddWithValue("@Address", hotel.Address); cmd.Parameters.AddWithValue("@Count_star", hotel.CountStar); cmd.Parameters.AddWithValue("@Count_room", hotel.CountRoom); cmd.ExecuteNonQuery(); return hotel; } public Hotel? Delete(int id) { var element = Get(id); using var con = SqlConnection.GetConnection(); con.Open(); using var cmd = new NpgsqlCommand($"DELETE FROM hotel WHERE hotel_id = {id}", con); cmd.ExecuteNonQuery(); return element; } public Hotel? Get(int id) { using var con = SqlConnection.GetConnection(); con.Open(); using var cmd = new NpgsqlCommand($"SELECT * FROM hotel WHERE hotel_id = {id}", con); using var reader = cmd.ExecuteReader(); if (reader.Read()) { return new Hotel { Id = reader.GetInt32(0), HotelName = reader.GetString(1), Address = reader.GetString(2), CountStar = reader.GetInt32(3), CountRoom = reader.GetInt32(4), }; } return null; } public List GetAll() { var hotels = new List(); using var con = SqlConnection.GetConnection(); con.Open(); using var cmd = new NpgsqlCommand("SELECT * FROM hotel order by hotel_id", con); using var reader = cmd.ExecuteReader(); while (reader.Read()) { hotels.Add(new Hotel { Id = reader.GetInt32(0), HotelName = reader.GetString(1), Address = reader.GetString(2), CountStar = reader.GetInt32(3), CountRoom = reader.GetInt32(4), }); } return hotels; } public Hotel? Update(Hotel hotel) { using var con = SqlConnection.GetConnection(); con.Open(); using var cmd = new NpgsqlCommand($"UPDATE hotel SET name = '{hotel.HotelName}', address = '{hotel.Address}', count_star = '{hotel.CountStar}', count_room = '{hotel.CountRoom}' WHERE hotel_id = '{hotel.Id}'", con); cmd.ExecuteNonQuery(); var element = Get(hotel.Id); return element; } } }