Beispiel #1
0
        public IQueryable <User> user_login([Service] Demo_Context context, string email, string password)
        {
            if (string.IsNullOrEmpty(email))
            {
                throw new QueryException(
                          ErrorBuilder.New()
                          .SetMessage("The email cannot be empty.")
                          .SetCode("EMAIL_EMPTY")
                          .Build());
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new QueryException(
                          ErrorBuilder.New()
                          .SetMessage("The password cannot be empty.")
                          .SetCode("PASSWORD_EMPTY")
                          .Build());
            }
            User user    = context.users.FirstOrDefault(u => u.email == email);
            bool success = PBKDF2.verify(user.pwhash, password, 16, 20, 100000);

            if (success)
            {
                log.Information("The {@User} logged in: {success}", user, success);
                return(context.users.Where(u => u.id == user.id));
            }

            return(null);
        }
Beispiel #2
0
        public IQueryable <User> user_register([Service] Demo_Context context, string email, string password)
        {
            if (string.IsNullOrEmpty(email))
            {
                throw new QueryException(
                          ErrorBuilder.New()
                          .SetMessage("The email cannot be empty.")
                          .SetCode("EMAIL_EMPTY")
                          .Build());
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new QueryException(
                          ErrorBuilder.New()
                          .SetMessage("The password cannot be empty.")
                          .SetCode("PASSWORD_EMPTY")
                          .Build());
            }

            User user = new User {
                email = email
            };

            user.guid   = Guid.NewGuid();
            user.pwhash = PBKDF2.genhash(password, 16, 20, 100000);

            log.Information("Adding {@User}", user);
            context.users.Add(user);
            try
            {
                context.SaveChanges();
            }
            catch
            {
                return(null);
            }


            context.books.Add(new Book {
                author_id = user.id, name = "Hello1"
            });
            context.books.Add(new Book {
                author_id = user.id, name = "Hello2"
            });
            context.books.Add(new Book {
                author_id = user.id, name = "Hello3"
            });
            try
            {
                context.SaveChanges();
            }
            catch
            {
                return(null);
            }

            return(context.users.Where(u => u.id == user.id));
        }
Beispiel #3
0
 public IQueryable <User> GetUserById([Service] Demo_Context context, [ID(nameof(User))] int id)
 {
     return(context.users.Where(u => u.id == id));
 }
Beispiel #4
0
 public IQueryable <User> GetUsers([Service] Demo_Context context)
 {
     return(context.users);
 }
Beispiel #5
0
 public IQueryable <Book> GetBooks(User user, [Service] Demo_Context context)
 {
     return(context.books.Where(b => b.author_id == user.id));
 }
Beispiel #6
0
 public IQueryable <Book> GetBookById([Service] Demo_Context context, [ID(nameof(Book))] int id)
 {
     return(context.books.Where(b => b.id == id));
 }
Beispiel #7
0
 public IQueryable <Book> GetBooks([Service] Demo_Context context)
 {
     return(context.books);
 }
Beispiel #8
0
 public IQueryable <User> GetAuthor(Book book, [Service] Demo_Context context)
 {
     return(context.users.Where(u => u.id == book.author_id));
 }