From ab85eaffb7dec4c6f86058797bd411da6e0b5bc4 Mon Sep 17 00:00:00 2001 From: ALEXXXANDRO Date: Wed, 21 Apr 2021 13:17:07 +0300 Subject: [PATCH 01/11] feat: Add GetListActivity to UserCashRepository --- .../Repositories/User\320\241acheRepository.cs" | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git "a/Kysect.GithubActivityAnalyzer/Data/Repositories/User\320\241acheRepository.cs" "b/Kysect.GithubActivityAnalyzer/Data/Repositories/User\320\241acheRepository.cs" index 1dfbc1b..8bff14e 100644 --- "a/Kysect.GithubActivityAnalyzer/Data/Repositories/User\320\241acheRepository.cs" +++ "b/Kysect.GithubActivityAnalyzer/Data/Repositories/User\320\241acheRepository.cs" @@ -1,4 +1,5 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using System.Text.Json; using Kysect.GithubActivityAnalyzer.ApiAccessor.ApiResponses; using Kysect.GithubActivityAnalyzer.Data.Entities; @@ -63,6 +64,20 @@ public ActivityInfo GetActivityFromUserCash(UserСache userCash) var activity = JsonSerializer.Deserialize(userCash.ActivityInfo, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); return activity; } + public List<(string Username, ActivityInfo Activity)> GetActivityFromUserCash(IEnumerable usernames, bool isParallel) + { + if (!isParallel) + { + return usernames + .Select(username => (username, GetActivityFromUserCash(FindByUsername(username)))) + .ToList(); + } + List<(string, ActivityInfo)> result = usernames + .AsParallel() + .Select(username => (username, GetActivityFromUserCash(FindByUsername(username)))) + .ToList(); + return result; + } } } From 8a764805359b34410981d665daed5ebee32c566e Mon Sep 17 00:00:00 2001 From: ALEXXXANDRO Date: Mon, 26 Apr 2021 21:12:54 +0300 Subject: [PATCH 02/11] refact: fix name, fix logic --- .../Data/Repositories/UserCacheRepository.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) rename "Kysect.GithubActivityAnalyzer/Data/Repositories/User\320\241acheRepository.cs" => Kysect.GithubActivityAnalyzer/Data/Repositories/UserCacheRepository.cs (90%) diff --git "a/Kysect.GithubActivityAnalyzer/Data/Repositories/User\320\241acheRepository.cs" b/Kysect.GithubActivityAnalyzer/Data/Repositories/UserCacheRepository.cs similarity index 90% rename from "Kysect.GithubActivityAnalyzer/Data/Repositories/User\320\241acheRepository.cs" rename to Kysect.GithubActivityAnalyzer/Data/Repositories/UserCacheRepository.cs index 8bff14e..8faf5eb 100644 --- "a/Kysect.GithubActivityAnalyzer/Data/Repositories/User\320\241acheRepository.cs" +++ b/Kysect.GithubActivityAnalyzer/Data/Repositories/UserCacheRepository.cs @@ -2,20 +2,21 @@ using System.Linq; using System.Text.Json; using Kysect.GithubActivityAnalyzer.ApiAccessor.ApiResponses; +using Kysect.GithubActivityAnalyzer.Data.Contexts; using Kysect.GithubActivityAnalyzer.Data.Entities; using Microsoft.EntityFrameworkCore; namespace Kysect.GithubActivityAnalyzer.Data.Repositories { - public class UserСacheRepository : IRepository + public class UserCacheRepository : IRepository { - readonly DbContext _context; + readonly ActivityContext _context; readonly DbSet _dbSet; - public UserСacheRepository(DbContext context, DbSet set) + public UserCacheRepository(ActivityContext context) { _context = context; - _dbSet = set; + _dbSet = context.UserСache; } public IQueryable GetAll() From c8326affdb25e209013c8e6fddf6463f70dd59f7 Mon Sep 17 00:00:00 2001 From: ALEXXXANDRO Date: Mon, 26 Apr 2021 21:14:40 +0300 Subject: [PATCH 03/11] feat: add ActivityService --- .../Server/Services/ActivityService.cs | 28 +++++++++++++++++++ .../Server/Services/IActivityService.cs | 14 ++++++++++ 2 files changed, 42 insertions(+) create mode 100644 Kysect.GithubActivityAnalyzer.WebDemo/Server/Services/ActivityService.cs create mode 100644 Kysect.GithubActivityAnalyzer.WebDemo/Server/Services/IActivityService.cs diff --git a/Kysect.GithubActivityAnalyzer.WebDemo/Server/Services/ActivityService.cs b/Kysect.GithubActivityAnalyzer.WebDemo/Server/Services/ActivityService.cs new file mode 100644 index 0000000..c597b40 --- /dev/null +++ b/Kysect.GithubActivityAnalyzer.WebDemo/Server/Services/ActivityService.cs @@ -0,0 +1,28 @@ +using Kysect.GithubActivityAnalyzer.ApiAccessor; +using Kysect.GithubActivityAnalyzer.ApiAccessor.ApiResponses; +using Kysect.GithubActivityAnalyzer.Data.Repositories; +using System.Threading.Tasks; + +namespace Kysect.GithubActivityAnalyzer.WebDemo.Server.Services +{ + public class ActivityService : IActivityService + { + private readonly GithubActivityProvider _githubActivityProvider; + private readonly UserCacheRepository _userСacheRepository; + public ActivityService(UserCacheRepository userСacheRepository, GithubActivityProvider githubActivityProvider) + { + this._userСacheRepository = userСacheRepository; + this._githubActivityProvider = githubActivityProvider; + } + + public async Task GetActivityInfoFromGithub(string username) + { + return await _githubActivityProvider.GetActivityInfo(username); + } + + public ActivityInfo GetActivityInfoFromDB(string username) + { + return _userСacheRepository.GetActivityFromUserCash(_userСacheRepository.FindByUsername(username)); + } + } +} diff --git a/Kysect.GithubActivityAnalyzer.WebDemo/Server/Services/IActivityService.cs b/Kysect.GithubActivityAnalyzer.WebDemo/Server/Services/IActivityService.cs new file mode 100644 index 0000000..add3b58 --- /dev/null +++ b/Kysect.GithubActivityAnalyzer.WebDemo/Server/Services/IActivityService.cs @@ -0,0 +1,14 @@ +using Kysect.GithubActivityAnalyzer.ApiAccessor.ApiResponses; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Kysect.GithubActivityAnalyzer.WebDemo.Server.Services +{ + public interface IActivityService + { + public Task GetActivityInfoFromGithub(string username); + public ActivityInfo GetActivityInfoFromDB(string username); + } +} From 15a2926a95a675a19ad4046749657acf5793b759 Mon Sep 17 00:00:00 2001 From: ALEXXXANDRO Date: Mon, 26 Apr 2021 21:18:31 +0300 Subject: [PATCH 04/11] feat: add DB UI to testPage --- .../Client/Pages/TestPage.razor | 10 ++++++++-- .../Server/Controllers/GitHubApiController.cs | 19 +++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Kysect.GithubActivityAnalyzer.WebDemo/Client/Pages/TestPage.razor b/Kysect.GithubActivityAnalyzer.WebDemo/Client/Pages/TestPage.razor index b6bbb16..6620585 100644 --- a/Kysect.GithubActivityAnalyzer.WebDemo/Client/Pages/TestPage.razor +++ b/Kysect.GithubActivityAnalyzer.WebDemo/Client/Pages/TestPage.razor @@ -6,7 +6,8 @@

