コード例 #1
0
        public ActionResult CreateWorker(string firstName = "", string lastName = "", int chiefId = 0)
        {
            //Used, yoda's abstract was.
            string emptyLine = "";

            if (emptyLine.Equals(firstName) && emptyLine.Equals(lastName))
            {
                //return "";
                return(Redirect(indexView));
            }

            Worker chief = null;

            if (chiefId != 0 & (chief = this.GetWorkerById(chiefId)) == null)
            {
                Redirect(errorView);
            }

            Worker worker = new Worker
            {
                FirstName = firstName,
                LastName  = lastName,
                Chief     = chief
            };

            context.Workers.Add(worker);
            context.SaveChanges();
            //return JsonConvert.SerializeObject(worker);
            return(Redirect(indexView));
        }
コード例 #2
0
        /// <summary>
        /// Edits a new user depending if the uderID already exists
        /// </summary>
        /// <param name="user">the user that is being added</param>
        /// <param name="sector">the sector that is being added</param>
        /// <returns>a new user</returns>
        public tblUser AddUser(tblUser user, tblSector sector)
        {
            InputCalculator iv = new InputCalculator();

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

                        tblUser newUser = new tblUser();
                        newUser.FirstName   = user.FirstName;
                        newUser.LastName    = user.LastName;
                        newUser.JMBG        = user.JMBG;
                        newUser.IDCard      = user.IDCard;
                        newUser.DateOfBirth = user.DateOfBirth;
                        newUser.Gender      = user.Gender;
                        newUser.PhoneNumber = user.PhoneNumber;
                        newUser.SectorID    = sector.SectorID;
                        newUser.LocationID  = user.LocationID;
                        newUser.MenagerID   = user.MenagerID;

                        context.tblUsers.Add(newUser);
                        context.SaveChanges();
                        user.UserID = newUser.UserID;

                        return(user);
                    }
                    else
                    {
                        tblUser usersToEdit = (from ss in context.tblUsers where ss.UserID == user.UserID select ss).First();
                        // Get the date of birth
                        user.DateOfBirth = iv.CountDateOfBirth(user.JMBG);

                        usersToEdit.FirstName   = user.FirstName;
                        usersToEdit.LastName    = user.LastName;
                        usersToEdit.JMBG        = user.JMBG;
                        usersToEdit.IDCard      = user.IDCard;
                        usersToEdit.DateOfBirth = user.DateOfBirth;
                        usersToEdit.Gender      = user.Gender;
                        usersToEdit.PhoneNumber = user.PhoneNumber;
                        usersToEdit.SectorID    = sector.SectorID;
                        usersToEdit.LocationID  = user.LocationID;
                        usersToEdit.MenagerID   = user.MenagerID;
                        usersToEdit.UserID      = user.UserID;

                        context.SaveChanges();
                        return(user);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }
コード例 #3
0
        public ActionResult Create([Bind(Include = "Id,Name,Position,Birthday")] Worker worker)
        {
            if (ModelState.IsValid)
            {
                db.Workers.Add(worker);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(worker));
        }
コード例 #4
0
        public async Task <IActionResult> Book(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var todayDate = DateTime.Today;

            var worker = await _context.Worker
                         .Include(o => o.Schedule)
                         .FirstOrDefaultAsync(m => m.ID == id);

            if (worker == null)
            {
                return(NotFound());
            }

            var currDate = todayDate;

            for (int i = 0; i <= 6; i++)
            {
                var day = worker.Schedule
                          .Where(o => o.dateTime == currDate)
                          .FirstOrDefault();

                if (day == null)
                {
                    worker.Schedule.Add(new DaySchedule()
                    {
                        dateTime = currDate
                    });
                }

                currDate = currDate.AddDays(1);
            }
            _context.SaveChanges();


            var days = worker.Schedule
                       .Where(ds => ds.dateTime >= todayDate && ds.dateTime <= currDate)
                       .ToList();

            return(View(new Dictionary <Worker, IEnumerable <DaySchedule> >()
            {
                [worker] = days
            }));
        }
