Exemplo n.º 1
0
        public ActionResult Register(Pub newPub, HttpPostedFileBase upImage)
        {
            // Verify the if the model is valid
            if (ModelState.IsValid == false)
            {
                ModelState.AddModelError("", "Error - Check information and try again");
                return(View(newPub));
            }

            // Get the user from the session that the User Controller generated with the basic data from the user
            var newUser = (User)System.Web.HttpContext.Current.Session["user"];

            // If there is nothing in the user session, return to the user registration page
            if (newUser == null)
            {
                return(RedirectToAction("Register", "User"));
            }

            // Get the coordinates of the bar location that the user has given
            Tuple <double, double> tuple = GoogleGeoLocation.GetCoordinates(newPub.Address, newPub.City, newPub.State);

            // Set the remaining properties of the model
            newPub.Lat               = tuple.Item1;
            newPub.Lng               = tuple.Item2;
            newPub.Photo             = ImageHandler.HttpPostedFileBaseToByteArray(upImage);
            newPub.PhotoType         = upImage.ContentType;
            newPub.LayoutStyle       = "dark";
            newUser.RegistrationDate = DateTime.Now;
            newUser.UserType         = nameof(Pub);
            newUser.Password         = CryptSharp.Crypter.MD5.Crypt(newUser.Password);

            // Insert in the database, if successful
            var returnedUser = UserDAO.Insert(newUser);

            newPub.UserId = returnedUser.Id;
            var returnedPub = PubDAO.Insert(newPub);

            if (returnedPub == null || returnedUser == null)
            {
                ModelState.AddModelError("", "Error - Check information and try again");
                return(View(newPub));
            }

            // Generate a session with the user database id
            UserSession.ReturnPubId(returnedPub.Id);
            UserSession.ReturnUserId(returnedPub.UserId);

            System.Web.HttpContext.Current.Session["user"] = null;
            return(RedirectToAction("Dashboard", "Pub"));
        }
Exemplo n.º 2
0
        public ActionResult Register(Pub newPub, HttpPostedFileBase upImage)
        {
            if (ModelState.IsValid == true)
            {
                Tuple <double, double> tuple = GoogleGeoLocation.GetCoordinates(newPub.Address, newPub.State);
                if (upImage == null)
                {
                    newPub.PhotoUrl  = null;
                    newPub.PhotoType = null;
                }
                else
                {
                    newPub.PhotoUrl  = ImageHandler.HttpPostedFileBaseToByteArray(upImage);
                    newPub.PhotoType = upImage.ContentType;
                }

                newPub.RegistrationDate = DateTime.Now;
                newPub.Lat = tuple.Item1;
                newPub.Lng = tuple.Item2;

                var returnedPub = PubDAO.Insert(newPub);
                if (returnedPub == null)
                {
                    ModelState.AddModelError("", "Error - Check information and try again");
                    return(View(newPub));
                }
                else
                {
                    UserSession.ReturnPubId(returnedPub.Id);
                    return(RedirectToAction("Dashboard", "Pub"));
                }
            }
            else
            {
                ModelState.AddModelError("", "Error - Check information and try again");
                return(View(newPub));
            }
        }