public async Task <IActionResult> CreateCarAd(BikeAndCarViewModel compositeModel)
        {
            // Google reCaptcha
            var googleRecaptcha = googleRecaptchaService.VerifyRecaptcha(compositeModel.RecaptchaToken);

            if (!googleRecaptcha.Result.success && googleRecaptcha.Result.score <= 0.5)
            {
                ModelState.AddModelError("", "Captcha failed, please try again");
                compositeModel.car.CarTypes = initializeCarModel().CarTypes;
                compositeModel.Currencies   = new SelectList(currencyContainer.GetCurrencyNameList());
                return(View("CreateAdvertise", compositeModel));
            }

            if (ModelState.IsValid)
            {
                var    model          = compositeModel.car;
                string uniqueFileName = null;
                if (model.Picture != null)
                {
                    uniqueFileName = ProcessUploadedPhoto(model.Picture);
                }

                var user = await userManager.GetUserAsync(User);

                if (user == null)
                {
                    ViewBag.ErrorMessage = "User cannot be found";
                    return(View("NotFound"));
                }
                else
                {
                    Advertisment advertisment = new Advertisment
                    {
                        Title           = model.Title,
                        PostDate        = DateTime.Now,
                        ApplicationUser = user,
                        Item            = new AutoItem
                        {
                            Price       = Double.Parse(model.Price),
                            Brand       = model.Brand,
                            Car_Type    = model.Car_Type,
                            ProductAge  = model.ProductAge,
                            Doors       = Int32.Parse(model.Doors),
                            Description = model.Description,
                            Seats       = Int32.Parse(model.Seats),
                            Mileage     = Int32.Parse(model.Mileage),
                            CurrencyId  = currencyContainer.GetIdByName(model.CurrencyName)
                        },
                    };

                    if (uniqueFileName != null)
                    {
                        advertisment.Picture = uniqueFileName;
                    }

                    try
                    {
                        await advertismentRepository.Add(advertisment);

                        InitializeResultView(true, "You have successfuly created a new article", "MyAdvertisments", "Advertisment", "my articles");
                    }
                    catch (Exception e)
                    {
                        InitializeResultView(false, "Failed to create new article", "CreateCarAD", "Advertisment", "");
                    }

                    return(View("ResultView"));
                }
            }
            compositeModel.Currencies   = new SelectList(currencyContainer.GetCurrencyNameList());
            compositeModel.car.CarTypes = initializeCarModel().CarTypes;
            return(View("CreateAdvertise", compositeModel));
        }
Exemple #2
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            // Google reCaptcha
            var googleRecaptcha = googleRecaptchaService.VerifyRecaptcha(model.RecaptchaToken);

            if (!googleRecaptcha.Result.success && googleRecaptcha.Result.score <= 0.5)
            {
                ModelState.AddModelError("", "Captcha failed, please try again");
                return(View(model));
            }

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    Email = model.Email, UserName = model.Email
                };
                var result = await userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var token = await userManager.GenerateEmailConfirmationTokenAsync(user);

                    var confirmationLink = Url.Action("ConfirmEmail", "Account",
                                                      new { userId = user.Id, token = token }, Request.Scheme);


                    if (signInManager.IsSignedIn(User) && (User.IsInRole("Admin") || User.IsInRole("Super Admin")))
                    {
                        return(RedirectToAction("ListUsers", "Administration"));
                    }


                    var status = await emailHandler.SendEmail(user.Email, "Registration Confirmation", "Click the link below to confirm your email\n" + confirmationLink);

                    if (status)
                    {
                        ViewBag.Title           = "Successful";
                        ViewBag.OperationResult = "Success";
                        ViewBag.Message         = "You have successfuly registered!\nBefore you can Login, please confirm your " +
                                                  "email\n, by clicking on the confirmation link we have emailed you ";
                        ViewBag.Controller = "Account";
                        ViewBag.Action     = "Login";
                        ViewBag.NextAction = "Login";
                    }
                    else
                    {
                        ViewBag.Title           = "Registration failed";
                        ViewBag.OperationResult = "Registration failed";
                        ViewBag.Message         = "The email you provided is invalid";
                        ViewBag.Controller      = "Account";
                        ViewBag.Action          = "Register";
                    }

                    return(View("ResultView"));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }

            return(View(model));
        }