public virtual IActionResult AddHospital([FromBody] HospitalCentre hc)
 {
     //TODO: Uncomment the next line to return response 405 or use other options such as return this.NotFound(), return this.BadRequest(..), ...
     // return StatusCode(405);
     _hospitalRepo.AddHospital(hc);
     return(StatusCode(201, hc));
 }
        public List <HospitalCentre> PostHospitalName(int id1, string name1, string address1, string city1, int pincode1)
        {
            if (string.IsNullOrEmpty(connstring))
            {
                throw new ArgumentException("No connection string in config.json");
            }

            List <HospitalCentre> HospitalCentreList = new List <HospitalCentre> ();

            using (var conn = new SqlConnection(connstring))
            {
                conn.Open();
                var sql = "BEGIN IF NOT EXISTS(SELECT * FROM Hospital where HospitalName='@HospitalName')BEGIN INSERT INTO Hospital (ID,HospitalName,Address,City,PinCode) VALUES(@ID,@HospitalName,@Address,@City,@PinCode) END END ";
                //"update [hospital] set [ID]=@Id,[HospitalName]=@HospitalName,[Address]=@Address,[City]=@City,[PinCode]=@Pincode where [HospitalName] != @HospitalName";
                //Insert into dbo.Hospital (ID,HospitalName,Address,City,PinCode) Values (@ID,@HospitalName,@Address,@City,@PinCode)" + "where @HospitalName !=HospitalName
                using (var cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@ID", id1);
                    cmd.Parameters.AddWithValue("@HospitalName", name1);
                    cmd.Parameters.AddWithValue("@Address", address1);
                    cmd.Parameters.AddWithValue("@City", city1);
                    cmd.Parameters.AddWithValue("@PinCode", pincode1);

                    cmd.ExecuteNonQuery();
                }
                conn.Close();
            }
            using (var conn = new SqlConnection(connstring))
            {
                conn.Open();
                var sql = "SELECT * FROM dbo.Hospital";
                using (var cmd = new SqlCommand(sql, conn))
                {
                    SqlDataReader reader = cmd.ExecuteReader();

                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            HospitalCentre hc = new HospitalCentre();
                            hc.Id      = reader.GetInt32(0);
                            hc.Name    = reader.GetString(1);
                            hc.Address = reader.GetString(2);
                            hc.City    = reader.GetString(3);
                            hc.Pincode = reader.GetInt32(4);
                            HospitalCentreList.Add(hc);
                            Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetInt32(4));
                        }
                    }
                    else
                    {
                        Console.WriteLine("No rows found.");
                        return(null);
                    }
                    reader.Close();
                    conn.Close();
                }
                return(HospitalCentreList);
            }
        }
        public int AddHospital(HospitalCentre hc)
        {
            if (string.IsNullOrEmpty(connstring))
            {
                throw new ArgumentException("No connection string in config.json");
            }

            int idRowsAffected = 0;

            using (var conn = new SqlConnection(connstring))
            {
                string maxIdPlus1 = "(select max (id) + 1 from dbo.hospital)";

                var sql = "INSERT INTO dbo.hospital (id , hospitalname, Address, City, Pincode) " +
                          "   VALUES ( " + maxIdPlus1 + ", '" + hc.Name + "'  , '" + hc.Address + "', '"
                          + hc.City + "' , " + hc.Pincode + " )";


                using (var cmd = new SqlCommand(sql, conn))
                {
                    conn.Open();
                    idRowsAffected = cmd.ExecuteNonQuery();
                    Console.WriteLine("Added a row.");
                    conn.Close();
                }
                return(idRowsAffected);
            }
        }
        public List <HospitalCentre> PatchHospitalName(int id2, string name2)
        {
            if (string.IsNullOrEmpty(connstring))
            {
                throw new ArgumentException("No connection string in config.json");
            }

            List <HospitalCentre> HospitalCentreList = new List <HospitalCentre> ();

            using (var conn = new SqlConnection(connstring))
            {
                conn.Open();
                var sql = "UPDATE [Hospital] SET [HospitalName]=@HospitalName WHERE [ID]=@ID";
                using (var cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@ID", id2);
                    cmd.Parameters.AddWithValue("@HospitalName", name2);

                    cmd.ExecuteNonQuery();
                }
                conn.Close();
            }
            using (var conn = new SqlConnection(connstring))
            {
                conn.Open();
                var sql = "SELECT * FROM dbo.Hospital";
                using (var cmd = new SqlCommand(sql, conn))
                {
                    SqlDataReader reader = cmd.ExecuteReader();

                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            HospitalCentre hc = new HospitalCentre();
                            hc.Id      = reader.GetInt32(0);
                            hc.Name    = reader.GetString(1);
                            hc.Address = reader.GetString(2);
                            hc.City    = reader.GetString(3);
                            hc.Pincode = reader.GetInt32(4);
                            HospitalCentreList.Add(hc);
                            Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetInt32(4));
                        }
                    }
                    else
                    {
                        Console.WriteLine("No rows found.");
                        return(null);
                    }
                    reader.Close();
                    conn.Close();
                }
                return(HospitalCentreList);
            }
        }
        public virtual IActionResult UpdateHospital([FromBody] HospitalCentre hc)
        {
            Console.WriteLine("Update hospital Controller");
            int rowsAffected = _hospitalRepo.UpdateHospital(hc);

            if (rowsAffected > 0)
            {
                return(Ok(hc));
            }
            else
            {
                return(StatusCode(404, hc));
            }
        }
        public List <HospitalCentre> GetHospitalName(string name)
        {
            if (string.IsNullOrEmpty(connstring))
            {
                throw new ArgumentException("No connection string in config.json");
            }

            List <HospitalCentre> HospitalCentreList = new List <HospitalCentre> ();

            using (var conn = new SqlConnection(connstring))
            {
                conn.Open();
                var sql = "SELECT * FROM dbo.Hospital where HospitalName like '%' + @hname + '%'";
                using (var cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@hname", name);

                    SqlDataReader reader = cmd.ExecuteReader();

                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            HospitalCentre hc = new HospitalCentre();
                            hc.Id      = reader.GetInt32(0);
                            hc.Name    = reader.GetString(1);
                            hc.Address = reader.GetString(2);
                            hc.City    = reader.GetString(3);
                            hc.Pincode = reader.GetInt32(4);
                            HospitalCentreList.Add(hc);
                            Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetInt32(4));
                        }
                    }
                    else
                    {
                        Console.WriteLine("No rows found.");
                        return(null);
                    }
                    reader.Close();
                    conn.Close();
                }
                return(HospitalCentreList);
            }
        }
 public virtual IActionResult GetHospitalById([FromRoute] int?hospitalId)
 {
     if ((hospitalId < 1) || (hospitalId >= 100000))
     {
         return(StatusCode(400, "Hospital Id shoild be between 1 & 99999"));
     }
     try
     {
         HospitalCentre hc = _hospitalRepo.GetHospital((int)hospitalId);
         if (hc == null)
         {
             return(this.NotFound("No hospital found for id : " + hospitalId));
         }
         return(StatusCode(200, hc));
     }
     catch (Exception ex)
     {
         return(StatusCode(500, ex.ToString()));
     }
 }
        public HospitalCentre GetHospital(int id)
        {
            if (string.IsNullOrEmpty(connstring))
            {
                throw new ArgumentException("No connection string in config.json");
            }

            HospitalCentre hc = new HospitalCentre();

            using (var conn = new SqlConnection(connstring))
            {
                var sql = "SELECT * FROM dbo.Hospital where id = " + id;
                using (var cmd = new SqlCommand(sql, conn))
                {
                    conn.Open();
                    SqlDataReader reader = cmd.ExecuteReader();

                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            hc.Id      = reader.GetInt32(0);
                            hc.Name    = reader.GetString(1);
                            hc.Address = reader.GetString(2);
                            hc.City    = reader.GetString(3);
                            hc.Pincode = reader.GetInt32(4);
                            Console.WriteLine("{0}\t{1}", reader.GetInt32(0), reader.GetString(1));
                        }
                    }
                    else
                    {
                        Console.WriteLine("No rows found.");
                        return(null);
                    }
                    reader.Close();
                    conn.Close();
                }
                return(hc);
            }
        }
