/// <summary>
 /// Saves the created Worker in database
 /// </summary>
 private void SaveWorker()
 {
     try
     {
         using (WorkersDbEntities db = new WorkersDbEntities())
         {
             if (ValideteJMBG(Worker.JMBG))
             {
                 if (counter == 0)
                 {
                     Worker.Gender = Gender;
                     db.tblWorkers.Add(Worker);
                     db.SaveChanges();
                     counter++;
                 }
                 WriteWorker();
             }
             else
             {
                 MessageBox.Show("Invalid JMBG");
             }
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
     }
 }
 /// <summary>
 /// Find Worker thats being updated
 /// </summary>
 /// <returns>Worker thats being updated</returns>
 private tblWorker FindWorker()
 {
     try
     {
         tblWorker worker = new tblWorker();
         using (WorkersDbEntities db = new WorkersDbEntities())
         {
             worker = db.tblWorkers.Where(x => x.Id == ViewWorker.Id).FirstOrDefault();
             return(worker);
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
         return(null);
     }
 }
 /// <summary>
 /// Deletes Worker from database
 /// </summary>
 private void WorkerDelete(object sender, DoWorkEventArgs e)
 {
     try
     {
         using (WorkersDbEntities db = new WorkersDbEntities())
         {
             Thread.Sleep(2000);
             tblWorker worker = db.tblWorkers.Where(x => x.Id == viewWorker.Id).FirstOrDefault();
             db.tblWorkers.Remove(worker);
             db.SaveChanges();
             AllInfoAboutWorker = GetAllInfoAboutWorkers();
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
     }
 }
 /// <summary>
 /// Gets all info about Workers
 /// </summary>
 /// <returns>All Workers and info about them</returns>
 private List <vwWorker> GetAllInfoAboutWorkers()
 {
     try
     {
         using (WorkersDbEntities db = new WorkersDbEntities())
         {
             List <vwWorker> allInfoAboutWorkers = new List <vwWorker>();
             allInfoAboutWorkers = db.vwWorkers.OrderBy(X => X.Id).ToList();
             AllInfoAboutWorker  = allInfoAboutWorkers;
             return(AllInfoAboutWorker);
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
         return(null);
     }
 }
 /// <summary>
 /// Deletes Selected Worker
 /// </summary>
 private void DeleteWorker()
 {
     try
     {
         MessageBoxResult messageBoxResult = MessageBox.Show("Are you sure?", "Delete Confirmation", MessageBoxButton.YesNo);
         if (messageBoxResult == MessageBoxResult.Yes)
         {
             using (WorkersDbEntities db = new WorkersDbEntities())
             {
                 bgWorker.DoWork             += WorkerDelete;
                 bgWorker.RunWorkerCompleted += WorkerDeleted;
                 bgWorker.RunWorkerAsync();
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message.ToString());
     }
 }
 /// <summary>
 /// Gets all Locations form txt file and write them into the database
 /// </summary>
 private void WriteLocations()
 {
     using (WorkersDbEntities db = new WorkersDbEntities())
     {
         List <tblLocation> locations = new List <tblLocation>();
         foreach (tblLocation location1 in db.tblLocations)
         {
             locations.Add(location1);
         }
         if (locations.Count == 0)
         {
             string location = @"~\..\..\..\Locations.txt";
             if (File.Exists(location))
             {
                 string[] allLocations = File.ReadAllLines(location);
                 foreach (string oneLocation in allLocations)
                 {
                     string[]    separeted   = oneLocation.Split(',');
                     string      address     = separeted[0];
                     string      city        = separeted[1];
                     string      country     = separeted[2];
                     tblLocation newLocation = new tblLocation();
                     newLocation.Address = address;
                     newLocation.City    = city;
                     newLocation.Country = country;
                     using (WorkersDbEntities dbase = new WorkersDbEntities())
                     {
                         dbase.tblLocations.Add(newLocation);
                         dbase.SaveChanges();
                     }
                 }
             }
             else
             {
                 System.Diagnostics.Debug.WriteLine("File not found");
             }
         }
     }
 }
 /// <summary>
 /// Gets all locations from the Database
 /// </summary>
 /// <returns>All Locations</returns>
 private List <string> GetAllLocations()
 {
     try
     {
         using (WorkersDbEntities db = new WorkersDbEntities())
         {
             List <tblLocation> list = new List <tblLocation>();
             list = db.tblLocations.Where(x => x.Id > 0).OrderBy(x => x.Address).ToList();
             List <string> allLocations = new List <string>();
             foreach (tblLocation location in list)
             {
                 string fullLocation = location.Address + "," + location.City + "," + location.Country;
                 allLocations.Add(fullLocation);
             }
             return(allLocations);
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
         return(null);
     }
 }
        /// <summary>
        /// Saves created Worker to the database
        /// </summary>
        private void SaveExecute()
        {
            tblLocation location = new tblLocation();

            string[] fullLocation = Location.Split(',');
            string   address      = fullLocation[0];

            try
            {
                using (WorkersDbEntities db = new WorkersDbEntities())
                {
                    location = db.tblLocations.Where(x => x.Address == address).FirstOrDefault();

                    Worker.FKLocation = location.Id;
                    Worker.Gender     = Gender;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
            }

            tblSector sector = new tblSector();

            sector.Sector = Sector;
            List <tblSector> sectors = new List <tblSector>();

            try
            {
                using (WorkersDbEntities db = new WorkersDbEntities())
                {
                    foreach (tblSector dbSector in db.tblSectors)
                    {
                        sectors.Add(dbSector);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
            }

            if (sectors.Count == 0)
            {
                try
                {
                    using (WorkersDbEntities db = new WorkersDbEntities())
                    {
                        db.tblSectors.Add(sector);
                        db.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
                }

                try
                {
                    using (WorkersDbEntities db = new WorkersDbEntities())
                    {
                        tblSector sector1 = db.tblSectors.Where(x => x.Sector == sector.Sector).FirstOrDefault();
                        Worker.FKSector = sector1.Id;
                        db.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
                }
            }
            else
            {
                for (int i = 0; i < sectors.Count; i++)
                {
                    if (sectors[i].Sector == sector.Sector)
                    {
                        Worker.FKSector = sectors[i].Id;
                    }
                    else if (i == sectors.Count - 1 && sectors[i].Sector != sector.Sector)
                    {
                        try

                        {
                            using (WorkersDbEntities db = new WorkersDbEntities())
                            {
                                db.tblSectors.Add(sector);
                                db.SaveChanges();
                            }
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
                        }

                        try
                        {
                            using (WorkersDbEntities db = new WorkersDbEntities())
                            {
                                tblSector sector1 = db.tblSectors.Where(x => x.Sector == sector.Sector).FirstOrDefault();
                                Worker.FKSector = sector1.Id;
                                db.SaveChanges();
                            }
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            SaveWorker();
        }