Ejemplo n.º 1
0
        public IActionResult AcceptCarSaleFunction(int id)
        {
            if (id < 1)
            {
                return(BadRequest("ID is not valid"));
            }

            var carSale = _carSaleService.GetCarSaleByID(id);

            if (carSale == null)
            {
                return(NotFound("This carsale is not in database"));
            }

            var res = _carSaleService.AcceptCarSale(id);

            if (res == true)
            {
                var carSales = _carSaleService.GetAdminCarSales();
                return(CreatedAtAction("Accepted!", carSales));
            }
            else
            {
                return(BadRequest("Could not accept the carsale"));
            }
        }
Ejemplo n.º 2
0
        //[AllowAnonymous]
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> Register([FromBody] PreRegisterViewModel preReg)
        {
            var role = preReg.Role;
            RegisterViewModel model = new RegisterViewModel();

            model.Email = preReg.Email;

            // Make sure that our password contains upper case letter
            do
            {
                model.Password = _identityService.GeneratePassword();
            } while (!model.Password.Any(p => char.IsUpper(p)));

            //model.Password = "******";

            // Check if everything is valid
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    // Assigning the user to the role Carsale
                    var roleResult = await _userManager.AddToRoleAsync(user, role);

                    if (roleResult.Succeeded)
                    {
                        // Assigning the user to its claim
                        var claimResult = await _userManager.AddClaimAsync(user, new System.Security.Claims.Claim(role, role));

                        if (claimResult.Succeeded)
                        {
                            if (role == "Carsale")
                            {
                                // Change the carsale to accepted in database

                                // TODO!! check if these fail... if something fails on the way
                                // we need to remove the carsale from the identity database
                                var carSale  = _carSaleService.GetCarSaleByEmail(model.Email);
                                var res      = _carSaleService.AcceptCarSale(carSale.ID);
                                var carSales = _carSaleService.GetAdminCarSales();

                                // Send email to the user to notify of new password
                                var notificationEmail = _emailSender.CreateCarSaleEmail(model);
                                _emailSender.SendEmail(notificationEmail);

                                return(CreatedAtAction("Registered", carSales));
                            }
                            else if (role == "Admin")
                            {
                                return(CreatedAtAction("Registered", null));
                            }
                        }
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }

            // If we got this far the registration model is not valid
            return(BadRequest(ModelState));
        }