Esempio n. 1
0
        public IActionResult Register(UserRegister user)
        {
            if (ModelState.IsValid)
            {
                User logged = _context.users.SingleOrDefault(we => we.email == user.Email);

                if (logged != null)
                {
                    TempData["Rerror"] = "Email already registered";
                    return(View("Index", user));
                }

                if (!(user.Password.Any(c => char.IsLetter(c)) && user.Password.Any(c => char.IsDigit(c)) && user.Password.Any(c => char.IsSymbol(c))))
                {
                    TempData["Rerror"] = "Password must contain 1 number, 1 letter, and a special character";
                    return(View("Index", user));
                }

                string hashed = HashedPass(user.Password);

                User newUser = new User
                {
                    firstName = user.FirstName,
                    lastName  = user.LastName,
                    email     = user.Email,
                    password  = hashed,
                };
                _context.users.Add(newUser);
                _context.SaveChanges();

                logged = _context.users.SingleOrDefault(we => we.email == user.Email);
                int userId = logged.id;
                HttpContext.Session.SetInt32("loggedId", userId);

                return(RedirectToAction("Index", "Home"));
            }
            return(View("Index", user));
        }
Esempio n. 2
0
        public IActionResult CreateActivity(ActivitieCreate info, string durationTime)
        {
            int?loggedId = HttpContext.Session.GetInt32("loggedId");

            if (loggedId == null)
            {
                return(RedirectToAction("Index", "LoginReg"));
            }

            if (ModelState.IsValid)
            {
                if (info.startDate < DateTime.Now)
                {
                    TempData["Error"] = "Start Date must be in the Future";
                    return(RedirectToAction("NewActivity", info));
                }

                User logged = _context.users.SingleOrDefault(we => we.id == loggedId);

                Activitie newAct = new Activitie
                {
                    title       = info.title,
                    startDate   = info.startDate,
                    duration    = info.duration.ToString() + " " + durationTime,
                    description = info.description,
                    userId      = logged.id,
                    user        = logged
                };

                List <Participant> participants = _context.participants.Include(stuff => stuff.activities).Where(we => we.usersId == loggedId).ToList();

                foreach (Participant part in participants)
                {
                    if (part.activities.startDate > newAct.startDate)
                    {
                        if (!(part.activities.startDate > AddDuration(newAct.startDate, newAct.duration)))
                        {
                            TempData["Error"] = $"{newAct.title} conflicts with {part.activities.title} in which you are participating";
                            return(View("NewActivity", info));
                        }
                    }
                    if (part.activities.startDate <= newAct.startDate)
                    {
                        if (!(AddDuration(part.activities.startDate, part.activities.duration) <= newAct.startDate))
                        {
                            TempData["Error"] = $"{newAct.title} conflicts with {part.activities.title} in which you are participating";
                            return(View("NewActivity", info));
                        }
                    }
                }

                _context.activities.Add(newAct);
                _context.SaveChanges();

                Participant newPart = new Participant
                {
                    activitiesId = newAct.id,
                    activities   = newAct,
                    usersId      = logged.id,
                    users        = logged,
                };
                _context.participants.Add(newPart);
                _context.SaveChanges();
                return(RedirectToAction("ActivityInfo", new { activityId = newAct.id }));
            }
            return(View("NewActivity", info));
        }