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));
        }
Example #2
0
        public async Task <IActionResult> AddReview(Reviews r, int?id)
        {
            var movie = await _context.Movies.SingleOrDefaultAsync(u => u.Id == id);

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

            return(View(MovieProfile(id)));
        }
        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));
        }