public RepositoriesService(
     GitDbContext dbContext,
     IValidator validator)
 {
     this.dbContext = dbContext;
     this.validator = validator;
 }
Ejemplo n.º 2
0
        public void SeedGitUsers(int amount = 10000, bool tuned = false)
        {
            var data = Builder <GitHubResume> .CreateListOfSize(amount).All()
                       .With(c => c.PiplMatchedDate = Faker.Date.Past())
                       .With(c => c.Login           = Faker.User.Username())
                       .With(c => c.Location        = Faker.Address.SecondaryAddress())
                       .With(c => c.Name            = Faker.Name.FullName())
                       .With(c => c.Bio             = Faker.Lorem.Paragraph(10))
                       .With(c => c.Email           = Faker.User.Email())
                       .Build();

            using (var context = new GitDbContext())
            {
                if (tuned)
                {
                    context.BulkInsertAsync(data, amount).Wait();
                }
                else
                {
                    context.GitHubResumes.AddRange(data);
                    context.SaveChanges();
                }
                // EFBatchOperation.For(context, context.GitHubResumes).InsertAll(data);
                //context.AttachAndModify(data);
                //context.BulkInsert(data);
                //context.GitHubResumes.AddRange(data);
                // context.SaveChanges();
            }
        }
        /// <summary>
        /// Adds PineBlog GitDb services to the specified services collection.
        /// </summary>
        /// <param name="services">The services available in the application.</param>
        /// <param name="configuration">The application configuration properties.</param>
        /// <returns>The original services object.</returns>
        public static IServiceCollection AddPineBlogGitDb(this IServiceCollection services, IConfiguration configuration)
        {
            if (((IConfigurationRoot)configuration).Providers.SingleOrDefault(p => p.GetType() == typeof(BlogSettingsConfigurationProvider)) == null)
            {
                throw new ConfigurationException("The PineBlog IConfigurationProvider(s) are not configured, please add \"AddPineBlogGitDbConfiguration\" to the \"ConfigureAppConfiguration\" on the \"IWebHostBuilder\".")
                      {
                          HelpLink = "https://github.com/ofpinewood/pineblog/blob/main/docs/getting-started.md#blog-settings-configurationprovider"
                      };
            }

            services.Configure <PineBlogGitDbOptions>(configuration.GetSection(nameof(PineBlogGitDbOptions)));

            services.AddTransient <GitDbContext>(provider =>
            {
                var options = provider.GetRequiredService <IOptions <PineBlogGitDbOptions> >();
                return(GitDbContext.Create(options.Value));
            });
            services.AddTransient <IBlogUnitOfWork, BlogUnitOfWork>();

            services.AddFeatureManagement(configuration);

            services.AddHostedService <GitDbSyncService>();

            return(services);
        }
Ejemplo n.º 4
0
 public CommitsController(
     IValidator validator,
     GitDbContext data)
 {
     this.validator = validator;
     this.data      = data;
 }
