diff --git a/report/AccountController.cs b/report/AccountController.cs new file mode 100644 index 0000000..98c5846 --- /dev/null +++ b/report/AccountController.cs @@ -0,0 +1,59 @@ +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using System.Threading.Tasks; + +public class AccountController : Controller +{ + private readonly UserManager _userManager; + private readonly SignInManager _signInManager; + + public AccountController(UserManager userManager, SignInManager signInManager) + { + _userManager = userManager; + _signInManager = signInManager; + } + + [HttpGet] + public IActionResult Register() => View(); + + [HttpPost] + public async Task Register(RegisterViewModel model) + { + if (ModelState.IsValid) + { + var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; + var result = await _userManager.CreateAsync(user, model.Password); + if (result.Succeeded) + { + await _signInManager.SignInAsync(user, isPersistent: false); + return RedirectToAction("Index", "Home"); + } + foreach (var error in result.Errors) + ModelState.AddModelError(string.Empty, error.Description); + } + return View(model); + } + + [HttpGet] + public IActionResult Login() => View(); + + [HttpPost] + public async Task Login(LoginViewModel model) + { + if (ModelState.IsValid) + { + var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false); + if (result.Succeeded) + return RedirectToAction("Index", "Home"); + + ModelState.AddModelError(string.Empty, "Invalid login attempt."); + } + return View(model); + } + + public async Task Logout() + { + await _signInManager.SignOutAsync(); + return RedirectToAction("Index", "Home"); + } +} diff --git a/report/ApplicationDbContext.cs b/report/ApplicationDbContext.cs new file mode 100644 index 0000000..37af93e --- /dev/null +++ b/report/ApplicationDbContext.cs @@ -0,0 +1,51 @@ +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; +using System.ComponentModel.DataAnnotations; +using System.Security.Cryptography; +using System.Text; + +public class ApplicationDbContext : IdentityDbContext +{ + public DbSet Patients { get; set; } + public DbSet Appointments { get; set; } + + public ApplicationDbContext(DbContextOptions options) : base(options) {} + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.Entity() + .Property(p => p.MedicalRecords) + .HasConversion( + v => EncryptData(v), + v => DecryptData(v) + ); + } + + private string EncryptData(string data) + { + if (string.IsNullOrEmpty(data)) return data; + using var aes = Aes.Create(); + aes.Key = Encoding.UTF8.GetBytes("YourSecureKeyHere!"); // Ensure this key is secure + aes.IV = new byte[16]; // Initialization vector + var encryptor = aes.CreateEncryptor(aes.Key, aes.IV); + using var ms = new MemoryStream(); + using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) + using (var sw = new StreamWriter(cs)) sw.Write(data); + return Convert.ToBase64String(ms.ToArray()); + } + + private string DecryptData(string encryptedData) + { + if (string.IsNullOrEmpty(encryptedData)) return encryptedData; + using var aes = Aes.Create(); + aes.Key = Encoding.UTF8.GetBytes("YourSecureKeyHere!"); + aes.IV = new byte[16]; + var decryptor = aes.CreateDecryptor(aes.Key, aes.IV); + using var ms = new MemoryStream(Convert.FromBase64String(encryptedData)); + using var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read); + using var sr = new StreamReader(cs); + return sr.ReadToEnd(); + } +} diff --git a/report/Appointment.cs b/report/Appointment.cs new file mode 100644 index 0000000..838cdca --- /dev/null +++ b/report/Appointment.cs @@ -0,0 +1,16 @@ +using System; + +public class Appointment +{ + public int Id { get; set; } + public DateTime AppointmentDate { get; set; } + + [Required] + public string CaregiverId { get; set; } + + [Required] + public int PatientId { get; set; } + + public string Notes { get; set; } + public bool IsConfirmed { get; set; } +} diff --git a/report/Patient.cs b/report/Patient.cs new file mode 100644 index 0000000..f098234 --- /dev/null +++ b/report/Patient.cs @@ -0,0 +1,15 @@ +public class Patient +{ + public int Id { get; set; } + + [Required] + [StringLength(100)] + public string Name { get; set; } + + [Required] + public string Address { get; set; } + + [Required] + [DataType(DataType.MultilineText)] + public string MedicalRecords { get; set; } +} diff --git a/report/PatientController.cs b/report/PatientController.cs new file mode 100644 index 0000000..0768931 --- /dev/null +++ b/report/PatientController.cs @@ -0,0 +1,29 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using System.Threading.Tasks; + +[Authorize] +public class PatientController : Controller +{ + private readonly ApplicationDbContext _context; + + public PatientController(ApplicationDbContext context) + { + _context = context; + } + + [HttpGet] + public IActionResult Create() => View(); + + [HttpPost] + public async Task Create(Patient patient) + { + if (ModelState.IsValid) + { + _context.Patients.Add(patient); + await _context.SaveChangesAsync(); + return RedirectToAction("Index", "Home"); + } + return View(patient); + } +} diff --git a/report/projectsetup.cs b/report/projectsetup.cs new file mode 100644 index 0000000..3e1ed9c --- /dev/null +++ b/report/projectsetup.cs @@ -0,0 +1,4 @@ +dotnet new mvc -n HealthcareSystem +cd HealthcareSystem +dotnet add package Microsoft.EntityFrameworkCore.SqlServer +dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore