From 33bc56915019cbc7c46ef4d5e916b63a4cf50b7c Mon Sep 17 00:00:00 2001 From: Gary Murphy Date: Sun, 30 Apr 2023 20:12:24 +0000 Subject: [PATCH] Frontend account and http service updates --- src/PanoptesFrontend/Data/Account/AddUser.cs | 13 ++++++ src/PanoptesFrontend/Data/Account/Login.cs | 13 ++++++ .../Data/Account/LoginUser.cs | 12 ++++++ src/PanoptesFrontend/Data/Post.cs | 15 +++++++ src/PanoptesFrontend/Data/User.cs | 9 +++++ src/PanoptesFrontend/Pages/Login.razor | 38 ++++++++++++++++++ src/PanoptesFrontend/Pages/Register.razor | 38 ++++++++++++++++++ src/PanoptesFrontend/Pages/TestRequest.razor | 4 ++ src/PanoptesFrontend/Program.cs | 3 +- .../Services/AccountService.cs | 40 +++++++++++++++++++ .../{Data => Services}/HttpService.cs | 13 +++++- src/PanoptesFrontend/Shared/NavMenu.razor | 11 ++--- 12 files changed, 202 insertions(+), 7 deletions(-) create mode 100644 src/PanoptesFrontend/Data/Account/AddUser.cs create mode 100644 src/PanoptesFrontend/Data/Account/Login.cs create mode 100644 src/PanoptesFrontend/Data/Account/LoginUser.cs create mode 100644 src/PanoptesFrontend/Data/Post.cs create mode 100644 src/PanoptesFrontend/Data/User.cs create mode 100644 src/PanoptesFrontend/Pages/Login.razor create mode 100644 src/PanoptesFrontend/Pages/Register.razor create mode 100644 src/PanoptesFrontend/Services/AccountService.cs rename src/PanoptesFrontend/{Data => Services}/HttpService.cs (70%) diff --git a/src/PanoptesFrontend/Data/Account/AddUser.cs b/src/PanoptesFrontend/Data/Account/AddUser.cs new file mode 100644 index 0000000..3998fa7 --- /dev/null +++ b/src/PanoptesFrontend/Data/Account/AddUser.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; + +namespace PanoptesFrontend.Data.Account; + +public class AddUser{ + + [Required] + public string Username { get; set; } + + [Required] + [MinLength(10, ErrorMessage = "The Password field must be a minimum of 10 characters")] + public string Password { get; set; } +} diff --git a/src/PanoptesFrontend/Data/Account/Login.cs b/src/PanoptesFrontend/Data/Account/Login.cs new file mode 100644 index 0000000..7ee9039 --- /dev/null +++ b/src/PanoptesFrontend/Data/Account/Login.cs @@ -0,0 +1,13 @@ +using System.ComponentModel.DataAnnotations; + +namespace PanoptesFrontend.Data.Account +{ + public class Login + { + [Required] + public string Username { get; set; } + + [Required] + public string Password { get; set; } + } +} \ No newline at end of file diff --git a/src/PanoptesFrontend/Data/Account/LoginUser.cs b/src/PanoptesFrontend/Data/Account/LoginUser.cs new file mode 100644 index 0000000..7c6d859 --- /dev/null +++ b/src/PanoptesFrontend/Data/Account/LoginUser.cs @@ -0,0 +1,12 @@ +using System.ComponentModel.DataAnnotations; + +namespace PanoptesFrontend.Data.Account; + +public class LoginUser +{ + [Required] + public string Username { get; set; } + + [Required] + public string Password { get; set; } +} diff --git a/src/PanoptesFrontend/Data/Post.cs b/src/PanoptesFrontend/Data/Post.cs new file mode 100644 index 0000000..2250a72 --- /dev/null +++ b/src/PanoptesFrontend/Data/Post.cs @@ -0,0 +1,15 @@ +public class Post { + + public int userId {get; set;} + public int id {get; set;} + public string title {get; set;} + public string body {get; set;} + + public Post(int userId, int id, string title, string body){ + userId = userId; + id = id; + title = title; + body = body; + } + +} diff --git a/src/PanoptesFrontend/Data/User.cs b/src/PanoptesFrontend/Data/User.cs new file mode 100644 index 0000000..880b3be --- /dev/null +++ b/src/PanoptesFrontend/Data/User.cs @@ -0,0 +1,9 @@ +namespace PanoptesFrontend.Data +{ + public class User + { + public string Id { get; set; } + public string Username { get; set; } + public string Token { get; set; } + } +} \ No newline at end of file diff --git a/src/PanoptesFrontend/Pages/Login.razor b/src/PanoptesFrontend/Pages/Login.razor new file mode 100644 index 0000000..a6fd16f --- /dev/null +++ b/src/PanoptesFrontend/Pages/Login.razor @@ -0,0 +1,38 @@ +@page "/account/login" +@using PanoptesFrontend.Data.Account; +@using PanoptesFrontend.Services; +@inject IAccountService AccountService + + +
+

Login

+
+ + +
+ + + +
+
+ + + +
+ + Register +
+
+
+ +@code { + private LoginUser model = new LoginUser(); + + private async void OnValidSubmit() + { + Console.WriteLine("Method called"); + await AccountService.Login(model); + } +} \ No newline at end of file diff --git a/src/PanoptesFrontend/Pages/Register.razor b/src/PanoptesFrontend/Pages/Register.razor new file mode 100644 index 0000000..f548edb --- /dev/null +++ b/src/PanoptesFrontend/Pages/Register.razor @@ -0,0 +1,38 @@ +@page "/account/register" +@using PanoptesFrontend.Data.Account; +@using PanoptesFrontend.Services; +@inject IAccountService AccountService + + +
+

Register

+
+ + +
+ + + +
+
+ + + +
+ + Cancel +
+
+
+ +@code { + private AddUser model = new AddUser(); + + private async void OnValidSubmit() + { + Console.WriteLine("Method called"); + await AccountService.Register(model); + } +} \ No newline at end of file diff --git a/src/PanoptesFrontend/Pages/TestRequest.razor b/src/PanoptesFrontend/Pages/TestRequest.razor index 92703d8..fb3d290 100644 --- a/src/PanoptesFrontend/Pages/TestRequest.razor +++ b/src/PanoptesFrontend/Pages/TestRequest.razor @@ -1,6 +1,8 @@ @page "/test" +@using System.Net; @using PanoptesFrontend.Data +@using PanoptesFrontend.Services @using System.Text.Json; @inject IHttpService httpService @@ -33,6 +35,8 @@ @code { private Component[] data; + + protected override async Task OnInitializedAsync() { var response = await httpService.GetAsync("http://localhost:10000/localhost:8080/schema"); diff --git a/src/PanoptesFrontend/Program.cs b/src/PanoptesFrontend/Program.cs index 24bb72d..8349c9f 100644 --- a/src/PanoptesFrontend/Program.cs +++ b/src/PanoptesFrontend/Program.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; -using PanoptesFrontend.Data; +using PanoptesFrontend.Services; var builder = WebApplication.CreateBuilder(args); @@ -9,6 +9,7 @@ builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddHttpClient(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); var app = builder.Build(); diff --git a/src/PanoptesFrontend/Services/AccountService.cs b/src/PanoptesFrontend/Services/AccountService.cs new file mode 100644 index 0000000..a50067e --- /dev/null +++ b/src/PanoptesFrontend/Services/AccountService.cs @@ -0,0 +1,40 @@ +using PanoptesFrontend.Data.Account; +using PanoptesFrontend.Data; +using Microsoft.AspNetCore.Components; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace PanoptesFrontend.Services; + +public interface IAccountService +{ + Task Register(AddUser model); + Task Login(LoginUser model); + Task Logout(User model); + +} + +public class AccountService : IAccountService +{ + private IHttpService httpService; + + public AccountService(IHttpService httpService) { + this.httpService = httpService; + } + + + public async Task Register(AddUser model){ + // endpoint doesnt exist yet + await httpService.PostAsync("http://localhost:8080/user/register", model); + } + + public async Task Login(LoginUser model){ + // endpoint doesnt exist yet + await httpService.PostAsync("http://localhost:8080/user/authenticate", model); + } + + public async Task Logout(User model){ + // endpoint doesnt exist yet + await httpService.PostAsync("http://localhost:8080/user/logout", model); + } +} \ No newline at end of file diff --git a/src/PanoptesFrontend/Data/HttpService.cs b/src/PanoptesFrontend/Services/HttpService.cs similarity index 70% rename from src/PanoptesFrontend/Data/HttpService.cs rename to src/PanoptesFrontend/Services/HttpService.cs index e658e23..1b60c6b 100644 --- a/src/PanoptesFrontend/Data/HttpService.cs +++ b/src/PanoptesFrontend/Services/HttpService.cs @@ -1,11 +1,14 @@ -namespace PanoptesFrontend.Data; +namespace PanoptesFrontend.Services; +using System.Net; using System.Net.Http; +using System.Net.Http.Json; using System.Threading.Tasks; using System.Text.Json; public interface IHttpService { Task GetAsync(string endpoint); + Task PostAsync(string endpoint, object value); } public class HttpService : IHttpService { @@ -28,4 +31,12 @@ public class HttpService : IHttpService { throw new Exception($"Failed to get data from endpoint: {endpoint}. Error code: {response.StatusCode}"); } } + + public async Task PostAsync(string endpoint, object value){ + var request = await httpClient.PostAsJsonAsync(endpoint, value); + + request.EnsureSuccessStatusCode(); + + return request.StatusCode; + } } \ No newline at end of file diff --git a/src/PanoptesFrontend/Shared/NavMenu.razor b/src/PanoptesFrontend/Shared/NavMenu.razor index f2799c9..50bf19d 100644 --- a/src/PanoptesFrontend/Shared/NavMenu.razor +++ b/src/PanoptesFrontend/Shared/NavMenu.razor @@ -1,4 +1,5 @@ -@using PanoptesFrontend.Data +@using PanoptesFrontend.Data; +@using PanoptesFrontend.Services; @using System.Text.Json; @inject IHttpService httpService @@ -13,7 +14,7 @@
@code { - public Module[] response; + @* public Module[] response; protected override async Task OnInitializedAsync() { response = await httpService.GetAsync("http://localhost:10000/modules"); - } + } *@ private bool collapseNavMenu = true; private string? NavMenuCssClass => collapseNavMenu ? "collapse" : null;