Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Madalan2 committed Nov 29, 2024
1 parent c940d6e commit 39ed72f
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 0 deletions.
59 changes: 59 additions & 0 deletions report/AccountController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

public class AccountController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;

public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}

[HttpGet]
public IActionResult Register() => View();

[HttpPost]
public async Task<IActionResult> 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<IActionResult> 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<IActionResult> Logout()
{
await _signInManager.SignOutAsync();
return RedirectToAction("Index", "Home");
}
}
51 changes: 51 additions & 0 deletions report/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -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<ApplicationUser>
{
public DbSet<Patient> Patients { get; set; }
public DbSet<Appointment> Appointments { get; set; }

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) {}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<Patient>()
.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();
}
}
16 changes: 16 additions & 0 deletions report/Appointment.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
15 changes: 15 additions & 0 deletions report/Patient.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
29 changes: 29 additions & 0 deletions report/PatientController.cs
Original file line number Diff line number Diff line change
@@ -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<IActionResult> Create(Patient patient)
{
if (ModelState.IsValid)
{
_context.Patients.Add(patient);
await _context.SaveChangesAsync();
return RedirectToAction("Index", "Home");
}
return View(patient);
}
}
4 changes: 4 additions & 0 deletions report/projectsetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dotnet new mvc -n HealthcareSystem
cd HealthcareSystem
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore

0 comments on commit 39ed72f

Please sign in to comment.