예제 #1
0
        public static void Seed()
        {
            var context  = new UserContext();
            var existing = context.GetAll <Question>(UserContext.ReadType.Shallow);

            //var parallel = 10;
            //var samplesPerYear = 10;
            if (existing.Length == 0)
            {
                var questions = new List <Question>();
                var json      = DefaultQuestions.Questions;
                var data      = JsonConvert.DeserializeObject <QuestionData[]>(json);

                foreach (var item in data)
                {
                    questions.Add(new Question(item.question, item.year));
                }

                for (int index = 0; index < questions.Count; index++)
                {
                    var question = questions[index];
                    context.AddDefault(question);
                    Console.WriteLine($"{index + 1}/{questions.Count}");
                }
            }
        }
예제 #2
0
        public Id?ImportBook(UserContext context, Id importId, string cover, string link)
        {
            context.ValidateUser(true);
            var import = UserContext.GetShallow <Import>(importId);

            if (import == null)
            {
                return(null);
            }
            var(title, author) = BigBookSearchUtil.SearchAmazon(link);
            var files = Directory.GetFiles(import.Path)
                        .Where(ImportRunner.IsAudioFile)
                        .ToList();
            double length;

            length = AudioFileUtil.GetLength(
                files.Count == 1 ?
                files.First() :
                AudioFileUtil.JoinFilesWithFfmpeg(import.Path));

            var book = new Book(title, author, import.Path, cover, length);

            UserContext.AddDefault(book);
            UserContext.Remove <Import>(importId);
            return(book.Id);
        }
예제 #3
0
        private static void Trigger(object sender, ElapsedEventArgs e)
        {
            var path           = AudioFileUtil.DataPath;
            var imports        = UserContext.GetAllShallow <Import>();
            var books          = UserContext.GetAllShallow <Book>();
            var paths          = FindAudioFolders(path);
            var newImportPaths = paths.Where(p => books.All(book => book.Path != p) && imports.All(book => book.Path != p)).ToList();

            newImportPaths.ForEach(d => UserContext.AddDefault(new Import(d)));
        }
예제 #4
0
        public static Token CreateToken(string username)
        {
            var existing = UserContext.SearchShallow <Token>(expr => expr.Filter(item => item.G("Username") == username))
                           .SingleOrDefault();
            var newToken = new Token(username);

            if (existing != null)
            {
                UserContext.UpdateDefault(newToken, existing.Id);
            }
            else
            {
                UserContext.AddDefault(newToken);
            }
            return(newToken);
        }
예제 #5
0
        public bool AddUser(UserContext context, string username, string password, bool admin)
        {
            context.ValidateUser(true);
            if (string.IsNullOrEmpty(username) ||
                string.IsNullOrEmpty(password))
            {
                return(false);
            }
            if (UserContext.SearchShallow <User>(expr => expr.Filter(item => item.G("Username") == username)).Any())
            {
                return(false);
            }

            UserContext.AddDefault(new User(username, password, admin));

            return(true);
        }
예제 #6
0
        public void ThreeTierShoudWork()
        {
            var dir      = Directory.GetCurrentDirectory();
            var assembly = Assembly.LoadFrom(Path.Combine(dir, "Rekyl.Test.dll"));

            UserContext.SetOverrideAssembly(assembly);
            UserContext.InitDb(new DatabaseUrl("localhost"), new DatabaseName("rekylTest"));
            UserContext.Reset();
            var test3 = new Test3();
            var test2 = new Test2(test3);
            var test1 = new Test1(new[] { test2 }, new[] { "a", "b" }, "sdfli", 0);

            UserContext.AddDefault(test3);
            UserContext.AddDefault(test2);
            UserContext.AddDefault(test1);
            var context = new TestContext("query{test1{test2{test3{id}}}}");
            var data    = UserContext.GetFull <Test1>(test1.Id);
        }
예제 #7
0
        public DefaultResult <User> SetUserName(UserContext context, string userName)
        {
            var  existingUser = context.Authorize(AuthMode.UserId);
            User newUser;

            if (existingUser == null)
            {
                newUser = new User(context.UserName, userName);
                context.AddDefault(newUser);
            }
            else
            {
                newUser = new User(existingUser.UserId, userName);
                context.UpdateDefault(newUser, existingUser.Id);
            }

            return(new DefaultResult <User>(newUser));
        }
예제 #8
0
        public BookResult[] Books(UserContext context)
        {
            var user       = context.ValidateUser();
            var books      = UserContext.GetAllShallow <Book>();
            var searchQl   = "query{dummy{user{id} book{id author title cover length} value lastPlayed id}}";
            var progresses =
                new UserContext(searchQl).Search <Progress>(expr => expr.Filter(item => item.G("User") == user.Id.ToString())).ToList();

            foreach (var book in books)
            {
                if (progresses.Any(d => d.Book.Id == book.Id))
                {
                    continue;
                }
                var progress = new Progress(book, user, 0, DateTime.Parse("1970-01-01"));
                UserContext.AddDefault(progress);
                progresses.Add(progress);
            }
            var ret = progresses.Select(d => new BookResult(d)).ToArray();

            return(ret);
        }
예제 #9
0
        public LoginResult Login(UserContext context, string username, string password)
        {
            var  users = UserContext.GetAllShallow <User>();
            User user;

            if (!users.Any())
            {
                user = new User(username, password, true);
                UserContext.AddDefault(user);
            }
            else
            {
                user = users.SingleOrDefault(d => d.Username == username);
                if (user == null || !LoginUtil.ValidatePassword(password, user.PasswordHash))
                {
                    return(null);
                }
            }
            return(new LoginResult {
                Token = LoginUtil.CreateToken(user.Username).LoginToken, Admin = user.Admin
            });
        }
예제 #10
0
        public DefaultResult <StandardGame> StartNewStandardGame(UserContext context)
        {
            var user = context.Authorize();
            var seed = QuestionUtil.GetRandomQuestion();

            Question firstQuestion;

            while (true)
            {
                firstQuestion = QuestionUtil.GetRandomQuestion();
                if (firstQuestion.Id != seed.Id)
                {
                    break;
                }
            }

            var game = new StandardGame(firstQuestion, new[] { seed }, 3, user);

            context.AddDefault(game);

            game.Scrub();
            return(new DefaultResult <StandardGame>(game));
        }
예제 #11
0
        public DefaultResult <ElevateItem> AddElevateItem(
            UserContext context,
            NonNull <string> firstname,
            NonNull <string> lastname,
            int number,
            NonNull <string> colour,
            NonNull <string> text)
        {
            var newItem = new ElevateItem(firstname, lastname, number, colour, text);

            if (
                number < 0 ||
                number > 1000 ||
                firstname.ToString() == "undefined" ||
                lastname.ToString() == "undefined" ||
                colour.ToString() == "undefined" ||
                text.ToString() == "undefined"
                )
            {
                return(null);
            }
            context.AddDefault(newItem);
            return(new DefaultResult <ElevateItem>(newItem));
        }