コード例 #5
0
        /// <summary>
        /// Deletes user if the uderID exists
        /// </summary>
        /// <param name="userID">the user that is being deleted</param>
        public void DeleteUser(int userID)
        {
            List <tblUser> tblUsers = GetAllUsers();

            try
            {
                using (WorkerContext context = new WorkerContext())
                {
                    bool isUser = IsUserID(userID);

                    if (isUser == true)
                    {
                        // find the user before removing them
                        tblUser userToDelete = (from r in context.tblUsers where r.UserID == userID select r).First();

                        context.tblUsers.Remove(userToDelete);
                        context.SaveChanges();
                    }
                    else
                    {
                        MessageBox.Show("Cannot delete the user");
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
            }
        }
コード例 #6
0
        public void ReadLocationFromFile(List <tblLocation> location)
        {
            string file = @"~\..\..\..\Locations.txt";
            int    id   = 0;

            using (WorkerContext context = new WorkerContext())
            {
                if (File.Exists(file))
                {
                    string[] readFile = File.ReadAllLines(file);

                    for (int i = 0; i < readFile.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(readFile[i]))
                        {
                            string[] trim    = readFile[i].Split(',');
                            string   address = trim[0];
                            string   city    = trim[1];
                            string   country = trim[2];
                            id++;

                            tblLocation s = new tblLocation(id, address, city, country);
                            location.Add(s);

                            context.tblLocations.Add(s);
                            context.SaveChanges();
                        }
                    }
                }
            }
        }
コード例 #7
0
        // add a worker
        public bool WorkerAdd(string IdCard, string FirstName, string SirName, string Phone, string Mail)
        {
            bool k      = false; // checks if worker was added
            bool helper = true;  // checks if worker exists in DB

            using (var db = new WorkerContext())
            {
                foreach (var item in db.Workers)
                {
                    if (IdCard == item.IdCard)
                    {
                        helper = false;
                    }
                }
                if (helper)
                {
                    db.Workers.Add(new Worker {
                        IdCard = IdCard, FirstName = FirstName, SirName = SirName, Phone = Phone, Mail = Mail, UserDetail = AddUser(IdCard, Phone)
                    });
                    db.SaveChanges();
                    k = true;
                }
            }
            return(k);
        }
コード例 #8
0
        public ActionResult Create(Worker worker)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new WorkerVM
                {
                    worker = worker,
                    List   = context.States.ToList(),
                    List1  = context.Countries.ToList()
                };

                return(View("Create", viewModel));
            }
            if (worker.ID == 0)
            {
                context.Workers.Add(worker);
            }
            else
            {
                State   m1         = new State();
                Country m2         = new Country();
                var     workerInDb = context.Workers.Single(c => c.ID == worker.ID);
                workerInDb.Name      = worker.Name;
                workerInDb.Birthdate = worker.Birthdate;
                worker.State         = m1.state;
                workerInDb.State     = worker.State;
                worker.Country       = m2.country;
                workerInDb.Country   = worker.Country;
            }

            context.SaveChanges();

            return(RedirectToAction("Index"));
        }
コード例 #9
0
 public Worker Action()
 {
     using WorkerContext db = new WorkerContext();
     Chief.SubWorkers.Add(Person);
     db.Entry(Chief).State  = EntityState.Modified;
     Person.Chief           = Chief;
     db.Entry(Person).State = EntityState.Modified;
     db.SaveChanges();
     return(Chief);
 }
コード例 #10
0
ファイル: TaskSystem.cs プロジェクト: TomGnill/ITMO_OOP_2020
        public Worker CreateWorker(string name)
        {
            Worker newWorker = new Worker {
                Name = name, Chief = null, SubWorkers = new List <Worker>()
            };

            using (WorkerContext db = new WorkerContext())
            {
                db.Workers.Add(newWorker);
                db.SaveChanges();
            }
            return(newWorker);
        }
コード例 #11
0
 static void Main()
 {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     Application.Run(new fmMain());
     using (WorkerContext db = new WorkerContext())
     {
         Worker worker1 = new Worker {
             Name = "Tom", Age = 33
         };
         Worker worker2 = new Worker {
             Name = "Sam", Age = 26
         };
         db.Workers.Add(worker1);
         db.Workers.Add(worker2);
         db.SaveChanges();
     }
 }
