Exemplo n.º 1
0
        public ActionResult AddReview(ReviewModel m)
        {
            string email = Globals.getCurrentPostEmail();

            grabFromDB  DB   = new grabFromDB();
            List <User> user = DB.getUserListByEmail(email);

            if (user.Count != 1)
            {
                //there was an error, must handle
            }
            else
            {
                m.Email     = user[0].email;
                m.Phone_Num = user[0].phone;
            }


            //check for empty fields
            if (m.Email == null || m.Phone_Num == null || m.Reviewer_Email == null || m.Description == null || m.Rating == null)
            {
                ViewBag.EmptyFields = "Please fill out all feilds.";
                return(View("ReviewUser", m));
            }

            //Check for invalid email
            try
            {
                MailAddress mail = new MailAddress(m.Reviewer_Email);
            }
            catch (FormatException)
            {
                ViewBag.InvalidEmail = "This is not a valid email address. Try again.";
                return(View("ReviewUser", m));
            }

            //check description length
            if (m.Description.Length > 400)
            {
                ViewBag.DescriptionLengthError = "Review too long. Try again.";
                return(View("ReviewUser", m));
            }

            //check email length
            if (m.Reviewer_Email.Length > 100)
            {
                ViewBag.InvalidEmail = "Email too long. Try again.";
                return(View("ReviewUser", m));
            }

            int rating;

            if (m.Rating == Enum.GetName(typeof(ratings), 0))
            {
                rating = 1;
            }
            else if (m.Rating == Enum.GetName(typeof(ratings), 1))
            {
                rating = 2;
            }
            else if (m.Rating == Enum.GetName(typeof(ratings), 2))
            {
                rating = 3;
            }
            else if (m.Rating == Enum.GetName(typeof(ratings), 3))
            {
                rating = 4;
            }
            else if (m.Rating == Enum.GetName(typeof(ratings), 4))
            {
                rating = 5;
            }
            else
            {
                //error, should never get here though
                rating = 5;
            }

            DB.insertReview(m.Phone_Num, m.Email, m.Reviewer_Email, m.Description, rating);

            int id = Globals.getCurrentPostId();

            return(RedirectToAction("Textbook/" + id, "Post"));
        }