Exemplo n.º 1
0
        public User GetUserInfoByLogin(string login)
        {
            try
            {
                List <User> res  = new List <User>();
                Statement   stmt = new Statement();
                stmt.SetNamespace(Options.Namespace);
                stmt.SetSetName(Options.Set.User);
                stmt.SetBinNames("Id", "Login", "PasswordHash", "Email", "IsActive");
                stmt.Filter = Filter.Equal("Login", login);
                RecordSet rs = Client.Query((QueryPolicy)Policy, stmt);
                while (rs.Next())
                {
                    Record r = rs.Record;
                    if (r.GetBool("IsActive"))
                    {
                        res.Add(new User()
                        {
                            Id    = r.GetInt("Id"),
                            Login = login,
                            Email = r.GetString("Email"),
                        });
                    }
                }

                rs.Dispose();

                if (res.Count == 1)
                {
                    return(res[0]);
                }
                if (res.Count == 0)
                {
                    throw new UserNotFoundException
                              ($"The user with login={login} doesn't exist.");
                }
                if (res.Count > 1)
                {
                    throw new DatabaseQueryException
                              ($"Too many users with login={login}: {res.Count}.");
                }
                else
                {
                    throw new DatabaseQueryException();
                }
            }
            catch (AerospikeException e)
            {
                throw new DatabaseQueryException
                          ("Please create User:Login index first.", e);
            }
            catch (Exception e)
            {
                throw new DatabaseQueryException("Can't get user by login", e);
            }
        }