diff --git a/SeniorAssistant/Controllers/HomeController.cs b/SeniorAssistant/Controllers/HomeController.cs index a92bf6c..e90d269 100644 --- a/SeniorAssistant/Controllers/HomeController.cs +++ b/SeniorAssistant/Controllers/HomeController.cs @@ -1,5 +1,7 @@ using LinqToDB; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using SeniorAssistant.Models; using System.Linq; namespace SeniorAssistant.Controllers @@ -12,9 +14,7 @@ namespace SeniorAssistant.Controllers [Route("Index")] public IActionResult Login() { - if (IsLogged()) - return View("Profile"); - return View(); + return CheckUnAuthorized("Login"); } [Route("Heartbeat")] @@ -44,39 +44,33 @@ namespace SeniorAssistant.Controllers [Route("User/{User}")] public IActionResult SingleUser(string user) { - var u = (from us in Db.Users - where us.Username.Equals(user) - select us).FirstOrDefault(); - return CheckAuthorized("User", u); + return CheckAuthorized("User", GetUser(user)); } [Route("Message/{User}")] public IActionResult Message(string user) { - return CheckAuthorized("Message", user); + return CheckAuthorized("Message", GetUser(user)); } - + [Route("Profile")] public IActionResult Profile() { - return CheckAuthorized("Profile"); + string username = HttpContext.Session.GetString(Username); + return CheckAuthorized("Profile", GetUser(username)); } [Route("Register")] public IActionResult Register() { - if (IsLogged()) - return View("Profile"); - return View(); + return CheckUnAuthorized("Register"); } [Route("Forgot")] public IActionResult Forgot(string username = "") { - if (IsLogged()) - return View("Profile"); var forgot = Db.Forgot.Where(f => f.Username.Equals(username)).FirstOrDefault(); - return View(forgot); + return CheckUnAuthorized("Forgot", forgot); } protected IActionResult CheckAuthorized(string view, object model = null) @@ -88,5 +82,24 @@ namespace SeniorAssistant.Controllers } return View(view, model); } + + protected IActionResult CheckUnAuthorized(string view, object model = null) + { + if (IsLogged()) + { + view = "Profile"; + model = GetUser(HttpContext.Session.GetString(Username)); + } + return View(view, model); + } + + private User GetUser(string username) + { + return Db.Users + .LoadWith(u => u.Doc) + .LoadWith(u => u.Pat) + .Where(u => u.Username.Equals(username)) + .FirstOrDefault(); + } } } \ No newline at end of file diff --git a/SeniorAssistant/Models/Users/User.cs b/SeniorAssistant/Models/Users/User.cs index 54cf019..16bdfea 100644 --- a/SeniorAssistant/Models/Users/User.cs +++ b/SeniorAssistant/Models/Users/User.cs @@ -1,5 +1,6 @@ using LinqToDB.Mapping; using Newtonsoft.Json; +using SeniorAssistant.Models.Users; namespace SeniorAssistant.Models { @@ -8,17 +9,29 @@ namespace SeniorAssistant.Models [Column(IsPrimaryKey = true, CanBeNull = false)] public string Username { get; set; } - [NotNull] + [Column(CanBeNull = false)] public string Email { get; set; } - [NotNull] [JsonIgnore] + [Column(CanBeNull = false)] public string Password { get; set; } public string Name { get; set; } - + public string LastName { get; set; } public string Avatar { get; set; } + + [JsonIgnore] + [Association(ThisKey = nameof(Username), OtherKey = nameof(Doctor.Username), CanBeNull = true)] + public Doctor Doc { get; set; } + + [JsonIgnore] + [Association(ThisKey = nameof(Username), OtherKey = nameof(Patient.Username), CanBeNull = true)] + public Patient Pat { get; set; } + + public bool IsDoctor() => Doc != null; + + public bool IsPatient() => Pat != null; } } diff --git a/SeniorAssistant/Program.cs b/SeniorAssistant/Program.cs index a0907b3..3d5efa5 100644 --- a/SeniorAssistant/Program.cs +++ b/SeniorAssistant/Program.cs @@ -1,12 +1,5 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; namespace SeniorAssistant { diff --git a/SeniorAssistant/Views/Home/Index.cshtml b/SeniorAssistant/Views/Home/Index.cshtml deleted file mode 100644 index 9157d25..0000000 --- a/SeniorAssistant/Views/Home/Index.cshtml +++ /dev/null @@ -1,36 +0,0 @@ - -@model string -@inject IHttpContextAccessor HttpContextAccessor - -@{ - ViewBag.Title = "Hello Razor"; - string session = HttpContextAccessor.HttpContext.Session.GetString("username"); -} - -
- @if (session == null) - { - @if (Model != null) - { -

Per poter accedere alla pagina [@Model] e' necessario essere loggati

- } - -
- @{ await Html.RenderPartialAsync("Login"); } -
-
- @{ await Html.RenderPartialAsync("Register"); } -
- - } - else - { - await Html.RenderPartialAsync("Profile"); // magari sostituire qui - } -
diff --git a/SeniorAssistant/Views/Home/Message.cshtml b/SeniorAssistant/Views/Home/Message.cshtml index 57746bb..a5c2350 100644 --- a/SeniorAssistant/Views/Home/Message.cshtml +++ b/SeniorAssistant/Views/Home/Message.cshtml @@ -1,4 +1,4 @@ -@model string +@model User @inject IHttpContextAccessor HttpContextAccessor @inject IDataContextFactory dbFactory @using LinqToDB; @@ -7,14 +7,12 @@ ViewBag.Title = "Hello Razor"; string username = HttpContextAccessor.HttpContext.Session.GetString("username"); var db = dbFactory.Create(); - var user = (from u in db.Users - where u.Username.Equals(Model) - select u).FirstOrDefault(); + var MaxMessages = 20; var messages = (from m in db.Messages - where (m.Username.Equals(Model) && m.Receiver.Equals(username)) - ||(m.Receiver.Equals(Model) && m.Username.Equals(username)) + where (m.Username.Equals(Model.Username) && m.Receiver.Equals(username)) + ||(m.Receiver.Equals(Model.Username) && m.Username.Equals(username)) orderby m.Time ascending - select m).ToArray(); + select m).Take(MaxMessages).ToArray(); }
@@ -24,7 +22,7 @@ } else { -

Messaggi con @user.Name @user.LastName

+

Messaggi con @Model.Name @Model.LastName

foreach (var message in messages) { @@ -72,7 +70,7 @@ url: "/Account/_sendMessage", type: "POST", data: { - Receiver: "@Model", + Receiver: "@Model.Username", Body: body }, success: function (data) { diff --git a/SeniorAssistant/Views/Home/Profile.cshtml b/SeniorAssistant/Views/Home/Profile.cshtml index 10afeda..0a0b8cb 100644 --- a/SeniorAssistant/Views/Home/Profile.cshtml +++ b/SeniorAssistant/Views/Home/Profile.cshtml @@ -1,44 +1,39 @@ @inject IHttpContextAccessor HttpContextAccessor @inject IDataContextFactory dbFactory - -@{ - var session = HttpContextAccessor.HttpContext.Session; - var db = dbFactory.Create(); - var username = session.GetString("username"); - var patientData = db.Patients.Where(p => p.Username.Equals(username)).ToArray().FirstOrDefault(); - var hasDoc = patientData != null; -} +@model User

- Welcome @username + Welcome @Model.Name @Model.LastName

- name: @session.GetString("name")
- lastname: @session.GetString("lastname")
- email: @session.GetString("email")
+ name: @Model.Name
+ lastname: @Model.LastName
+ email: @Model.Email
- @if (hasDoc) // is patient and has doc, must show doc data + @if (Model.IsPatient()) // is patient and has doc, must show doc data { + var db = dbFactory.Create(); var doctor = (from u in db.Users join d in db.Doctors on u.Username equals d.Username - where d.Username.Equals(patientData.Doctor) + where d.Username.Equals(Model.Pat.Doctor) select new { u.Username, u.Name, u.LastName, d.Location }).ToArray().First();

Dottore: @doctor.Name @doctor.LastName

Dove mi puoi trovare? @doctor.Location

- + Invia un messaggio al tuo dottore } else { + var db = dbFactory.Create(); dynamic[] data; Type type = null; string title = null; - var docData = db.Doctors.Where(d => d.Username.Equals(username)).ToArray().FirstOrDefault(); + var docData = db.Doctors.Where(d => d.Username.Equals(Model.Username)).ToArray().FirstOrDefault(); if (docData != null) // is DOC { diff --git a/SeniorAssistant/Views/Home/User.cshtml b/SeniorAssistant/Views/Home/User.cshtml index c4ea0b4..4829542 100644 --- a/SeniorAssistant/Views/Home/User.cshtml +++ b/SeniorAssistant/Views/Home/User.cshtml @@ -9,16 +9,8 @@ bool filter = HttpContextAccessor.HttpContext.Request.Query["from"] != (String)null; bool auth = username.Equals(Model.Username); - bool isDoc = session.GetString("role").Equals("doctor"); - Patient patient = null; - if (isDoc) - { - var db = dbFactory.Create(); - patient = (from p in db.Patients - where p.Username.Equals(Model.Username) && p.Doctor.Equals(username) - select p).ToArray().FirstOrDefault(); - auth = auth || patient != null; - } + bool isDoc = Model.IsPatient() && username.Equals(Model.Pat.Doctor); + auth = auth || isDoc; } @if (!auth) @@ -41,21 +33,21 @@ else
- @if (isDoc && patient != null) + @if (isDoc) {
- +

- Invia un messaggio al tuo paziente + Invia un messaggio al tuo paziente

Inserisci un minimo o massimo valore per il battito cardiaco

Se il valore del battito del paziente supera i valori che hai inserito verrai notificato

- + - +