public DoctorDTO Add(DoctorDTO doctorDTO) { using (_connection = new SqlConnection(_connectionString)) { try { SQLPracticeRepository practiceRepository = new SQLPracticeRepository(_configuration); if (practiceRepository.PracticeExists(doctorDTO.Practice_id)) { var query = "insert into Doctor(Name,Surname,Birthdate,Telephone,Email,Practice_id) values (@name,@surname,@birthdate,@telephone,@email,@practice_id)"; SqlCommand sqlCommand = new SqlCommand(query, _connection); sqlCommand.Parameters.AddWithValue("@name", doctorDTO.Name); sqlCommand.Parameters.AddWithValue("@surname", doctorDTO.Surname); sqlCommand.Parameters.AddWithValue("@birthdate", doctorDTO.Birthdate); sqlCommand.Parameters.AddWithValue("@telephone", doctorDTO.Telephone); sqlCommand.Parameters.AddWithValue("@email", doctorDTO.Email); sqlCommand.Parameters.AddWithValue("@practice_id", doctorDTO.Practice_id); _connection.Open(); sqlCommand.ExecuteNonQuery(); var doctor = new Doctors { Name = doctorDTO.Name, Surname = doctorDTO.Surname, Birthdate = doctorDTO.Birthdate, Telephone = doctorDTO.Telephone, Email = doctorDTO.Email, Practice_id = doctorDTO.Practice_id }; return(DoctorToDTO(doctor)); } else { return(null); } } catch { throw; } } }
public int Update(DoctorDTO doctorDTOChanges, int id) { if (!DoctorExists(id)) { return(0); } using (_connection = new SqlConnection(_connectionString)) { try { SQLPracticeRepository practiceRepository = new SQLPracticeRepository(_configuration); if (practiceRepository.PracticeExists(doctorDTOChanges.Practice_id)) { var query = $"update Doctor set Name=@name,Surname=@surname,Birthdate=@birthdate,Telephone=@telephone,Email=@email,Practice=@practice_id where id = {id}"; SqlCommand sqlCommand = new SqlCommand(query, _connection); sqlCommand.Parameters.AddWithValue("Name", doctorDTOChanges.Name); sqlCommand.Parameters.AddWithValue("Surname", doctorDTOChanges.Surname); sqlCommand.Parameters.AddWithValue("Birthdate", doctorDTOChanges.Birthdate); sqlCommand.Parameters.AddWithValue("Telephone", doctorDTOChanges.Telephone); sqlCommand.Parameters.AddWithValue("Email", doctorDTOChanges.Email); sqlCommand.Parameters.AddWithValue("Practice_id", doctorDTOChanges.Practice_id); _connection.Open(); sqlCommand.ExecuteNonQuery(); return(1); } else { return(-1); } } catch { throw; } } }