From 66ab052e742ce56768b7db59f46f9c8641bec5da Mon Sep 17 00:00:00 2001 From: Antonin Rouxel Date: Wed, 26 Jun 2024 12:38:45 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Ajout=20de=20la=20programmation=20r?= =?UTF-8?q?=C3=A9active=20pour=20la=20page=20d'accueil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Configurations/GithubSettings.cs | 4 +- Layout/MainLayout.razor | 2 + Models/Github.cs | 9 -- Models/License.cs | 15 -- Models/Profile.cs | 69 --------- Models/Repository.cs | 164 ---------------------- Pages/Home/Index.razor | 9 -- Pages/Home/Sections/Home.razor.cs | 29 ---- Pages/Home/Sections/Projects.razor | 55 -------- Pages/Home/Sections/Projects.razor.cs | 66 --------- Program.cs | 4 +- Services/GithubService.cs | 33 ++--- Services/IGithubService.cs | 9 +- ViewModels/Home/HomeViewModel.cs | 34 +++++ Views/Home/IndexViews.razor | 6 + {Pages => Views}/Home/Sections/Home.razor | 18 +-- Views/Home/Sections/Home.razor.cs | 19 +++ _Imports.razor | 8 +- anrouxel.csproj | 2 + wwwroot/appsettings.json | 3 +- 20 files changed, 93 insertions(+), 465 deletions(-) delete mode 100644 Models/Github.cs delete mode 100644 Models/License.cs delete mode 100644 Models/Profile.cs delete mode 100644 Models/Repository.cs delete mode 100644 Pages/Home/Index.razor delete mode 100644 Pages/Home/Sections/Home.razor.cs delete mode 100644 Pages/Home/Sections/Projects.razor delete mode 100644 Pages/Home/Sections/Projects.razor.cs create mode 100644 ViewModels/Home/HomeViewModel.cs create mode 100644 Views/Home/IndexViews.razor rename {Pages => Views}/Home/Sections/Home.razor (90%) create mode 100644 Views/Home/Sections/Home.razor.cs diff --git a/Configurations/GithubSettings.cs b/Configurations/GithubSettings.cs index c4d3cb2..4b9450c 100644 --- a/Configurations/GithubSettings.cs +++ b/Configurations/GithubSettings.cs @@ -2,8 +2,6 @@ namespace anrouxel.Configurations { public class GithubSettings { - public string GITHUB_API_URL { get; set; } = null!; - - public string GITHUB_PROFILE_URL { get; set; } = null!; + public string GITHUB_PROFILE { get; set; } = null!; } } \ No newline at end of file diff --git a/Layout/MainLayout.razor b/Layout/MainLayout.razor index e0f0c55..61c2606 100644 --- a/Layout/MainLayout.razor +++ b/Layout/MainLayout.razor @@ -1,6 +1,8 @@ @inherits LayoutComponentBase
+ +
@Body diff --git a/Models/Github.cs b/Models/Github.cs deleted file mode 100644 index 5a00fe5..0000000 --- a/Models/Github.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace anrouxel.Models -{ - public class Github - { - public Profile? profile { get; set; } - - public Repository[]? repositories { get; set; } - } -} diff --git a/Models/License.cs b/Models/License.cs deleted file mode 100644 index c98fe69..0000000 --- a/Models/License.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace anrouxel.Models -{ - public class License - { - public string? key { get; set; } - - public string? name { get; set; } - - public string? spdx_id { get; set; } - - public Uri? url { get; set; } - - public string? node_id { get; set; } - } -} \ No newline at end of file diff --git a/Models/Profile.cs b/Models/Profile.cs deleted file mode 100644 index 424cb00..0000000 --- a/Models/Profile.cs +++ /dev/null @@ -1,69 +0,0 @@ -namespace anrouxel.Models -{ - public class Profile - { - public string? login { get; set; } - - public int id { get; set; } - - public string? node_id { get; set; } - - public string? avatar_url { get; set; } - - public string? gravatar_id { get; set; } - - public Uri? url { get; set; } - - public Uri? html_url { get; set; } - - public Uri? followers_url { get; set; } - - public Uri? following_url { get; set; } - - public Uri? gists_url { get; set; } - - public Uri? starred_url { get; set; } - - public Uri? subscriptions_url { get; set; } - - public Uri? organizations_url { get; set; } - - public Uri? repos_url { get; set; } - - public Uri? events_url { get; set; } - - public Uri? received_events_url { get; set; } - - public string? type { get; set; } - - public bool site_admin { get; set; } - - public string? name { get; set; } - - public string? company { get; set; } - - public string? blog { get; set; } - - public string? location { get; set; } - - public string? email { get; set; } - - public string? hireable { get; set; } - - public string? bio { get; set; } - - public string? twitter_username { get; set; } - - public int public_repos { get; set; } - - public int public_gists { get; set; } - - public int followers { get; set; } - - public int following { get; set; } - - public string? created_at { get; set; } - - public string? updated_at { get; set; } - } -} \ No newline at end of file diff --git a/Models/Repository.cs b/Models/Repository.cs deleted file mode 100644 index eebfad1..0000000 --- a/Models/Repository.cs +++ /dev/null @@ -1,164 +0,0 @@ -using System.Text.Json.Serialization; - -namespace anrouxel.Models -{ - public class Repository - { - public int id { get; set; } - - public string? node_id { get; set; } - - public string? name { get; set; } - - public string? full_name { get; set; } - - [JsonPropertyName("private")] - public bool private_repo { get; set; } - - public Profile? owner { get; set; } - - public Uri? html_url { get; set; } - - public string? description { get; set; } - - public bool fork { get; set; } - - public Uri? url { get; set; } - - public Uri? forks_url { get; set; } - - public Uri? keys_url { get; set; } - - public Uri? collaborators_url { get; set; } - - public Uri? teams_url { get; set; } - - public Uri? hooks_url { get; set; } - - public Uri? issue_events_url { get; set; } - - public Uri? events_url { get; set; } - - public Uri? assignees_url { get; set; } - - public Uri? branches_url { get; set; } - - public Uri? tags_url { get; set; } - - public Uri? blobs_url { get; set; } - - public Uri? git_tags_url { get; set; } - - public Uri? git_refs_url { get; set; } - - public Uri? trees_url { get; set; } - - public Uri? statuses_url { get; set; } - - public Uri? languages_url { get; set; } - - public Uri? stargazers_url { get; set; } - - public Uri? contributors_url { get; set; } - - public Uri? subscribers_url { get; set; } - - public Uri? subscription_url { get; set; } - - public Uri? commits_url { get; set; } - - public Uri? git_commits_url { get; set; } - - public Uri? comments_url { get; set; } - - public Uri? issue_comment_url { get; set; } - - public Uri? contents_url { get; set; } - - public Uri? compare_url { get; set; } - - public Uri? merges_url { get; set; } - - public Uri? archive_url { get; set; } - - public Uri? downloads_url { get; set; } - - public Uri? issues_url { get; set; } - - public Uri? pulls_url { get; set; } - - public Uri? milestones_url { get; set; } - - public Uri? notifications_url { get; set; } - - public Uri? labels_url { get; set; } - - public Uri? releases_url { get; set; } - - public Uri? deployments_url { get; set; } - - public DateTimeOffset created_at { get; set; } - - public DateTimeOffset updated_at { get; set; } - - public DateTimeOffset pushed_at { get; set; } - - public Uri? git_url { get; set; } - - public Uri? ssh_url { get; set; } - - public Uri? clone_url { get; set; } - - public Uri? svn_url { get; set; } - - public string? homepage { get; set; } - - public int size { get; set; } - - public int stargazers_count { get; set; } - - public int watchers_count { get; set; } - - public string? language { get; set; } - - public bool has_issues { get; set; } - - public bool has_projects { get; set; } - - public bool has_downloads { get; set; } - - public bool has_wiki { get; set; } - - public bool has_pages { get; set; } - - public int forks_count { get; set; } - - public Uri? mirror_url { get; set; } - - public bool archived { get; set; } - - public bool disabled { get; set; } - - public int open_issues_count { get; set; } - - public License? license { get; set; } - - public bool allow_forking { get; set; } - - public bool is_template { get; set; } - - public bool web_commit_signoff_required { get; set; } - - public Object[]? topics { get; set; } - - public string? visibility { get; set; } - - public int forks { get; set; } - - public int open_issues { get; set; } - - public int watchers { get; set; } - - public string? default_branch { get; set; } - } -} \ No newline at end of file diff --git a/Pages/Home/Index.razor b/Pages/Home/Index.razor deleted file mode 100644 index c87e940..0000000 --- a/Pages/Home/Index.razor +++ /dev/null @@ -1,9 +0,0 @@ -@page "/" -@using anrouxel.Pages.Home.Sections - -Accueil - - - - - \ No newline at end of file diff --git a/Pages/Home/Sections/Home.razor.cs b/Pages/Home/Sections/Home.razor.cs deleted file mode 100644 index 0ce2577..0000000 --- a/Pages/Home/Sections/Home.razor.cs +++ /dev/null @@ -1,29 +0,0 @@ - -using System.Net.Http.Json; -using anrouxel.Models; -using anrouxel.Services; -using Microsoft.AspNetCore.Components; - -namespace anrouxel.Pages.Home.Sections -{ - public partial class Home - { - [Inject] - private NavigationManager navigationManager { get; set; } = null!; - - [Inject] - private IGithubService _githubService { get; set; } = null!; - - private Profile? profile { get; set; } - - protected override async Task OnInitializedAsync() - { - profile = await _githubService.GetProfileAsync(); - } - - private void navigateTo(string uri) - { - navigationManager.NavigateTo(uri, true); - } - } -} \ No newline at end of file diff --git a/Pages/Home/Sections/Projects.razor b/Pages/Home/Sections/Projects.razor deleted file mode 100644 index 396f9bb..0000000 --- a/Pages/Home/Sections/Projects.razor +++ /dev/null @@ -1,55 +0,0 @@ -
-
-
-
-
- -
-
- -
- - -
-
- -
- @foreach (var repository in repositories) - { -
-
-
-

@repository.name

-

@repository.description

-
-
- -
- - @if (repositories.IndexOf(repository) != repositories.Count - 1) - { -
- } - } -
-
-
-
-
-
-
-
-
diff --git a/Pages/Home/Sections/Projects.razor.cs b/Pages/Home/Sections/Projects.razor.cs deleted file mode 100644 index 6b3a6a9..0000000 --- a/Pages/Home/Sections/Projects.razor.cs +++ /dev/null @@ -1,66 +0,0 @@ - -using System.Net.Http.Json; -using anrouxel.Models; -using anrouxel.Services; -using Microsoft.AspNetCore.Components; - -namespace anrouxel.Pages.Home.Sections -{ - public partial class Projects - { - [Inject] - private NavigationManager navigationManager { get; set; } = null!; - - [Inject] - private IGithubService _githubService { get; set; } = null!; - - private List repositories = new List(); - - protected override async Task OnAfterRenderAsync(bool firstRender) - { - if (firstRender) - { - repositories = await _githubService.GetRepositoriesAsync(); - StateHasChanged(); - } - } - - private string CalculateTimeDifference(DateTimeOffset time) - { - DateTimeOffset now = DateTimeOffset.Now; - TimeSpan difference = now - time; - - if (difference.TotalDays >= 365) - { - return $"{(int)(difference.TotalDays / 365)} année{(difference.TotalDays >= 730 ? "s" : "")}"; - } - else if (difference.TotalDays >= 30) - { - return $"{(int)(difference.TotalDays / 30)} mois"; - } - else if (difference.TotalDays >= 1) - { - return $"{(int)difference.TotalDays} jour{(difference.TotalDays >= 2 ? "s" : "")}"; - } - else if (difference.TotalHours >= 1) - { - return $"{(int)difference.TotalHours} heure{(difference.TotalHours >= 2 ? "s" : "")}"; - } - else if (difference.TotalMinutes >= 1) - { - return $"{(int)difference.TotalMinutes} minute{(difference.TotalMinutes >= 2 ? "s" : "")}"; - } - else if (difference.TotalSeconds >= 1) - { - return $"{(int)difference.TotalSeconds} seconde{(difference.TotalSeconds >= 2 ? "s" : "")}"; - } else { - return "à l'instant"; - } - } - - private void navigateTo(string uri) - { - navigationManager.NavigateTo(uri, true); - } - } -} \ No newline at end of file diff --git a/Program.cs b/Program.cs index e927cd1..e6f77ea 100644 --- a/Program.cs +++ b/Program.cs @@ -3,6 +3,7 @@ using anrouxel; using anrouxel.Configurations; using anrouxel.Services; +using anrouxel.ViewModels; var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add("#app"); @@ -12,6 +13,7 @@ builder.Services.Configure(builder.Configuration.GetSection("GithubSettings")); -builder.Services.AddScoped(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); await builder.Build().RunAsync(); diff --git a/Services/GithubService.cs b/Services/GithubService.cs index c1b8bfd..646d752 100644 --- a/Services/GithubService.cs +++ b/Services/GithubService.cs @@ -1,46 +1,31 @@ -using System.Net.Http.Json; using anrouxel.Configurations; -using anrouxel.Models; using Microsoft.Extensions.Options; +using Octokit; +using Octokit.Reactive; namespace anrouxel.Services { - public class GithubService : IGithubService + public class GithubService: IGithubService { private readonly GithubSettings _githubSettings; - private readonly HttpClient _httpClient; + private readonly ObservableGitHubClient _client; public GithubService( IOptions githubSettings ) { _githubSettings = githubSettings.Value; - _httpClient = new HttpClient(); - _httpClient.BaseAddress = new Uri(githubSettings.Value.GITHUB_API_URL); + _client = new ObservableGitHubClient(new ProductHeaderValue(_githubSettings.GITHUB_PROFILE)); } - private string MakeUrl(string? path) + public IObservable GetProfileAsync() { - if (path == null) - { - return _githubSettings.GITHUB_PROFILE_URL; - } - return $"{_githubSettings.GITHUB_PROFILE_URL}/{path}"; + return _client.User.Get(_githubSettings.GITHUB_PROFILE); } - public async Task GetProfileAsync() + public IObservable GetRepositoriesAsync() { - return await _httpClient.GetFromJsonAsync(MakeUrl(null)); - } - - public async Task> GetRepositoriesAsync() - { - var repositories = await _httpClient.GetFromJsonAsync>(MakeUrl("repos")); - if (repositories == null) - { - return new List(); - } - return repositories; + return _client.Repository.GetAllForUser(_githubSettings.GITHUB_PROFILE); } } } \ No newline at end of file diff --git a/Services/IGithubService.cs b/Services/IGithubService.cs index b22c83f..8b1dbb7 100644 --- a/Services/IGithubService.cs +++ b/Services/IGithubService.cs @@ -1,14 +1,11 @@ -using System.Net.Http.Json; -using anrouxel.Configurations; -using anrouxel.Models; -using Microsoft.Extensions.Options; +using Octokit; namespace anrouxel.Services { public interface IGithubService { - Task GetProfileAsync(); + IObservable GetProfileAsync(); - Task> GetRepositoriesAsync(); + IObservable GetRepositoriesAsync(); } } \ No newline at end of file diff --git a/ViewModels/Home/HomeViewModel.cs b/ViewModels/Home/HomeViewModel.cs new file mode 100644 index 0000000..cb80b25 --- /dev/null +++ b/ViewModels/Home/HomeViewModel.cs @@ -0,0 +1,34 @@ +using anrouxel.Services; +using Microsoft.AspNetCore.Components; +using System.Reactive; +using System.Threading.Tasks; +using ReactiveUI; +using Octokit; +using System.Reactive.Linq; + +// View Model qui va gérer la page d'accueil (récupération des données de l'utilisateur) +namespace anrouxel.ViewModels +{ + public class HomeViewModel: ReactiveObject + { + private readonly IGithubService _githubService; + + private readonly ObservableAsPropertyHelper _profile; + + public HomeViewModel(IGithubService githubService) { + _githubService = githubService; + + GetProfile = ReactiveCommand.CreateFromTask(GetProfileAsync); + + _profile = GetProfile.ToProperty(this, x => x.Profile); + } + + public ReactiveCommand GetProfile { get; } + + public User Profile => _profile.Value; + + private async Task GetProfileAsync() { + return await _githubService.GetProfileAsync(); + } + } +} \ No newline at end of file diff --git a/Views/Home/IndexViews.razor b/Views/Home/IndexViews.razor new file mode 100644 index 0000000..0583145 --- /dev/null +++ b/Views/Home/IndexViews.razor @@ -0,0 +1,6 @@ +@page "/" +@using anrouxel.Views.Home.Sections + +Accueil + + \ No newline at end of file diff --git a/Pages/Home/Sections/Home.razor b/Views/Home/Sections/Home.razor similarity index 90% rename from Pages/Home/Sections/Home.razor rename to Views/Home/Sections/Home.razor index b5c1050..b9344f2 100644 --- a/Pages/Home/Sections/Home.razor +++ b/Views/Home/Sections/Home.razor @@ -1,12 +1,11 @@ -
+@inherits ReactiveInjectableComponentBase + +
@@ -14,12 +13,9 @@
Bienvenue, je suis
- @if (profile != null) - {
- @profile.name + @ViewModel!.Profile?.Name
- }
@@ -55,4 +51,4 @@
-
\ No newline at end of file +
diff --git a/Views/Home/Sections/Home.razor.cs b/Views/Home/Sections/Home.razor.cs new file mode 100644 index 0000000..4fe1be7 --- /dev/null +++ b/Views/Home/Sections/Home.razor.cs @@ -0,0 +1,19 @@ +using anrouxel.ViewModels; +using ReactiveUI.Blazor; +using ReactiveUI; + +namespace anrouxel.Views.Home.Sections +{ + public partial class Home: ReactiveInjectableComponentBase + { + public Home() + { + this.WhenActivated(disposables => {}); + } + + protected override void OnInitialized() + { + ViewModel!.GetProfile.Execute().Subscribe(); + } + } +} \ No newline at end of file diff --git a/_Imports.razor b/_Imports.razor index e21ff13..42d9fad 100644 --- a/_Imports.razor +++ b/_Imports.razor @@ -6,8 +6,12 @@ @using Microsoft.AspNetCore.Components.Web.Virtualization @using Microsoft.AspNetCore.Components.WebAssembly.Http @using Microsoft.JSInterop +@using ReactiveUI +@using System.Reactive +@using System.Reactive.Linq +@using ReactiveUI.Blazor @using anrouxel @using anrouxel.Layout -@using anrouxel.Models @using anrouxel.Configurations -@using anrouxel.Services \ No newline at end of file +@using anrouxel.Services +@using anrouxel.ViewModels \ No newline at end of file diff --git a/anrouxel.csproj b/anrouxel.csproj index 6883cd1..cd9af05 100644 --- a/anrouxel.csproj +++ b/anrouxel.csproj @@ -12,6 +12,8 @@ + + diff --git a/wwwroot/appsettings.json b/wwwroot/appsettings.json index 443e148..88ac529 100644 --- a/wwwroot/appsettings.json +++ b/wwwroot/appsettings.json @@ -1,6 +1,5 @@ { "GithubSettings": { - "GITHUB_API_URL": "https://api.github.com/users/", - "GITHUB_PROFILE_URL": "anrouxel" + "GITHUB_PROFILE": "anrouxel" } } \ No newline at end of file