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 ServiceImplement : IServiceLogic { public Service? Create(Service service) { using var con = SqlConnection.GetConnection(); con.Open(); using var cmd = new NpgsqlCommand("INSERT INTO service (name, price) VALUES (@Name, @Price)", con); cmd.Parameters.AddWithValue("@Name", service.Name); cmd.Parameters.AddWithValue("@Price", service.Price); cmd.ExecuteNonQuery(); return service; } public Service? Delete(int id) { var element = Get(id); using var con = SqlConnection.GetConnection(); con.Open(); using var cmd = new NpgsqlCommand($"DELETE FROM service WHERE service_id = {id}", con); cmd.ExecuteNonQuery(); return element; } public Service? Get(int id) { using var con = SqlConnection.GetConnection(); con.Open(); using var cmd = new NpgsqlCommand($"SELECT * FROM service WHERE service_id = {id}", con); using var reader = cmd.ExecuteReader(); if (reader.Read()) { return new Service { Id = reader.GetInt32(0), Name = reader.GetString(1), Price = reader.GetDouble(2), }; } return null; } public List GetAll() { var services = new List(); using var con = SqlConnection.GetConnection(); con.Open(); using var cmd = new NpgsqlCommand("SELECT * FROM service order by service_id", con); using var reader = cmd.ExecuteReader(); while (reader.Read()) { services.Add(new Service { Id = reader.GetInt32(0), Name = reader.GetString(1), Price = reader.GetDouble(2), }); } return services; } public Service? Update(Service service) { using var con = SqlConnection.GetConnection(); con.Open(); using var cmd = new NpgsqlCommand($"UPDATE service SET name = '{service.Name}', price = '{service.Price}' WHERE service_id = {service.Id}", con); cmd.ExecuteNonQuery(); var element = Get(service.Id); return element; } } }