Пример #1
0
        public async Task <ActionResult <UserDTO> > Login(string username, string password)
        {
            UserAccount userAccount = await _userAccountRepository.GetByUsername(username);

            if (userAccount != null)
            {
                if (userAccount.AccountTypeId == 1)
                {
                    Administrator userAdmin = await _administratorRepository.GetByUserAccountId(userAccount.Id);

                    if (userAdmin != null)
                    {
                        if (userAccount.Password != password)
                        {
                            return(BadRequest());
                        }
                        UserDTO userDTO = new UserDTO
                        {
                            FirstName     = userAdmin.FirstName,
                            LastName      = userAdmin.LastName,
                            UserId        = userAdmin.Id,
                            Username      = userAccount.Username,
                            Password      = userAccount.Password,
                            AvatarUrl     = userAccount.AvatarUrl,
                            AccountTypeId = userAccount.AccountTypeId,
                            AccountType   = userAccount.AccountType.Type,
                            UserAccountId = userAccount.Id,
                            Email         = userAdmin.Email,
                            Phone         = userAdmin.Phone
                        };
                        return(Ok(userDTO));
                    }
                }
                else if (userAccount.AccountTypeId == 2)
                {
                    Educator userEducator = await _educatorRepository.GetByUserAccountId(userAccount.Id);

                    if (userEducator != null)
                    {
                        if (userAccount.Password != password)
                        {
                            return(BadRequest());
                        }
                        UserDTO userDTO = new UserDTO
                        {
                            FirstName     = userEducator.FirstName,
                            LastName      = userEducator.LastName,
                            UserId        = userEducator.Id,
                            Username      = userAccount.Username,
                            Password      = userAccount.Password,
                            AvatarUrl     = userAccount.AvatarUrl,
                            AccountTypeId = userAccount.AccountTypeId,
                            AccountType   = userAccount.AccountType.Type,
                            UserAccountId = userAccount.Id,
                            Phone         = userEducator.Phone,
                            Email         = userEducator.Email
                        };
                        return(Ok(userDTO));
                    }
                }
                else if (userAccount.AccountTypeId == 3)
                {
                    Student userStudent = await _studentRepository.GetByUserAccountId(userAccount.Id);

                    if (userStudent != null)
                    {
                        if (userAccount.Password != password)
                        {
                            return(BadRequest());
                        }
                        UserDTO userDTO = new UserDTO
                        {
                            FirstName     = userStudent.FirstName,
                            LastName      = userStudent.LastName,
                            UserId        = userStudent.Id,
                            Username      = userAccount.Username,
                            Password      = userAccount.Password,
                            AvatarUrl     = userAccount.AvatarUrl,
                            AccountTypeId = userAccount.AccountTypeId,
                            AccountType   = userAccount.AccountType.Type,
                            UserAccountId = userAccount.Id,
                            Email         = userStudent.Email,
                            Phone         = userStudent.Phone
                        };
                        return(Ok(userDTO));
                    }
                }
                else
                {
                    return(NotFound());
                }
            }

            return(NotFound());
        }