From 062bdff9da3f0de240b2e952eca3ff0b604aec22 Mon Sep 17 00:00:00 2001 From: Giacomo Bertolazzi <20015159@studenti.uniupo.it> Date: Mon, 21 Jan 2019 15:04:00 +0100 Subject: [PATCH] Association in user --- SeniorAssistant/Controllers/HomeController.cs | 29 +++++++++++------- SeniorAssistant/Models/Users/User.cs | 19 ++++++++++-- SeniorAssistant/Program.cs | 9 +----- SeniorAssistant/Views/Home/Index.cshtml | 6 ++-- SeniorAssistant/Views/Home/Message.cshtml | 13 +++----- SeniorAssistant/Views/Home/User.cshtml | 22 +++++-------- SeniorAssistant/Views/Shared/Profile.cshtml | 27 +++++++--------- SeniorAssistant/senior.db | Bin 114688 -> 114688 bytes 8 files changed, 61 insertions(+), 64 deletions(-) diff --git a/SeniorAssistant/Controllers/HomeController.cs b/SeniorAssistant/Controllers/HomeController.cs index 60d3b07..c47e47d 100644 --- a/SeniorAssistant/Controllers/HomeController.cs +++ b/SeniorAssistant/Controllers/HomeController.cs @@ -1,4 +1,7 @@ -using Microsoft.AspNetCore.Mvc; +using LinqToDB; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using SeniorAssistant.Models; using System.Linq; namespace SeniorAssistant.Controllers @@ -11,7 +14,8 @@ namespace SeniorAssistant.Controllers [Route("Index")] public IActionResult Index() { - return View(); + string username = HttpContext.Session.GetString(Username); + return View("Index", GetUser(username)); } [Route("Heartbeat")] @@ -41,25 +45,28 @@ 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)); + } + + private User GetUser(string username) + { + return Db.Users + .LoadWith(u => u.Doc) + .LoadWith(u => u.Pat) + .Where(u => u.Username.Equals(username)) + .FirstOrDefault(); } private IActionResult CheckAuthorized(string view, object model = null) { if (!IsLogged()) - { - model = "/" + view; - view = "Index"; - } + return View("Index", "/" + view); return View(view, model); } } diff --git a/SeniorAssistant/Models/Users/User.cs b/SeniorAssistant/Models/Users/User.cs index 98f56ba..422f837 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,15 +9,27 @@ 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; } + + [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 index 9157d25..5141726 100644 --- a/SeniorAssistant/Views/Home/Index.cshtml +++ b/SeniorAssistant/Views/Home/Index.cshtml @@ -5,7 +5,7 @@ logo sito disattivare l-aside e le opzioni se non loggato deve tornare qua --> -@model string +@model object @inject IHttpContextAccessor HttpContextAccessor @{ @@ -16,7 +16,7 @@ se non loggato deve tornare qua
@if (session == null) { - @if (Model != null) + @if (Model is string) {

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

} @@ -31,6 +31,6 @@ se non loggato deve tornare qua } else { - await Html.RenderPartialAsync("Profile"); // magari sostituire qui + await Html.RenderPartialAsync("Profile", Model); // magari sostituire qui }
diff --git a/SeniorAssistant/Views/Home/Message.cshtml b/SeniorAssistant/Views/Home/Message.cshtml index 57746bb..edeffa4 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,12 +7,9 @@ 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 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(); } @@ -24,7 +21,7 @@ } else { -

Messaggi con @user.Name @user.LastName

+

Messaggi con @Model.Name @Model.LastName

foreach (var message in messages) { @@ -72,7 +69,7 @@ url: "/Account/_sendMessage", type: "POST", data: { - Receiver: "@Model", + Receiver: "@Model.Username", Body: body }, success: function (data) { diff --git a/SeniorAssistant/Views/Home/User.cshtml b/SeniorAssistant/Views/Home/User.cshtml index 5eeac2a..752b93c 100644 --- a/SeniorAssistant/Views/Home/User.cshtml +++ b/SeniorAssistant/Views/Home/User.cshtml @@ -8,16 +8,8 @@ var username = session.GetString("username"); 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) @@ -31,21 +23,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

- + - +