Ejemplo n.º 1
0
        /// <summary>
        /// Checks if the jmbg is correct
        /// </summary>
        /// <param name="jmbg">the jmbg we are checking</param>
        /// <param name="id">for the specific user</param>
        /// <returns>null if the input is correct or string error message if its wrong</returns>
        public string JMBGChecker(string jmbg, int id)
        {
            Service service = new Service();

            List <tblUser> AllUsers    = service.GetAllUsers();
            DateTime       dt          = default(DateTime);
            string         currentJbmg = "";

            if (jmbg == null)
            {
                return("JMBG cannot be empty.");
            }

            // Get the current users jmbg
            for (int i = 0; i < AllUsers.Count; i++)
            {
                if (AllUsers[i].UserID == id)
                {
                    currentJbmg = AllUsers[i].JMBG;
                    break;
                }
            }

            // Check if the jmbg already exists, but it is not the current user jmbg
            for (int i = 0; i < AllUsers.Count; i++)
            {
                if (AllUsers[i].JMBG == jmbg && currentJbmg != jmbg)
                {
                    return("This JMBG already exists!");
                }
            }

            if (!(jmbg.Length == 13))
            {
                return("Please enter a number with 13 characters.");
            }

            // Get date
            dt = iv.CountDateOfBirth(jmbg);

            if (dt == default(DateTime))
            {
                return("Incorrect JMBG Format.");
            }

            return(null);
        }
        /// <summary>
        /// Creates or edits a manager
        /// </summary>
        /// <param name="manager">manager to add or edit</param>
        /// <returns>a new or edited manager</returns>
        public vwManager AddManager(vwManager manager)
        {
            InputCalculator iv = new InputCalculator();

            try
            {
                using (ReportDBEntities context = new ReportDBEntities())
                {
                    if (manager.UserID == 0)
                    {
                        manager.DateOfBirth = iv.CountDateOfBirth(manager.JMBG);

                        tblUser newManager = new tblUser();
                        newManager.FirstName    = manager.FirstName;
                        newManager.LastName     = manager.LastName;
                        newManager.JMBG         = manager.JMBG;
                        newManager.DateOfBirth  = manager.DateOfBirth;
                        newManager.BankAccount  = manager.BankAccount;
                        newManager.Email        = manager.Email;
                        newManager.Position     = manager.Position;
                        newManager.Salary       = manager.Salary;
                        newManager.Username     = manager.Username;
                        newManager.UserPassword = manager.UserPassword;
                        newManager.Sector       = manager.Sector;
                        newManager.Access       = manager.Access;

                        context.tblUsers.Add(newManager);
                        context.SaveChanges();
                        manager.UserID = newManager.UserID;
                        return(manager);
                    }
                    else
                    {
                        tblUser managerToEdit = (from ss in context.tblUsers where ss.UserID == manager.UserID select ss).First();

                        // Get the date of birth
                        manager.DateOfBirth = iv.CountDateOfBirth(manager.JMBG);

                        managerToEdit.FirstName    = manager.FirstName;
                        managerToEdit.LastName     = manager.LastName;
                        managerToEdit.JMBG         = manager.JMBG;
                        managerToEdit.DateOfBirth  = manager.DateOfBirth;
                        managerToEdit.BankAccount  = manager.BankAccount;
                        managerToEdit.Email        = manager.Email;
                        managerToEdit.Salary       = manager.Salary;
                        managerToEdit.Username     = manager.Username;
                        managerToEdit.Position     = manager.Position;
                        managerToEdit.UserPassword = manager.UserPassword;
                        managerToEdit.Sector       = manager.Sector;
                        managerToEdit.Access       = manager.Access;

                        managerToEdit.UserID = manager.UserID;

                        tblUser managerEdit = (from ss in context.tblUsers
                                               where ss.UserID == manager.UserID
                                               select ss).First();
                        context.SaveChanges();
                        return(manager);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }
        /// <summary>
        /// Creates or edits a worker
        /// </summary>
        /// <param name="worker">worker to add or edit</param>
        /// <returns>a new or edited worker</returns>
        public vwUser AddWorker(vwUser worker)
        {
            InputCalculator iv = new InputCalculator();

            try
            {
                using (ReportDBEntities context = new ReportDBEntities())
                {
                    if (worker.UserID == 0)
                    {
                        worker.DateOfBirth = iv.CountDateOfBirth(worker.JMBG);

                        tblUser newWorker = new tblUser();
                        newWorker.FirstName    = worker.FirstName;
                        newWorker.LastName     = worker.LastName;
                        newWorker.JMBG         = worker.JMBG;
                        newWorker.DateOfBirth  = worker.DateOfBirth;
                        newWorker.BankAccount  = worker.BankAccount;
                        newWorker.Email        = worker.Email;
                        newWorker.Position     = worker.Position;
                        newWorker.Salary       = worker.Salary;
                        newWorker.Username     = worker.Username;
                        newWorker.UserPassword = worker.UserPassword;

                        context.tblUsers.Add(newWorker);
                        context.SaveChanges();
                        worker.UserID = newWorker.UserID;

                        return(worker);
                    }
                    else
                    {
                        tblUser workerToEdit = (from ss in context.tblUsers where ss.UserID == worker.UserID select ss).First();

                        // Get the date of birth
                        worker.DateOfBirth = iv.CountDateOfBirth(worker.JMBG);

                        workerToEdit.FirstName    = worker.FirstName;
                        workerToEdit.LastName     = worker.LastName;
                        workerToEdit.JMBG         = worker.JMBG;
                        workerToEdit.DateOfBirth  = worker.DateOfBirth;
                        workerToEdit.BankAccount  = worker.BankAccount;
                        workerToEdit.Email        = worker.Email;
                        workerToEdit.Salary       = worker.Salary;
                        workerToEdit.Username     = worker.Username;
                        workerToEdit.Position     = worker.Position;
                        workerToEdit.UserPassword = worker.UserPassword;

                        workerToEdit.UserID = worker.UserID;

                        tblUser workerEdit = (from ss in context.tblUsers
                                              where ss.UserID == worker.UserID
                                              select ss).First();
                        context.SaveChanges();
                        return(worker);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }