public GuestResponseDto GetGuestById(int idGuest) { using var sqlConn = new SqlConnection(ConnStr); using var sqlCmd = new SqlCommand { Connection = sqlConn }; sqlCmd.CommandText = $@"SELECT g.IdGosc, g.Imie, g.Nazwisko, g.Procent_rabatu FROM Gosc g WHERE g.IdGosc = @IdGuest;"; sqlCmd.Parameters.AddWithValue("IdGuest", idGuest); sqlConn.Open(); using var reader = sqlCmd.ExecuteReader(); while (reader.Read()) { var item = new GuestResponseDto { IdGuest = int.Parse(reader["IdGosc"].ToString()), FirstName = reader["Imie"].ToString(), LastName = reader["Nazwisko"].ToString(), DiscountPercent = !string.IsNullOrEmpty(reader["Procent_rabatu"]?.ToString()) ? int.Parse(reader["Procent_rabatu"].ToString()) : (int?)null }; return(item); } return(null); }
public ICollection <GuestResponseDto> GetGuestsCollection(string lastName) { var listToReturn = new List <GuestResponseDto>(); using var sqlConn = new SqlConnection(ConnStr); using var sqlCmd = new SqlCommand { Connection = sqlConn }; if (string.IsNullOrEmpty(lastName)) { sqlCmd.CommandText = @"SELECT g.IdGosc, g.Imie, g.Nazwisko, g.Procent_rabatu FROM Gosc g;"; } else { sqlCmd.CommandText = @"SELECT g.IdGosc, g.Imie, g.Nazwisko, g.Procent_rabatu FROM Gosc g WHERE g.Nazwisko = @LastName;"; sqlCmd.Parameters.AddWithValue("LastName", lastName); } sqlConn.Open(); using var reader = sqlCmd.ExecuteReader(); while (reader.Read()) { var item = new GuestResponseDto { IdGuest = int.Parse(reader["IdGosc"].ToString()), FirstName = reader["Imie"].ToString(), LastName = reader["Nazwisko"].ToString(), DiscountPercent = !string.IsNullOrEmpty(reader["Procent_rabatu"]?.ToString()) ? int.Parse(reader["Procent_rabatu"].ToString()) : (int?)null }; listToReturn.Add(item); } return(listToReturn); }