コード例 #12
0
        public async Task <IActionResult> ChangeRole(string Username, string Role, string AddRemove)
        {
            IdentityUser user = await _userManager.FindByNameAsync(Username);

            IdentityRole UsrRole = await _roleManager.FindByNameAsync(Role);

            if (AddRemove.Equals("Add"))
            {
                if (Role.Equals("Worker"))
                {
                    // Create new Worker

                    Worker worker = new Worker()
                    {
                        Name = Username,
                        User = user,
                    };
                    _context.Add(worker);
                    _context.SaveChanges();
                }

                await _userManager.AddToRoleAsync(user, UsrRole.Name);

                return(Json(new { success = true, errorMessage = "" }));
            }
            else if (AddRemove.Equals("Remove"))
            {
                if (Role.Equals("Admin"))
                {
                    IList <IdentityUser> AdminUsers = await _userManager.GetUsersInRoleAsync("Admin");

                    if (AdminUsers.Count <= 1)
                    {
                        return(Json(new { success = false, errorMessage = "Can't Remove Last Admin from Database." }));
                    }
                }
                await _userManager.RemoveFromRoleAsync(user, UsrRole.Name);

                return(Json(new { success = true, errorMessage = "" }));
            }
            return(Json(new { success = false, errorMessage = "" }));
        }
コード例 #13
0
        public void ProcessMessage(string message)
        {
            var probe = message.Substring(0, 7);
            var value = message.Substring(8);

            var time = DateTime.Now;

            var data = new ProbeData
            {
                ProbeName   = probe,
                Time        = time,
                Temperature = value
            };

            _context.ProbeData.Add(data);

            WriteToFile(data);

            _context.SaveChanges();
        }
コード例 #14
0
        /// <summary>
        /// Adds a new sector to the database
        /// </summary>
        /// <param name="sector">the sector that is being searched if it exists</param>
        /// <returns>a new sector</returns>
        public tblSector AddSector(tblSector sector)
        {
            try
            {
                using (WorkerContext context = new WorkerContext())
                {
                    // Check if that Sector name already exists
                    for (int i = 0; i < GetAllSectors().Count; i++)
                    {
                        if (sector.SectorName == GetAllSectors()[i].SectorName)
                        {
                            sector.SectorName = GetAllSectors()[i].SectorName;
                            sector.SectorID   = GetAllSectors()[i].SectorID;
                            return(sector);
                        }
                    }

                    if (sector.SectorID == 0)
                    {
                        tblSector newSector = new tblSector();
                        newSector.SectorName = sector.SectorName;

                        context.tblSector.Add(newSector);
                        context.SaveChanges();
                        sector.SectorID = newSector.SectorID;

                        return(sector);
                    }
                    else
                    {
                        return(sector);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }
コード例 #15
0
 public ActionResult Register(RegisterModel model)
 {
     Log.Info(User.Identity.Name, "Home/Register");
     if (ModelState.IsValid)
     {
         Worker worker = null;
         using (WorkerContext db = new WorkerContext())
         {
             worker = db.Workers.FirstOrDefault(u => u.Login == model.Login);
         }
         if (worker == null)
         {
             string sha1pass = HashSHA1Decryption(model.Password).ToLower();
             using (WorkerContext db = new WorkerContext())
             {
                 db.Workers.Add(new Worker {
                     Login = model.Login, PassHash = sha1pass
                 });
                 db.SaveChanges();
                 Log.Info("Create worker: " + worker.Login);
                 worker = db.Workers.Where(u => u.Login == model.Login && u.PassHash == sha1pass).FirstOrDefault();
             }
             if (worker != null)
             {
                 FormsAuthentication.SetAuthCookie(model.Login, true);
                 return(RedirectToAction("Index", "Home"));
             }
         }
         else
         {
             Log.Info(worker.Login + " Пользователь с таким логином уже существует ");
             ModelState.AddModelError("", "Пользователь с таким логином уже существует");
         }
     }
     return(View(model));
 }
コード例 #16
0
        public void Create(TEntity item)
        {
            _dbSet.Add(item);

            _context.SaveChanges();
        }
コード例 #17
0
 public void Save()
 {
     db.SaveChanges();
 }
コード例 #18
0
 public void Create(Worker item)
 {
     context.WorkersItems.Add(item);
     context.SaveChanges();
 }