// hospitalname, Address, City, Pincode
        public int UpdateHospital(HospitalCentre hc)
        {
            if (string.IsNullOrEmpty(connstring))
            {
                throw new ArgumentException("No connection string in config.json");
            }

            int    idRowsAffected = 0;
            string updateCmd      = "UPDATE dbo.Hospital SET hospitalname = '" + hc.Name + "', Address = '" + hc.Address + "',  City = '" + hc.City + "', Pincode = " + hc.Pincode + " where id = " + hc.Id;

            Console.WriteLine(updateCmd);
            using (var conn = new SqlConnection(connstring))
            {
                using (var cmd = new SqlCommand(updateCmd, conn))
                {
                    conn.Open();
                    idRowsAffected = cmd.ExecuteNonQuery();
                    conn.Close();
                }
                return(idRowsAffected);
            }
        }
        public List <HospitalCentre> GetHospitals()
        {
            List <HospitalCentre> allCentres = new List <HospitalCentre> ();

            {
                HospitalCentre hC = new HospitalCentre();
                hC.Id   = 1;
                hC.Name = "Bowring";
                allCentres.Add(hC);
            }
            {
                HospitalCentre hC = new HospitalCentre();
                hC.Id   = 2;
                hC.Name = "Leprose";
                allCentres.Add(hC);
            }
            {
                HospitalCentre hC = new HospitalCentre();
                hC.Id   = 3;
                hC.Name = "Cancer";
                allCentres.Add(hC);
            }
            return(allCentres);
        }
 public int UpdateHospital(HospitalCentre hc)
 {
     return(0);
 }
 public int AddHospital(HospitalCentre hc)
 {
     return(0);
 }
        public List <HospitalCentre> AddNewHospital(JObject jsonResult, int id)
        {
            HospitalCentre item = JsonConvert.DeserializeObject <HospitalCentre>(jsonResult.ToString());

            return(_hospital.PatchHospitalName(id, item.Name));
        }
        public List <HospitalCentre> AddtnNewHospital(JObject jsonResult)
        {
            HospitalCentre item = JsonConvert.DeserializeObject <HospitalCentre>(jsonResult.ToString());

            return(_hospital.PostHospitalName(item.Id, item.Name, item.Address, item.City, item.Pincode));
        }