public AppointmentDTO Add(AppointmentDTO appointmentDTO) { using (_connection = new SqlConnection(_connectionString)) { try { SQLPatientRepository patientRepository = new SQLPatientRepository(_configuration); SQLDoctorRepository doctorRepository = new SQLDoctorRepository(_configuration); if (patientRepository.PatientExists(appointmentDTO.Patient_id) && doctorRepository.DoctorExists(appointmentDTO.Doctor_id)) { var query = "insert into Appointment(Patient_id,Doctor_id,Diagnosis_code,Diagnosis_description, Date) values (@patient_id,@doctor_id,@diagnosis_code,@diagnosis_description, @date)"; SqlCommand sqlCommand = new SqlCommand(query, _connection); sqlCommand.Parameters.AddWithValue("@patient_id", appointmentDTO.Patient_id); sqlCommand.Parameters.AddWithValue("@doctor_id", appointmentDTO.Doctor_id); sqlCommand.Parameters.AddWithValue("@diagnosis_code", appointmentDTO.DiagnosisCode); sqlCommand.Parameters.AddWithValue("@diagnosis_description", appointmentDTO.DiagnosisDescription); sqlCommand.Parameters.AddWithValue("@date", appointmentDTO.Date); _connection.Open(); sqlCommand.ExecuteNonQuery(); var appointment = new Appointments { Patient_id = appointmentDTO.Patient_id, Doctor_id = appointmentDTO.Doctor_id, DiagnosisCode = appointmentDTO.DiagnosisCode, DiagnosisDescription = appointmentDTO.DiagnosisDescription, Date = appointmentDTO.Date }; return(AppointmentToDTO(appointment)); } else { return(null); } } catch { throw; } } }
public int Update(AppointmentDTO appointmentDTOChanges, int id) { if (!AppointmentExists(id)) { return(0); } using (_connection = new SqlConnection(_connectionString)) { try { SQLPatientRepository patientRepository = new SQLPatientRepository(_configuration); SQLDoctorRepository doctorRepository = new SQLDoctorRepository(_configuration); if (patientRepository.PatientExists(appointmentDTOChanges.Patient_id) && doctorRepository.DoctorExists(appointmentDTOChanges.Doctor_id)) { var query = $"update Appointment set Patient_id=@patient_id,Doctor_id=@doctor_id,Diagnosis_code=@diagnosis_code,Diagnosis_description=@diagnosis_description,Date=@date where id = {id}"; SqlCommand sqlCommand = new SqlCommand(query, _connection); sqlCommand.Parameters.AddWithValue("@patient_id", appointmentDTOChanges.Patient_id); sqlCommand.Parameters.AddWithValue("@doctor_id", appointmentDTOChanges.Doctor_id); sqlCommand.Parameters.AddWithValue("@diagnosis_code", appointmentDTOChanges.DiagnosisCode); sqlCommand.Parameters.AddWithValue("@diagnosis_description", appointmentDTOChanges.DiagnosisDescription); sqlCommand.Parameters.AddWithValue("@date", appointmentDTOChanges.Date); _connection.Open(); sqlCommand.ExecuteNonQuery(); return(1); } else { return(-1); } } catch { throw; } } }