示例#1
0
        public async Task <AirlineAdminResponse> AddAirlineAdminAsync(AirlineAdmin admin)
        {
            var temp = await context.AirlineAdmins.FindAsync(admin.Username);

            if (temp == null)
            {
                context.AirlineAdmins.Add(admin);
                return(new AirlineAdminResponse(admin));
            }
            else
            {
                return(new AirlineAdminResponse("Admin with given username already exists."));
            }
        }
示例#2
0
 public async Task <IdentityResult> RegisterAirlineAdmin(AirlineAdmin admin, string password)
 {
     return(await userManager.CreateAsync(admin, password));
 }
示例#3
0
        public async Task <IActionResult> RegisterAirlineAdmin([FromBody] RegisterAirlineAdminDto registerDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Model is invalid."));
            }

            try
            {
                string userId   = User.Claims.First(c => c.Type == "UserID").Value;
                string userRole = User.Claims.First(c => c.Type == "Roles").Value;
                var    user     = await unitOfWork.UserManager.FindByIdAsync(userId);

                if (!userRole.Equals("Admin"))
                {
                    return(Unauthorized());
                }

                if (user == null)
                {
                    return(NotFound("User not found"));
                }

                //if ((await unitOfWork.AuthenticationRepository.GetPersonByEmail(registerDto.Email)) != null)
                //{
                //    return BadRequest("User with that email already exists!");
                //}
                if ((await unitOfWork.AuthenticationRepository.GetPersonByUserName(registerDto.UserName)) != null)
                {
                    return(BadRequest("User with that usermane already exists!"));
                }
                if (registerDto.Password.Length > 20 || registerDto.Password.Length < 8)
                {
                    return(BadRequest("Password length has to be between 8-20"));
                }
                if (!unitOfWork.AuthenticationRepository.CheckPasswordMatch(registerDto.Password, registerDto.ConfirmPassword))
                {
                    return(BadRequest("Passwords dont match"));
                }

                var admin = new AirlineAdmin()
                {
                    Email    = registerDto.Email,
                    UserName = registerDto.UserName,
                };
                //var address = new Address() {
                //    City = registerDto.Address.City,
                //    State = registerDto.Address.State,
                //    Lat = registerDto.Address.Lat,
                //    Lon = registerDto.Address.Lon
                //};
                var airline = new Airline()
                {
                    Name    = registerDto.Name,
                    Address = new Address()
                    {
                        City  = registerDto.Address.City,
                        State = registerDto.Address.State,
                        Lat   = registerDto.Address.Lat,
                        Lon   = registerDto.Address.Lon
                    },
                    Admin = (AirlineAdmin)admin
                };

                admin.Airline = airline;

                //using (var transaction = new TransactionScope())
                //{
                try
                {
                    await this.unitOfWork.AuthenticationRepository.RegisterAirlineAdmin(admin, registerDto.Password);

                    await unitOfWork.AuthenticationRepository.AddToRole(admin, "AirlineAdmin");

                    //await transaction.Result.CommitAsync();
                    await unitOfWork.Commit();
                }
                catch (Exception)
                {
                    //await transaction.Result.RollbackAsync();
                    //unitOfWork.Rollback();
                    //transaction.Dispose();
                    return(StatusCode(500, "Failed to register airline admin. One of transactions failed"));
                }
                //}
                return(Ok());
            }
            catch (Exception)
            {
                return(StatusCode(500, "Failed to register airline admin"));
            }
        }