public async Task <IActionResult> Create([Bind("review")] AddReviewDTO dto)
        {
            int    movieId  = (int)HttpContext.Session.GetInt32("CurrentMovieId");
            int    userId   = (int)HttpContext.Session.GetInt32("CurrentUserId");
            string username = HttpContext.Session.GetString("CurrentUsername");

            var reviews = await _context.Reviews.SingleOrDefaultAsync(u => u.userId == userId &&
                                                                      u.movieId == movieId);

            if (reviews != null)
            {
                ModelState.AddModelError("", "you already reviewed this movie."); //we should never get here
                return(View());
            }

            var review = new Reviews
            {
                userId     = userId,
                movieId    = movieId,
                userName   = username,
                review     = dto.review,
                timePosted = DateTime.Now
            };

            _context.Add(review);
            await _context.SaveChangesAsync();

            return(View("Details", review));
        }
        public async Task <IActionResult> Signup([Bind("email,password,firstName,lastName,country")] DTO.RegisterDTO dto)
        {
            var users = await _context.Users.SingleOrDefaultAsync(u => u.email == dto.email);

            if (users != null)
            {
                ModelState.AddModelError("", "User with this email already exist.");
                dto.email = "";
                return(View());
            }

            byte[] passwordBytes = Encoding.ASCII.GetBytes(dto.password);
            var    md5           = new MD5CryptoServiceProvider();

            byte[] md5data      = md5.ComputeHash(passwordBytes);
            string passwordHash = Encoding.ASCII.GetString(md5data);

            var user = new Users
            {
                email        = dto.email,
                firstName    = dto.firstName,
                lastName     = dto.lastName,
                passwordHash = passwordHash,
                country      = dto.country,
                admin        = false //default
            };

            //updating flags in session
            HttpContext.Session.SetInt32("isSignedIn", 1);
            HttpContext.Session.SetInt32("Role", 1);

            HttpContext.Session.SetInt32("CurrentUserId", user.Id);
            HttpContext.Session.SetString("CurrentUsername", user.firstName + " " + user.lastName);

            //HttpContext.Session.SetInt32("CurrentUserId", users.Id);

            _context.Add(user);
            await _context.SaveChangesAsync();

            return(View("Profile", user));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> AddMovie([Bind("Title,ReleaseDate,Genre,Length")] AddMovieDTO dto)
        {
            var movies = await _context.Movies.SingleOrDefaultAsync(u => u.Title == dto.Title);

            if (movies != null)
            {
                ModelState.AddModelError("", "This movie already exists.");
                dto.Title = "";
                return(View());
            }

            var movie = new Movies
            {
                Title       = dto.Title,
                ReleaseDate = dto.ReleaseDate,
                Genre       = dto.Genre,
                Length      = int.Parse(dto.Length)
            };

            _context.Add(movie);
            await _context.SaveChangesAsync();

            return(View("MovieProfile", movie));
        }