Ejemplo n.º 5
0
 public List <GitHubResume> GetAllResumes()
 {
     using (var context = new GitDbContext())
     {
         return(context.GitHubResumes.ToList());
     }
 }
 public RepositoriesController(
     GitDbContext data,
     IValidator validator)
 {
     this.validator = validator;
     this.data      = data;
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Load the blog settings configuration from the repository.
        /// </summary>
        public override void Load()
        {
            var gitDbContext = GitDbContext.Create(_source.Options);

            IDictionary <string, byte[]> files;

            try
            {
                files = gitDbContext.GetFiles(new string[] { PathHelper.Build(_source.Options.RootPath, GitDbConstants.BlogSettingsFile) });
            }
            catch
            {
                return;
            }

            var json     = Encoding.UTF8.GetString(files.Values.Single());
            var settings = JsonSerializer.Deserialize <BlogSettings>(json, new JsonSerializerOptions {
                AllowTrailingCommas = true
            });

            if (settings == null)
            {
                return;
            }

            Data = new Dictionary <string, string>();
            Data.Add($"{nameof(PineBlogOptions)}:{nameof(PineBlogOptions.Title)}", settings.Title);
            Data.Add($"{nameof(PineBlogOptions)}:{nameof(PineBlogOptions.Description)}", settings.Description);
            Data.Add($"{nameof(PineBlogOptions)}:{nameof(PineBlogOptions.CoverCaption)}", settings.CoverCaption);
            Data.Add($"{nameof(PineBlogOptions)}:{nameof(PineBlogOptions.CoverLink)}", settings.CoverLink);
            Data.Add($"{nameof(PineBlogOptions)}:{nameof(PineBlogOptions.CoverUrl)}", settings.CoverUrl);
        }
Ejemplo n.º 8
0
 // <summary>
 /// Get Resumes with complex sql logic that should take some time
 /// </summary>
 /// <returns></returns>
 public int ExecuteComplexQuery()
 {
     using (var context = new GitDbContext())
     {
         return(context.Database.ExecuteSqlCommand("Select * FROM GitHubResume WHERE PiplMatchedDate IN (SELECT top 1000 PiplMatchedDate From GitHubResume ORDER BY NEWID())"));
     }
 }
Ejemplo n.º 9
0
        private void Sync(object state)
        {
            var basePath = _options.Value.LocalRepositoryBasePath;

            if (!Directory.Exists(basePath))
            {
                return;
            }

            // Stop timer until sync finished;
            Timer?.Change(Timeout.Infinite, 0);

            try
            {
                using (var context = GitDbContext.CreateFromLocal(_options.Value))
                {
                    var branchesSynced = context.Sync();
                    if (branchesSynced < 1)
                    {
                        _logger.LogError($"Could not sync repository \"{_options.Value.RepositoryUrl}\".");
                    }

                    CheckBlogSettingsFileChanges(context);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Could not sync repository \"{_options.Value.RepositoryUrl}\".");
            }

            Timer?.Change(TimeSpan.FromSeconds(_options.Value.SyncFrequency), TimeSpan.FromSeconds(_options.Value.SyncFrequency));

            _logger.LogInformation($"GitDbSyncService: \"{_options.Value.RepositoryUrl}\" synced.");
        }
Ejemplo n.º 10
0
        public BlogUnitOfWork(GitDbContext gitDbContext, IOptions <PineBlogGitDbOptions> options)
        {
            _gitDbContext = gitDbContext;

            BlogSettings = new BlogSettingsRepository(_gitDbContext, options);
            Authors      = new AuthorRepository(_gitDbContext, options);
            Posts        = new PostRepository(_gitDbContext, options);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Get Resumes with complex sql logic that should take some time
 /// </summary>
 /// <returns></returns>
 public List <GitHubResume> ResumesByComplexQuery()
 {
     using (var context = new GitDbContext())
     {
         var result = context.GitHubResumes.SqlQuery("Select * FROM GitHubResume WHERE PiplMatchedDate IN (SELECT top 100 PiplMatchedDate From GitHubResume ORDER BY NEWID())").ToList();
         return(result);
     }
 }
Ejemplo n.º 12
0
 public List <GitHubUser> GetAllUsers()
 {
     using (var context = new GitDbContext())
     {
         var k = context.GitHubUsers.ToList();
         return(k);
     }
 }
Ejemplo n.º 13
0
 public List <GitHubResume> GetSuggestedResumes(int count = 50)
 {
     ExecuteComplexQuery();
     using (var context = new GitDbContext())
     {
         context.Configuration.LazyLoadingEnabled = false;
         return(context.GitHubResumes.OrderBy(x => Guid.NewGuid()).Select(x => x).AsNoTracking().Take(count).ToList());
     }
 }
Ejemplo n.º 14
0
 public UsersService(
     IValidator validator,
     PasswordHasher passwordHasher,
     GitDbContext dbcontext)
 {
     this.validator      = validator;
     this.passwordHasher = passwordHasher;
     this.dbcontext      = dbcontext;
 }
 public UsersController(
     IValidator validator,
     IPasswordHasher passwordHasher,
     GitDbContext data)
 {
     this.validator      = validator;
     this.passwordHasher = passwordHasher;
     this.data           = data;
 }
Ejemplo n.º 16
0
 public UsersController(GitDbContext data,
                        IValidator validator,
                        IUsersService usersService,
                        IPasswordHasher passwordHasher
                        )
 {
     this.data           = data;
     this.validator      = validator;
     this.usersService   = usersService;
     this.passwordHasher = passwordHasher;
 }
Ejemplo n.º 17
0
        public GitDbFixture()
        {
            var configuration = new ConfigurationBuilder()
                                .AddJsonFile("appsettings.json")
                                .Build();

            var options = configuration.GetSection(nameof(PineBlogGitDbOptions)).Get <PineBlogGitDbOptions>();

            _gitDbContext = GitDbContext.Create(options);
            _gitDbContext.CheckoutBranch(options.Branch);
        }
Ejemplo n.º 18
0
        private void Dispose(bool disposing)
        {
            if (disposing)
            {
            }

            // always try to dispose, even if disposing=false
            if (_gitDbContext != null)
            {
                try { _gitDbContext?.Dispose(); } catch { }
                _gitDbContext = null;
                GC.Collect();
            }
        }
Ejemplo n.º 19
0
 public int GetResumesCount(bool fast = false)
 {
     using (var context = new GitDbContext())
     {
         if (fast)
         {
             return(context.GitHubResumes.Select(x => x.Id).Count());
         }
         else
         {
             return(context.GitHubResumes.Select(x => x.Id).ToList().Count);
         }
     }
 }
Ejemplo n.º 20
0
        // TODO: add caching for get all posts
        protected async Task <IEnumerable <Post> > GetAllAsync(CancellationToken cancellationToken)
        {
            IDictionary <string, byte[]> files;
            var posts = new List <Post>();

            try
            {
                files = await GitDbContext.GetFilesAsync(PathHelper.Build(Options.Value.RootPath, GitDbConstants.PostsFolder), cancellationToken);
            }
            catch
            {
                return(posts);
            }

            var postFiles = files.Values.Select(b => Encoding.UTF8.GetString(b));

            foreach (var postFile in postFiles)
            {
                var json      = postFile.Substring(0, postFile.IndexOf(GitDbConstants.MetaDataSeparator));
                var gitDbPost = JsonSerializer.Deserialize <GitDbPost>(json, new JsonSerializerOptions {
                    AllowTrailingCommas = true
                });

                var post = new Post
                {
                    Title        = gitDbPost.Title,
                    Description  = gitDbPost.Description,
                    Categories   = gitDbPost.Categories,
                    Published    = gitDbPost.Published,
                    Slug         = gitDbPost.Slug,
                    CoverUrl     = gitDbPost.CoverUrl,
                    CoverCaption = gitDbPost.CoverCaption,
                    CoverLink    = gitDbPost.CoverLink,
                };

                var content = postFile.Substring(postFile.IndexOf(GitDbConstants.MetaDataSeparator) + GitDbConstants.MetaDataSeparator.Length);
                post.Content = content.Trim();

                // TODO: add caching for authors
                var author = await _authorRepository.SingleOrDefaultAsync(a => a.UserName == gitDbPost.AuthorId, cancellationToken);

                post.Author = author;

                posts.Add(post);
            }

            return(posts);
        }
Ejemplo n.º 21
0
 public List <GitHubResume> GetResumes(int count = 50000, bool notracking = false)
 {
     using (var context = new GitDbContext())
     {
         IQueryable <GitHubResume> data;
         if (notracking)
         {
             data = context.GitHubResumes.AsNoTracking();
         }
         else
         {
             data = context.GitHubResumes;
         }
         return(data.Take(count).ToList());
     }
 }
Ejemplo n.º 22
0
        public void SeedLData(int amount = 1000)
        {
            var data = new List <LData>();

            for (var i = 0; i < amount; i++)
            {
                var item = new LData();
                item.Randomize();
                data.Add(item);
            }
            using (var context = new GitDbContext())
            {
                //context.BulkInsert(data);
                context.SaveChanges();
            }
        }
Ejemplo n.º 23
0
        private void CheckBlogSettingsFileChanges(GitDbContext context)
        {
            var path = PathHelper.Build(_options.Value.LocalRepositoryBasePath, _options.Value.RootPath, GitDbConstants.BlogSettingsFile);

            if (!File.Exists(path))
            {
                return;
            }

            var lastModifiedDate = File.GetLastWriteTimeUtc(path);

            if (lastModifiedDate != null && lastModifiedDate > DateTime.UtcNow.AddSeconds(-10))
            {
                path = PathHelper.Build(_options.Value.RootPath, GitDbConstants.BlogSettingsFile);
                var files = context.GetFiles(new string[] { path });
                FileChangeObserver.Instance.OnChanged(new FileChangeEventArgs(GitDbConstants.BlogSettingsFile, files[path]));
            }
        }
Ejemplo n.º 24
0
        public async Task <BlogSettings> SingleOrDefaultAsync(CancellationToken cancellationToken)
        {
            IDictionary <string, byte[]> files;

            try
            {
                files = await GitDbContext.GetFilesAsync(new string[] { PathHelper.Build(Options.Value.RootPath, GitDbConstants.BlogSettingsFile) }, cancellationToken);
            }
            catch
            {
                return(null);
            }

            var json         = Encoding.UTF8.GetString(files.Values.Single());
            var blogSettings = JsonSerializer.Deserialize <BlogSettings>(json, new JsonSerializerOptions {
                AllowTrailingCommas = true
            });

            return(blogSettings);
        }
Ejemplo n.º 25
0
        public async Task <Author> SingleOrDefaultAsync(Expression <Func <Author, bool> > predicate, CancellationToken cancellationToken)
        {
            IDictionary <string, byte[]> files;

            try
            {
                files = await GitDbContext.GetFilesAsync(new string[] { PathHelper.Build(Options.Value.RootPath, GitDbConstants.AuthorsFile) }, cancellationToken);
            }
            catch
            {
                return(null);
            }

            var json    = Encoding.UTF8.GetString(files.Values.Single());
            var authors = JsonSerializer.Deserialize <IEnumerable <Author> >(json, new JsonSerializerOptions {
                AllowTrailingCommas = true
            });

            return(authors.SingleOrDefault(predicate.Compile()));
        }
Ejemplo n.º 26
0
 /// <summary>
 /// Remove example
 /// </summary>
 /// <param name="id"></param>
 /// <param name="tuned"></param>
 /// <returns></returns>
 public bool RemoveResume(int id, bool tuned = false)
 {
     using (var context = new GitDbContext())
     {
         if (!tuned)
         {
             var item = context.GitHubResumes.Find(id);
             context.GitHubResumes.Remove(item);
             context.SaveChanges();
         }
         else
         {
             var item = new GitHubResume()
             {
                 Id = id
             };
             context.Entry(item).State = System.Data.Entity.EntityState.Deleted;
             context.SaveChanges();
         }
     }
     return(true);
 }
Ejemplo n.º 27
0
        public void SeedRandomContact(int count = 100000)
        {
            var rand = new Random();

            using (var context = new GitDbContext())
            {
                var ids        = context.GitHubResumes.OrderBy(x => Guid.NewGuid()).Take(count).Select(x => x.Id).ToList();
                var toSaveList = new List <ContactData>();
                foreach (var id in ids)
                {
                    var amount = rand.Next(1, 5);
                    for (var i = 0; i < amount; i++)
                    {
                        var cd = new ContactData()
                        {
                            GitHubResumeId = id, ContactType = (ContactType)rand.Next(2)
                        };
                        if (cd.ContactType == ContactType.Address)
                        {
                            cd.Data = Faker.Address.SecondaryAddress();
                        }
                        else if (cd.ContactType == ContactType.Email)
                        {
                            cd.Data = Faker.User.Email();
                        }
                        else
                        {
                            cd.Data = Faker.Number.RandomNumber(950000000, 959999999).ToString();
                        }
                        cd.PersonalKey = Faker.User.Password(10, true);
                        toSaveList.Add(cd);
                    }
                }
                //context.ContactDatas.AddRange(toSaveList)
                context.BulkInsertAsync(toSaveList).Wait();
            }
        }
Ejemplo n.º 28
0
 public List <GitHubResume> GetResumesWithContacts(bool tuned = false, int count = 1000)
 {
     using (var context = new GitDbContext(true))
     {
         IQueryable <GitHubResume> resumes = null;
         if (!tuned)
         {
             resumes = context.GitHubResumes.Where(x => x.Contacts.Any()).Take(count);
         }
         else
         {
             resumes = context.GitHubResumes.Include(x => x.Contacts).Where(x => x.Contacts.Any()).Take(count);
         }
         foreach (var r in resumes)
         {
             foreach (var c in r.Contacts)
             {
                 // hide personal key
                 c.PersonalKey = string.Empty;
             }
         }
         return(resumes.ToList());
     }
 }
Ejemplo n.º 29
0
 public BlogSettingsRepository(GitDbContext gitDbContext, IOptions <PineBlogGitDbOptions> options) : base(gitDbContext, options)
 {
 }
 public RepositoryService(GitDbContext data) => this.data = data;