diff --git a/Helios/Controllers/LoginController.cs b/Helios/Controllers/LoginController.cs new file mode 100644 index 0000000000000000000000000000000000000000..a7f79babbcca79f2c2d4920300b4e1ff74941932 --- /dev/null +++ b/Helios/Controllers/LoginController.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Helios.Source; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace Helios.Controllers +{ + public class LoginController : Controller + { + private readonly LoginService _loginService; + + public LoginController(LoginService service) + { + _loginService = service; + } + + public IActionResult UserLogin(string emailAddress, string password) + { + Login login = _loginService.GetLogin(emailAddress, password); + + if (login.IsValidUser) + { + return View(); + } + else + { + return View(); + } + } + } +} \ No newline at end of file diff --git a/Helios/Helios.csproj b/Helios/Helios.csproj index 58e835af95531dde3ab8136f7037af9d7dcb3e78..9ac6a6603ec6c13eed653231c619281da2a19be8 100644 --- a/Helios/Helios.csproj +++ b/Helios/Helios.csproj @@ -9,11 +9,15 @@ <ItemGroup> <None Remove="Source\" /> <None Remove="Npgsql" /> + <None Remove="Microsoft.VisualStudio.Web.CodeGeneration.Design" /> + <None Remove="Swashbuckle.AspNetCore" /> </ItemGroup> <ItemGroup> <Folder Include="Source\" /> </ItemGroup> <ItemGroup> <PackageReference Include="Npgsql" Version="6.0.4" /> + <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.3" /> + <PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.0" /> </ItemGroup> </Project> diff --git a/Helios/Program.cs b/Helios/Program.cs index 9fbb57d813d00c423b5df4416b71d395a42421a6..3d467e3fbf9e6292208a38c188c00e991976ac0d 100644 --- a/Helios/Program.cs +++ b/Helios/Program.cs @@ -1,8 +1,13 @@ -var builder = WebApplication.CreateBuilder(args); +using Helios.Source; + +var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); +builder.Services.AddTransient<LoginService>(provider + => new LoginService(builder.Configuration.GetConnectionString("default"))); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/Helios/Source/Login.cs b/Helios/Source/Login.cs new file mode 100644 index 0000000000000000000000000000000000000000..1a4531fb393f3b6e31e5dd1878392ffa511b92ff --- /dev/null +++ b/Helios/Source/Login.cs @@ -0,0 +1,16 @@ +using System; +namespace Helios.Source +{ + public class Login + { + public string EmailAddress { get; set; } + public bool IsValidUser { get; set; } + public bool IsAdmin { get; set; } + + public Login(string emailAddress) + { + EmailAddress = emailAddress; + } + } +} + diff --git a/Helios/Source/LoginService.cs b/Helios/Source/LoginService.cs new file mode 100644 index 0000000000000000000000000000000000000000..3cc92a5b3a76bf572ea83448e0197b3b0208ba8d --- /dev/null +++ b/Helios/Source/LoginService.cs @@ -0,0 +1,52 @@ +using System; +using Npgsql; + +namespace Helios.Source +{ + public class LoginService + { + private readonly NpgsqlConnection _connection; + + private string password = string.Empty; + + public LoginService(string connectionString) + { + _connection = new NpgsqlConnection(connectionString); + } + + public Login GetLogin(string email, string password) + { + this.password = password; + + Login userLogin = new Login(email); + + userLogin = GetUserDetails(userLogin); + + return userLogin; + } + + private Login GetUserDetails(Login userLogin) + { + NpgsqlCommand cmd = new NpgsqlCommand("select * from user_login where email = @emailParam and user_password = @passwordParam", _connection); + + cmd.Parameters.AddWithValue("@emailParam", userLogin.EmailAddress); + cmd.Parameters.AddWithValue("@passwordParam", this.password); + + using var reader = cmd.ExecuteReader(); + + if (reader.Read()) + { + userLogin.IsValidUser = true; + userLogin.IsAdmin = bool.Parse(reader["is_admin"].ToString()); + } + else + { + userLogin.IsValidUser = false; + userLogin.IsAdmin = false; + } + + return userLogin; + } + } +} + diff --git a/SqlScripts/Login.sql b/SqlScripts/Login.sql index 987ec2d507787e28e452f5f214344c2886c88e31..cade967003a25ecc5a4f2d2cd2998db3c44d35b1 100644 --- a/SqlScripts/Login.sql +++ b/SqlScripts/Login.sql @@ -1,11 +1,11 @@ -create table Login( +create table User_Login( id SERIAL PRIMARY KEY, email TEXT, user_password TEXT, is_admin BOOLEAN ) -INSERT INTO Login +INSERT INTO User_Login (email, user_password, is_admin) VALUES ('user@helios.co.uk', 'userpassword', FALSE),