public HomeController(VacantionContext context, UserManager <User> userManager,
                       SignInManager <User> signInManager, RoleManager <Role> roleManager)
 {
     _context       = context;
     _userManager   = userManager;
     _signInManager = signInManager;
     _roleManager   = roleManager;
     foreach (var role in roles)
     {
         if (!_roleManager.Roles.Any(x => x.Name == role))
         {
             _context.Add(new Role()
             {
                 Name = role, NormalizedName = role.ToUpper()
             });
             _context.SaveChanges();
         }
     }
     if (!_context.Teams.Any(x => x.TeamName == "-"))
     {
         _context.Add(new Team()
         {
             TeamName = "-"
         });
         _context.SaveChanges();
     }
 }
Ejemplo n.º 2
0
        public async Task <IActionResult> SendRequest(VacationsCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                User currentUser = await GetCurrentUser();

                Vacantion newVacation = new Vacantion
                {
                    FromDate         = model.FromDate,
                    ToDate           = model.ToDate,
                    Type             = model.Type,
                    FromUser         = currentUser,
                    HalfDayVacantion = model.HalfDayVacantion
                };

                bool mark = false;
                if (model.FromDate > model.ToDate)
                {
                    ModelState.AddModelError("FromDate", "From Date must be less than To Date.");
                    ModelState.AddModelError("ToDate", "From Date must be less than To Date.");
                    mark = true;
                }

                if (model.Type == "sick" && model.ImageUpload == null)
                {
                    ModelState.AddModelError("ImageUpload", "Must upload image of sheet or record.");
                    mark = true;
                }
                else if (model.ImageUpload != null)
                {
                    if (model.ImageUpload.Length > 0)
                    //Convert Image to byte and save to database
                    {
                        byte[] p1 = null;
                        using (var fs1 = model.ImageUpload.OpenReadStream())
                            using (var ms1 = new MemoryStream())
                            {
                                fs1.CopyTo(ms1);
                                p1 = ms1.ToArray();
                            }
                        newVacation.ImageUpload = p1;
                    }
                }

                if (mark)
                {
                    return(View(model));
                }

                _context.Add(newVacation);
                _context.SaveChanges();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
Ejemplo n.º 3
0
        public IActionResult Create(ProjectsCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                Project newProject = new Project
                {
                    ProjectName = model.ProjectName,
                    Description = model.Description
                };

                _context.Add(newProject);
                _context.SaveChanges();
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
        public async Task <IActionResult> Create(TeamsCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                User currentUser = await GetCurrentUser();

                Team newTeam = new Team
                {
                    //TeamName = model.TeamName,
                    WorkingOnProject = _context.Projects.First(t => t.ProjectName == model.WorkingOnProject),
                    //Developers = model.Developers.ToList();
                };

                if (_context.Teams.Any(x => x.TeamName == model.TeamName))
                {
                    ModelState.AddModelError($"TeamName", "Team with such name exists already.");
                }
                else
                {
                    newTeam.TeamName = model.TeamName;
                }

                try
                {
                    string[] names = model.Leader.Split();
                    newTeam.Leader = _context.Users
                                     .Include(u => u.Role)
                                     .First(u => u.FirstName == names.First() && u.LastName == names.Last());
                    if (newTeam.Leader.Role.Name == "Team Lead")
                    {
                        ModelState.AddModelError("Leader", "This user is already a leader of an existing team.");
                    }
                    else if (newTeam.Leader.Role.Name == "CEO")
                    {
                        ModelState.AddModelError("Leader", "This is the CEO.");
                    }
                    else
                    {
                        newTeam.Leader.Role = _context.Roles.First(r => r.Name == "Team Lead");
                        newTeam.Leader.Team = newTeam;
                    }
                }
                catch
                {
                    ModelState.AddModelError("Leader", "No such user.");
                }


                var developers = model.Developers.Where(c => c != null).ToArray();
                model.BoxesToShow = developers.Count() < 2 ? 2 : developers.Count();

                bool mark = false;
                if (developers.Count() < 2)
                {
                    ModelState.AddModelError("Developers", "Team must have at least two members exept the Leader.");
                    mark = true;
                }

                bool duplicates = false;
                if (developers.Count() != developers.Distinct().Count())
                {
                    duplicates = true;
                }

                for (int i = 0; i < developers.Count(); i++)
                {
                    if (duplicates)
                    {
                        for (int j = i + 1; j < developers.Count(); j++)
                        {
                            if (developers[j] == developers[i])
                            {
                                ModelState.AddModelError($"Developers[{j}]", "Already taken in above choices.");
                                break;
                            }
                        }
                    }

                    string[] names = developers[i].Split();
                    if (_context.Users.Any(u => u.FirstName == names.First() && u.LastName == names.Last()))
                    {
                        User member = _context.Users
                                      .Include(u => u.Role)
                                      .First(u => u.FirstName == names[0] && u.LastName == names[1]);
                        if (member == newTeam.Leader)
                        {
                            ModelState.AddModelError($"Developers[{i}]", "This user is Team Leader of this team.");
                        }
                        else if (member.Role.Name == "Team Lead")
                        {
                            ModelState.AddModelError($"Developers[{i}]", "This user is a leader of an existing team.");
                        }
                        else
                        {
                            member.Team = newTeam;
                            member.Role = _context.Roles.First(r => r.Name == "Developer");
                        }
                    }
                    else
                    {
                        mark = true;
                        ModelState.AddModelError($"Developers[{i}]", "No such user.");
                    }
                }

                if (mark || duplicates)
                {
                    return(View(model));
                }

                _context.Add(newTeam);
                _context.SaveChanges();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }