public NewDoctorDto AddDoctor(NewDoctorDto newDoctor)
        {
            using (SqlConnection connection = new SqlConnection(ConString))
                using (SqlCommand command = new SqlCommand())
                {
                    command.Connection = connection;
                    connection.Open();
                    var transaction = connection.BeginTransaction();
                    command.Transaction = transaction;
                    SqlDataReader dataReader = null;

                    try
                    {
                        command.CommandText = "insert into Doctors(FirstName, LastName, Email) " +
                                              "values(@FirstName, @LastName, @Email) ";
                        command.Parameters.AddWithValue("FirstName", newDoctor.FirstName);
                        command.Parameters.AddWithValue("LastName", newDoctor.LastName);
                        command.Parameters.AddWithValue("Email", newDoctor.Email);
                        command.ExecuteNonQuery();

                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        if (dataReader != null)
                        {
                            dataReader.Close();
                            transaction.Rollback();
                        }
                        throw e;
                    }

                    return(newDoctor);
                }
        }
Example #2
0
 public IActionResult Post([FromBody] NewDoctorDto doctor)
 {
     try
     {
         return(Ok(_dbService.AddDoctor(doctor)));
     }
     catch (Exception e)
     {
         return(BadRequest("Exception: " + e.Message + "\n" + e.StackTrace));
     }
 }
Example #3
0
 public IActionResult Put(int id, [FromBody] NewDoctorDto doctor)
 {
     try
     {
         if (_dbService.GetDoctor(id) == null)
         {
             return(NotFound("Selected doctor doesn't exist"));
         }
         return(Ok(_dbService.UpdateDoctor(id, doctor)));
     }
     catch (Exception e)
     {
         return(BadRequest("Exception: " + e.Message + "\n" + e.StackTrace));
     }
 }
        public NewDoctorDto UpdateDoctor(int id, NewDoctorDto updatedDoctor)
        {
            using (SqlConnection connection = new SqlConnection(ConString))
                using (SqlCommand command = new SqlCommand())
                {
                    command.Connection = connection;
                    connection.Open();
                    var transaction = connection.BeginTransaction();
                    command.Transaction = transaction;
                    SqlDataReader dataReader = null;

                    try
                    {
                        command.CommandText = "update Doctors " +
                                              "set FirstName = @FirstName, LastName = @LastName, Email = @Email " +
                                              "where IdDoctor =  @IdDoctor";
                        command.Parameters.AddWithValue("FirstName", updatedDoctor.FirstName);
                        command.Parameters.AddWithValue("LastName", updatedDoctor.LastName);
                        command.Parameters.AddWithValue("Email", updatedDoctor.Email);
                        command.Parameters.AddWithValue("IdDoctor", id);
                        command.ExecuteNonQuery();

                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        if (dataReader != null)
                        {
                            dataReader.Close();
                            transaction.Rollback();
                        }
                        throw e;
                    }

                    return(updatedDoctor);
                }
        }