Пример #1
0
        public IActionResult Register(string email, string username, string password)
        {
            if (email.Length > 2 && username.Length > 2 && password.Length > 2)
            {
                //Check if email or username already exist
                int existsUser  = _context.Users.Where(u => u.Username == username).Count();
                int existsEmail = _context.Users.Where(u => u.Email == email).Count();

                if (existsUser > 0 || existsEmail > 0)
                {
                    if (existsUser > 0)
                    {
                        ViewBag.Message = "Nome de utilizador já existente.";
                    }
                    if (existsEmail > 0)
                    {
                        ViewBag.Message = "Email já existente.";
                    }

                    return(View());
                }

                // Create new user and save it on db
                User u = new User {
                    Email = email, Username = username, Password = password, Answers = new List <Answer>()
                };
                _context.Users.Add(u);
                _context.SaveChanges();

                // Guarda informacao do utilizador na sessao
                HttpContext.Session.SetString("Username", u.Username);
                HttpContext.Session.SetInt32("Userid", u.UserID);

                // guardar o ultimo loggin feito pelo utilizador
                Response.Cookies.Append("LastLoggedTime", DateTime.Now.ToString());

                return(RedirectToAction("DashBoard"));
            }

            else
            {
                ViewBag.Message = "Dados inválidos!";
            }

            return(View());
        }
Пример #2
0
        public IActionResult CreateAnswer(String Content)
        {
            // get room
            int room_id = (int)HttpContext.Session.GetInt32("RoomID");
            // get logged user
            int loggedUser = (int)HttpContext.Session.GetInt32("Userid");
            //get QuestionID
            int questionID = (int)HttpContext.Session.GetInt32("QuestionID");

            // Create new answer and save it on db
            Answer a;

            // If it's a mentor answering automatically mark as valid
            int usertype = _context.UserRooms.Where(ur => ur.RoomID == room_id && ur.UserID == loggedUser).FirstOrDefault().UserTypeID;

            if (_context.UserTypes.Find(usertype).Description == "Mentor" || _context.UserTypes.Find(usertype).Description == "Admin")
            {
                a = new Answer {
                    Content = Content, Date = DateTime.Now, QuestionID = questionID, UserID = loggedUser, Valid = true, UserName = _context.Users.Find(loggedUser).Username
                };
            }
            else
            {
                a = new Answer {
                    Content = Content, Date = DateTime.Now, QuestionID = questionID, UserID = loggedUser, Valid = false, UserName = _context.Users.Find(loggedUser).Username
                }
            };
            _context.Answers.Add(a);

            // Notify owner of the question and followers
            Question     q = _context.Questions.Find(questionID);
            Notification n = new Notification {
                Type     = "New answer on question " + q.Title,
                Date     = DateTime.Now,
                RoomID   = room_id,
                RoomName = _context.Rooms.Find(room_id).Nome,
                UserName = _context.Users.Find(loggedUser).Username,
                UserID   = loggedUser
            };

            _context.Add(n);

            // Owner
            NotificationUser owner = new NotificationUser {
                UserID = q.UserID, Notification = n, Visible = 1
            };

            if (owner.UserID != loggedUser)
            {
                _context.Add(owner);
            }

            // Followers
            foreach (FollowerQuestion fq in _context.FollowerQuestions.Where(fq => fq.FollowingID == q.QuestionID))
            {
                NotificationUser nu = new NotificationUser {
                    UserID = fq.FollowerID, Notification = n
                };
                if (nu.UserID != loggedUser && nu.UserID != owner.UserID)
                {
                    _context.Add(nu);
                }
            }
            _context.SaveChanges();


            return(RedirectToAction("EnterQuestion", new { id = questionID }));
        }
Пример #3
0
        public IActionResult AssociateTags(string Name)
        {
            try
            {
                string username = HttpContext.Session.GetString("Username");
                int    userid   = (int)HttpContext.Session.GetInt32("Userid");

                int roomID = (int)HttpContext.Session.GetInt32("RoomID");

                // Create new Tag and save it on db
                Tag t = new Tag {
                    Name = Name
                };
                _context.Tags.Add(t);
                _context.SaveChanges();

                // Create new TagRoom and save it on db
                TagRoom tr = new TagRoom {
                    RoomID = roomID, TagID = t.TagID
                };
                _context.TagRooms.Add(tr);
                _context.SaveChanges();

                ViewBag.UserName = username;

                ViewBag.Name     = Name;
                ViewBag.RoomName = HttpContext.Session.GetString("RoomName");
            }

            catch
            {
                ViewBag.Message = "Erro ao criar tag";
            }
            return(View("Tag"));
        }