- + + @if(SetName == null) {

Please write name

@@ -32,6 +33,11 @@ else protected async Task CreateClient() { - Info = await Http.GetFromJsonAsync($"GitHubApi?username={SetName}"); + Info = await Http.GetFromJsonAsync($"GitHubApi/fromGithub?username={SetName}"); + } + + protected async Task CreateClient1() + { + Info = await Http.GetFromJsonAsync($"GitHubApi/fromDB?username={SetName}"); } } diff --git a/Kysect.GithubActivityAnalyzer.WebDemo/Server/Controllers/GitHubApiController.cs b/Kysect.GithubActivityAnalyzer.WebDemo/Server/Controllers/GitHubApiController.cs index 466a021..f488fac 100644 --- a/Kysect.GithubActivityAnalyzer.WebDemo/Server/Controllers/GitHubApiController.cs +++ b/Kysect.GithubActivityAnalyzer.WebDemo/Server/Controllers/GitHubApiController.cs @@ -1,6 +1,7 @@ using System.Text.Json; using Kysect.GithubActivityAnalyzer.ApiAccessor; using Kysect.GithubActivityAnalyzer.ApiAccessor.ApiResponses; +using Kysect.GithubActivityAnalyzer.WebDemo.Server.Services; using Microsoft.AspNetCore.Mvc; namespace Kysect.GithubActivityAnalyzer.WebDemo.Server.Controllers @@ -9,12 +10,22 @@ namespace Kysect.GithubActivityAnalyzer.WebDemo.Server.Controllers [Route("[controller]")] public class GitHubApiController : Controller { - private GithubActivityProvider provider = new GithubActivityProvider(); + private readonly IActivityService _activityService; + public GitHubApiController(IActivityService activityService) + { + _activityService = activityService; + } + + [HttpGet("fromGithub")] + public ActivityInfo OnGetFromGithub(string username) + { + return _activityService.GetActivityInfoFromGithub(username).Result; + } - [HttpGet] - public ActivityInfo Get(string username) + [HttpGet("FromDB")] + public ActivityInfo OnGetFromDB(string username) { - return provider.GetActivityInfo(username).Result; + return _activityService.GetActivityInfoFromDB(username); } } } From 26faf02622157151f6252d13097c536c8c73fc2b Mon Sep 17 00:00:00 2001 From: ALEXXXANDRO Date: Mon, 26 Apr 2021 21:19:19 +0300 Subject: [PATCH 05/11] feat: add db logic, DI --- ...ct.GithubActivityAnalyzer.WebDemo.Server.csproj | 1 + .../Server/Program.cs | 14 ++++++++++++-- .../Server/Startup.cs | 14 ++++++++++++++ .../Data/Contexts/ActivityContext.cs | 10 +--------- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Kysect.GithubActivityAnalyzer.WebDemo/Server/Kysect.GithubActivityAnalyzer.WebDemo.Server.csproj b/Kysect.GithubActivityAnalyzer.WebDemo/Server/Kysect.GithubActivityAnalyzer.WebDemo.Server.csproj index 1ac373f..65b48ac 100644 --- a/Kysect.GithubActivityAnalyzer.WebDemo/Server/Kysect.GithubActivityAnalyzer.WebDemo.Server.csproj +++ b/Kysect.GithubActivityAnalyzer.WebDemo/Server/Kysect.GithubActivityAnalyzer.WebDemo.Server.csproj @@ -7,6 +7,7 @@ + diff --git a/Kysect.GithubActivityAnalyzer.WebDemo/Server/Program.cs b/Kysect.GithubActivityAnalyzer.WebDemo/Server/Program.cs index 47f98a9..f84bcbf 100644 --- a/Kysect.GithubActivityAnalyzer.WebDemo/Server/Program.cs +++ b/Kysect.GithubActivityAnalyzer.WebDemo/Server/Program.cs @@ -1,5 +1,7 @@ -using Microsoft.AspNetCore.Hosting; +using Kysect.GithubActivityAnalyzer.Data.Contexts; +using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -15,7 +17,15 @@ public class Program { public static void Main(string[] args) { - CreateHostBuilder(args).Build().Run(); + var host = CreateHostBuilder(args).Build(); + + using (var scope = host.Services.CreateScope()) + using (var context = scope.ServiceProvider.GetService()) + { + context.Database.EnsureCreated(); + } + + host.Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => diff --git a/Kysect.GithubActivityAnalyzer.WebDemo/Server/Startup.cs b/Kysect.GithubActivityAnalyzer.WebDemo/Server/Startup.cs index bdf59c1..ebbdc5b 100644 --- a/Kysect.GithubActivityAnalyzer.WebDemo/Server/Startup.cs +++ b/Kysect.GithubActivityAnalyzer.WebDemo/Server/Startup.cs @@ -6,6 +6,11 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Kysect.GithubActivityAnalyzer.Data.Contexts; +using Microsoft.EntityFrameworkCore; +using Kysect.GithubActivityAnalyzer.WebDemo.Server.Services; +using Kysect.GithubActivityAnalyzer.Data.Repositories; +using Kysect.GithubActivityAnalyzer.ApiAccessor; namespace Kysect.GithubActivityAnalyzer.WebDemo.Server { @@ -36,6 +41,15 @@ public void ConfigureServices(IServiceCollection services) .AllowAnyHeader(); }); }); + services.AddDbContext(options => + { + options.UseSqlite($"Data Source= ActivityDB.db"); + }); + services.AddScoped(); + + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); services.AddControllers(); } diff --git a/Kysect.GithubActivityAnalyzer/Data/Contexts/ActivityContext.cs b/Kysect.GithubActivityAnalyzer/Data/Contexts/ActivityContext.cs index 6faebe7..0c276bb 100644 --- a/Kysect.GithubActivityAnalyzer/Data/Contexts/ActivityContext.cs +++ b/Kysect.GithubActivityAnalyzer/Data/Contexts/ActivityContext.cs @@ -5,21 +5,13 @@ namespace Kysect.GithubActivityAnalyzer.Data.Contexts { public class ActivityContext : DbContext { + public ActivityContext(DbContextOptions options) : base(options) { } public DbSet UserСache { get; set; } - - protected override void OnConfiguring(DbContextOptionsBuilder options) - => options.UseSqlite($"Data Source=..\\ActivityDB.db"); - protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() .HasKey(b => b.Username) .HasName("PrimaryKey_Username"); } - - public ActivityContext() - { - Database.EnsureCreated(); - } } } From 1eb14a8e2ee28d85cecadb50a2544ba332a3f18a Mon Sep 17 00:00:00 2001 From: ALEXXXANDRO Date: Tue, 27 Apr 2021 14:57:48 +0300 Subject: [PATCH 06/11] refactor: remove WetherForeCast --- .../Client/Pages/FetchData.razor | 46 ------------------- .../Client/Shared/NavMenu.razor | 5 -- .../Controllers/WeatherForecastController.cs | 41 ----------------- .../Shared/WeatherForecast.cs | 15 ------ 4 files changed, 107 deletions(-) delete mode 100644 Kysect.GithubActivityAnalyzer.WebDemo/Client/Pages/FetchData.razor delete mode 100644 Kysect.GithubActivityAnalyzer.WebDemo/Server/Controllers/WeatherForecastController.cs delete mode 100644 Kysect.GithubActivityAnalyzer.WebDemo/Shared/WeatherForecast.cs diff --git a/Kysect.GithubActivityAnalyzer.WebDemo/Client/Pages/FetchData.razor b/Kysect.GithubActivityAnalyzer.WebDemo/Client/Pages/FetchData.razor deleted file mode 100644 index 43e1a0a..0000000 --- a/Kysect.GithubActivityAnalyzer.WebDemo/Client/Pages/FetchData.razor +++ /dev/null @@ -1,46 +0,0 @@ -@page "/fetchdata" -@using Kysect.GithubActivityAnalyzer.WebDemo.Shared -@inject HttpClient Http - -

Weather forecast

- -

This component demonstrates fetching data from the server.

- -@if (forecasts == null) -{ -

Loading...

-} -else -{ - - - - - - - - - - - @foreach (var forecast in forecasts) - { - - - - - - - } - -
DateTemp. (C)Temp. (F)Summary
@forecast.Date.ToShortDateString()@forecast.TemperatureC@forecast.TemperatureF@forecast.Summary
-} - -@code { - private WeatherForecast[] forecasts; - - protected override async Task OnInitializedAsync() - { - forecasts = await Http.GetFromJsonAsync("WeatherForecast"); - } - -} diff --git a/Kysect.GithubActivityAnalyzer.WebDemo/Client/Shared/NavMenu.razor b/Kysect.GithubActivityAnalyzer.WebDemo/Client/Shared/NavMenu.razor index e2366cb..c2730e1 100644 --- a/Kysect.GithubActivityAnalyzer.WebDemo/Client/Shared/NavMenu.razor +++ b/Kysect.GithubActivityAnalyzer.WebDemo/Client/Shared/NavMenu.razor @@ -17,11 +17,6 @@ Counter -