diff --git a/SeniorAssistant/Controllers/AccountController.cs b/SeniorAssistant/Controllers/AccountController.cs index 6a999d4..0eb61e9 100644 --- a/SeniorAssistant/Controllers/AccountController.cs +++ b/SeniorAssistant/Controllers/AccountController.cs @@ -6,6 +6,8 @@ using LinqToDB; using System.Linq; using System; using SeniorAssistant.Models.Users; +using SeniorAssistant.Data; +using System.Threading.Tasks; namespace IdentityDemo.Controllers { @@ -14,111 +16,112 @@ namespace IdentityDemo.Controllers public class AccountController : BaseController { private static readonly string NoteModified = "Il tuo dottore ha modificato la nota per te"; + private static readonly string InvalidLogIn = "Username o Password sbagliati"; + private static readonly string AlreadyLogIn = "L'utente e' gia' loggato"; + private static readonly string UsernameDupl = "Lo username selezionato e' gia' in uso"; + private static readonly string ModNotExists = "L'oggetto da modificare non esiste"; + private static readonly string AlreadyPatie = "Sei gia' un paziente"; + private static readonly string DocNotExists = "Il dottore selezionato non esiste"; + private static readonly string InsertAsDoct = "Ti ha inserito come il suo dottore: "; [HttpPost] - public ActionResult _login(string username, string password) + public async Task _login(string username, string password) { - JsonResponse response = new JsonResponse - { - Success = false, - Message = "Username or password is invalid." - }; - - var result = Db.GetTable().Where(user => user.Username.Equals(username) && user.Password.Equals(password)).ToListAsync().Result; + var result = await (from u in Db.Users + where u.Username.Equals(username) + && u.Password.Equals(password) + select u).ToListAsync(); if (result.Count == 1) { - var loggedUser = HttpContext.Session.GetString(Username); - if (loggedUser==null || !loggedUser.Equals(username)) // non ha senso - { - User user = result.First(); - HttpContext.Session.SetString(Username, username); - HttpContext.Session.SetString("email", user.Email); - HttpContext.Session.SetString("name", user.Name); - HttpContext.Session.SetString("lastname", user.LastName); + User user = result.First(); + HttpContext.Session.SetString(Username, username); + HttpContext.Session.SetString("email", user.Email); + HttpContext.Session.SetString("name", user.Name); + HttpContext.Session.SetString("lastname", user.LastName); - var isDoc = (from d in Db.Doctors - where d.Username.Equals(username) - select d).ToArray().FirstOrDefault() != null; - HttpContext.Session.SetString("role", isDoc? "doctor":"patient"); + var isDoc = (from d in Db.Doctors + where d.Username.Equals(username) + select d).ToArray().FirstOrDefault() != null; + HttpContext.Session.SetString("role", isDoc? "doctor":"patient"); - response.Success = true; - response.Message = Request.Query["ReturnUrl"]; - } - else - { - response.Message = "User already logged"; - } + return Json(OkJson); } - return Json(response); + return Json(new JsonResponse() + { + Success = false, + Message = InvalidLogIn + }); } [HttpPost] public ActionResult _logout() { HttpContext.Session.Clear(); - return Json(new JsonResponse()); + return Json(OkJson); } [HttpPost] - public ActionResult _register(User user) + public async Task _register(User user) { - return Action(() => + try { - try + Db.Insert(user); + return await _login(user.Username, user.Password); + } + catch + { + return Json(new JsonResponse() { - Db.Insert(user); - return _login(user.Username, user.Password); - } - catch - { - return Json(new JsonResponse(false, "Username already exists")); - } - }); + Success = false, + Message = UsernameDupl + }); + } } [HttpPost] - public ActionResult _notification(string username, string message) + public async Task _notification(string username, string message, string redirectUrl = "#") { - return LoggedAction(() => + return await LoggedAction(() => { Db.Insert(new Notification() { - Message = message, - Username = username, - Time = DateTime.Now, - Seen = false + Body = message, + Username = HttpContext.Session.GetString(Username), + Receiver = username, + Url = redirectUrl, + Time = DateTime.Now }); return Json(OkJson); }); } [HttpPut] - public ActionResult _notification(int id) + public async Task _notification(int id) { - return LoggedAction(() => + return await LoggedAction(() => { JsonResponse response = OkJson; Notification note = Db.Notifications.Where(n => n.Id == id).ToArray().FirstOrDefault(); if(note != null) { - note.Seen = true; + note.Seen = DateTime.Now; Db.Update(note); } else { response.Success = false; - response.Message = "La notifica da modificare non esiste"; + response.Message = ModNotExists; } return Json(response); }); } [HttpPost] - public ActionResult _addDoc(string doctor) + public async Task _addDoc(string doctor) { - return LoggedAction(() => + return await LoggedAction(() => { string username = HttpContext.Session.GetString(Username); var isAlreadyPatient = Db.Patients.Where(p => p.Username.Equals(username)).ToArray().FirstOrDefault() != null; @@ -126,7 +129,7 @@ namespace IdentityDemo.Controllers return Json(new JsonResponse() { Success = false, - Message = "You are already a patient" + Message = AlreadyPatie }); var docExist = Db.Doctors.Where(d => d.Username.Equals(doctor)).ToArray().FirstOrDefault() != null; @@ -134,7 +137,7 @@ namespace IdentityDemo.Controllers return Json(new JsonResponse() { Success = false, - Message = "Doctor doesn't exist" + Message = DocNotExists }); Db.Insert(new Patient() @@ -143,40 +146,65 @@ namespace IdentityDemo.Controllers Username = username }); - _notification(doctor, "L'utente "+username+" ti ha inserito come il suo dottore."); - return Json(new JsonResponse()); + var a = _notification(doctor, InsertAsDoct + username); + return Json(OkJson); }); } [HttpPost] - public ActionResult _sendMessage(string reciver, string body) + public async Task _sendMessage(string receiver, string body) { - return LoggedAction(() => { + return await LoggedAction(() => { string username = HttpContext.Session.GetString(Username); Message message = new Message() { - Reciver = reciver, + Receiver = receiver, Body = body, Time = DateTime.Now, - Username = username, - Seen = false + Username = username }; Db.Insert(message); - return Json(new JsonResponse()); + return Json(OkJson); }); } [HttpPut] - public ActionResult _addNote(string patient, string text) + public async Task _addNote(string patient, string text) { - return LoggedAccessDataOf(patient, () => + return await LoggedAccessDataOf(patient, true, () => { var pat = Db.Patients.Where((p) => p.Username.Equals(patient)).FirstOrDefault(); pat.Notes = text; Db.Update(pat); - _notification(patient, NoteModified); + var a = _notification(patient, NoteModified); + + return Json(OkJson); + }); + } + + [HttpPut] + public async Task _minHeartToPatient(string patient, int value) + { + return await LoggedAccessDataOf(patient, true, () => + { + var pat = Db.Patients.Where((p) => p.Username.Equals(patient)).FirstOrDefault(); + pat.MinHeart = value; + Db.Update(pat); + + return Json(OkJson); + }); + } + + [HttpPut] + public async Task _maxHeartToPatient(string patient, int value) + { + return await LoggedAccessDataOf(patient, true, () => + { + var pat = Db.Patients.Where((p) => p.Username.Equals(patient)).FirstOrDefault(); + pat.MaxHeart = value; + Db.Update(pat); return Json(OkJson); }); diff --git a/SeniorAssistant/Controllers/HomeController.cs b/SeniorAssistant/Controllers/HomeController.cs index daefdd2..60d3b07 100644 --- a/SeniorAssistant/Controllers/HomeController.cs +++ b/SeniorAssistant/Controllers/HomeController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using System.Linq; namespace SeniorAssistant.Controllers { @@ -16,19 +17,19 @@ namespace SeniorAssistant.Controllers [Route("Heartbeat")] public IActionResult Heartbeat() { - return CheckAuthorized("Heartbeat"); + return CheckAuthorized("Data", "Heartbeat"); } [Route("Sleep")] public IActionResult Sleep() { - return CheckAuthorized("Sleep"); + return CheckAuthorized("Data", "Sleep"); } [Route("Step")] public IActionResult Step() { - return CheckAuthorized("Step"); + return CheckAuthorized("Data", "Step"); } [Route("Users")] @@ -40,13 +41,16 @@ namespace SeniorAssistant.Controllers [Route("User/{User}")] public IActionResult SingleUser(string user) { - return CheckAuthorized("Data", user); + var u = (from us in Db.Users + where us.Username.Equals(user) + select us).FirstOrDefault(); + return CheckAuthorized("User", u); } - [Route("Message/{Id}")] - public IActionResult Message(int id) + [Route("Message/{User}")] + public IActionResult Message(string user) { - return CheckAuthorized("Message", id); + return CheckAuthorized("Message", user); } private IActionResult CheckAuthorized(string view, object model = null) diff --git a/SeniorAssistant/Controllers/Services/ApiControllers.cs b/SeniorAssistant/Controllers/Services/ApiControllers.cs index 7959e9b..3b16c6c 100644 --- a/SeniorAssistant/Controllers/Services/ApiControllers.cs +++ b/SeniorAssistant/Controllers/Services/ApiControllers.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using SeniorAssistant.Models; +using SeniorAssistant.Models.Data; using SeniorAssistant.Models.Users; namespace SeniorAssistant.Controllers.Services diff --git a/SeniorAssistant/Controllers/Services/BaseController.cs b/SeniorAssistant/Controllers/Services/BaseController.cs index 94dc2fb..092544b 100644 --- a/SeniorAssistant/Controllers/Services/BaseController.cs +++ b/SeniorAssistant/Controllers/Services/BaseController.cs @@ -1,8 +1,10 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using SeniorAssistant.Data; +using SeniorAssistant.Models.Users; using System.Linq; using System; +using System.Threading.Tasks; namespace SeniorAssistant.Controllers { @@ -10,12 +12,13 @@ namespace SeniorAssistant.Controllers { protected static readonly string MustBeLogged = "Devi essere loggato per vedere/modificare questo dato"; protected static readonly string InvalidModel = "Modello non valido"; - protected static readonly string NoAuthorized = "Non sei autorizzato a vedere questi dati"; + protected static readonly string NoAuthorized = "Non sei autorizzato ad accedere a questi dati"; + protected static readonly string ExceptionSer = "Il server ha riscontrato un problema: "; protected static readonly string Username = "username"; protected readonly JsonResponse OkJson = new JsonResponse(); - IDataContextFactory dbFactory; - SeniorDataContext db; + private IDataContextFactory dbFactory; + private SeniorDataContext db; protected T TryResolve() => (T)HttpContext.RequestServices.GetService(typeof(T)); @@ -34,42 +37,45 @@ namespace SeniorAssistant.Controllers { return HttpContext.Session.GetString(Username) != null; } - - protected ActionResult Action(Func success) + + protected async Task LoggedAction(Func success) { - return ModelState.IsValid ? - success.Invoke() : - Json(new JsonResponse() + try + { + if (IsLogged()) + return success.Invoke(); + + return Json(new JsonResponse() { Success = false, - Message = InvalidModel + Message = MustBeLogged }); + } + catch (Exception e) + { + return Json(new JsonResponse() + { + Success = false, + Message = ExceptionSer + Environment.NewLine + + e.Message + Environment.NewLine + + e.StackTrace + Environment.NewLine + + e.TargetSite + Environment.NewLine + + e.InnerException + }); + } } - protected ActionResult LoggedAction(Func success) + protected async Task LoggedAccessDataOf(string username, bool patients, Func success) { - return Action(() => + return await LoggedAction(() => { - return IsLogged() ? - success.Invoke() : - Json(new JsonResponse() - { - Success = false, - Message = MustBeLogged - }); - }); - } - - protected ActionResult LoggedAccessDataOf(string username, Func success, bool patients = true) - { - return LoggedAction(() => - { - var loggedUser = HttpContext.Session.GetString(Username); - var condition = username.Equals(loggedUser); - - condition = condition || (patients && (from patient in Db.Patients - where patient.Doctor.Equals(loggedUser) && patient.Username.Equals(username) - select patient).ToArray().FirstOrDefault() != null); + var session = HttpContext.Session.GetString(Username); + var condition = username.Equals(session); + var query = from patient in Db.Patients + where patient.Doctor.Equals(session) && patient.Username.Equals(username) + select patient; + var num = query.ToList().Count(); + condition = condition || (patients && num != 0); return condition ? success.Invoke() : @@ -80,6 +86,47 @@ namespace SeniorAssistant.Controllers }); }); } + + static protected JsonResponse LoggedAction(SeniorDataContext db, string session, Func success) + { + try + { + return session != null ? + success.Invoke() : + new JsonResponse() + { + Success = false, + Message = MustBeLogged + }; + } + catch (Exception e) + { + return new JsonResponse() + { + Success = false, + Message = ExceptionSer + e.Message + }; + } + } + + static protected JsonResponse LoggedAccessDataOf(SeniorDataContext db, string session, string username, bool patients, Func success) + { + return LoggedAction(db, session, () => + { + var condition = username.Equals(session); + condition = condition || (patients && (from patient in db.Patients + where patient.Doctor.Equals(session) && patient.Username.Equals(username) + select patient).ToArray().FirstOrDefault() != null); + + return condition ? + success.Invoke() : + new JsonResponse() + { + Success = false, + Message = NoAuthorized + }; + }); + } } public class JsonResponse diff --git a/SeniorAssistant/Controllers/Services/CrudController.cs b/SeniorAssistant/Controllers/Services/CrudController.cs index 4ba8e87..3f5291b 100644 --- a/SeniorAssistant/Controllers/Services/CrudController.cs +++ b/SeniorAssistant/Controllers/Services/CrudController.cs @@ -13,7 +13,7 @@ namespace SeniorAssistant.Controllers.Services [HttpGet("{username}")] public async Task Read(string username) { - return LoggedAccessDataOf(username, () => + return await LoggedAccessDataOf(username, true, () => { return Json(Db.GetTable().Where((u) => u.Username.Equals(username)).ToArray()); }); @@ -22,12 +22,12 @@ namespace SeniorAssistant.Controllers.Services [HttpPut("{username}")] public async Task Update(string username, [FromBody] TEntity entity) { - return LoggedAccessDataOf(username, () => + return await LoggedAccessDataOf(username, false, () => { entity.Username = username; Db.Update(entity); return Json(OkJson); - }, false); + }); } } } diff --git a/SeniorAssistant/Controllers/Services/CrudTimeController.cs b/SeniorAssistant/Controllers/Services/CrudTimeController.cs index 1235bdf..897f314 100644 --- a/SeniorAssistant/Controllers/Services/CrudTimeController.cs +++ b/SeniorAssistant/Controllers/Services/CrudTimeController.cs @@ -1,8 +1,10 @@ using LinqToDB; using Microsoft.AspNetCore.Mvc; +using SeniorAssistant.Models.Data; using SeniorAssistant.Models; using System; using System.Linq; +using System.Net; using System.Threading.Tasks; namespace SeniorAssistant.Controllers.Services @@ -11,6 +13,7 @@ namespace SeniorAssistant.Controllers.Services where TEntity : class, IHasTime { private static readonly string DateNotCorrect = "Il formato della data non e' corretto"; + private static readonly string AnomalDataHear = "Valore dei battiti cardiaci anomalo"; [HttpGet("{username}/{date:regex((today|\\d{{4}}-\\d{{2}}-\\d{{2}}))}/{hour:range(0, 23)?}")] public async Task Read(string username, string date, int hour = -1) => await Read(username, date, date, hour); @@ -18,7 +21,7 @@ namespace SeniorAssistant.Controllers.Services [HttpGet("{username}/{from:regex((today|\\d{{4}}-\\d{{2}}-\\d{{2}}))}/{to:regex((today|\\d{{4}}-\\d{{2}}-\\d{{2}}))}/{hour:range(0, 23)?}")] public async Task Read(string username, string from, string to, int hour = -1) { - return LoggedAccessDataOf(username, () => + return await LoggedAccessDataOf(username, true, () => { try { @@ -42,7 +45,7 @@ namespace SeniorAssistant.Controllers.Services [HttpGet("{username}/last/{hour:min(1)}")] public async Task Read(string username, int hour) { - return LoggedAccessDataOf(username, () => + return await LoggedAccessDataOf(username, true, () => { DateTime date = DateTime.Now.AddHours(-hour); return Json((from entity in Db.GetTable() @@ -53,24 +56,42 @@ namespace SeniorAssistant.Controllers.Services } [HttpPost] - public async Task Create([FromBody]TEntity item) + public async Task Create(TEntity item) { - return Action(() => + return await LoggedAccessDataOf(item.Username, false, () => { Db.Insert(item); + + if (item is Heartbeat temp) + { + var result = (from p in Db.Patients + where p.Username.Equals(item.Username) + select p).ToArray().FirstOrDefault(); + if (result.MinHeart > temp.Value || result.MaxHeart < temp.Value) + { + var date = WebUtility.UrlEncode(item.Time.ToString("yyyy/MM/dd")); + Db.Insert(new Notification() { + Username = item.Username, + Receiver = result.Doctor, + Body = item.Username + ":" + AnomalDataHear, + Url = "/user/" + item.Username + "?from=" + date + "&to=" + date, + Time = DateTime.Now + }); + } + } + return Json(OkJson); }); } [HttpPut] - public async Task Update([FromBody]TEntity item) + public async Task Update(TEntity item) { - return LoggedAccessDataOf(item.Username, () => + return await LoggedAccessDataOf(item.Username, false, () => { - var e = Read(item.Username, item.Time); - if (e == null) + if (Read(item.Username, item.Time) == null) { - Create(item); + var a = Create(item); } else { @@ -78,7 +99,7 @@ namespace SeniorAssistant.Controllers.Services } return Json(OkJson); - }, false); + }); } [NonAction] diff --git a/SeniorAssistant/Data/SeniorDataContext.cs b/SeniorAssistant/Data/SeniorDataContext.cs index 027a32c..389283a 100644 --- a/SeniorAssistant/Data/SeniorDataContext.cs +++ b/SeniorAssistant/Data/SeniorDataContext.cs @@ -1,7 +1,10 @@ -using LinqToDB; +using System; +using System.Linq; +using LinqToDB; using LinqToDB.Data; using LinqToDB.DataProvider; using SeniorAssistant.Models; +using SeniorAssistant.Models.Data; using SeniorAssistant.Models.Users; namespace SeniorAssistant.Data @@ -20,5 +23,37 @@ namespace SeniorAssistant.Data public ITable Patients => GetTable(); public ITable Notifications => GetTable(); public ITable Messages => GetTable(); + + public T[] GetLastMessages(ITable table, string receiver, ref int numNotSeen, int max = 10) + where T : IHasMessage + { + var notSeen = (from t in table + where t.Receiver.Equals(receiver) && t.Seen == default + orderby t.Time descending + select t).Take(max).ToArray(); + var messages = new T[max]; + numNotSeen = notSeen.Length; + + int i; + for (i = 0; i < numNotSeen; i++) + { + messages[i] = notSeen[i]; + } + + if (numNotSeen < max) + { + var messSeen = (from t in table + where t.Receiver.Equals(receiver) && t.Seen != default + orderby t.Time descending + select t).Take(max - numNotSeen).ToArray(); + + foreach (var m in messSeen) + { + messages[i] = m; + i++; + } + } + return messages; + } } } diff --git a/SeniorAssistant/Models/Heartbeat.cs b/SeniorAssistant/Models/Data/Heartbeat.cs similarity index 60% rename from SeniorAssistant/Models/Heartbeat.cs rename to SeniorAssistant/Models/Data/Heartbeat.cs index 7bcfd6d..c88a4a2 100644 --- a/SeniorAssistant/Models/Heartbeat.cs +++ b/SeniorAssistant/Models/Data/Heartbeat.cs @@ -1,7 +1,7 @@ using LinqToDB.Mapping; using System; -namespace SeniorAssistant.Models +namespace SeniorAssistant.Models.Data { public class Heartbeat : IHasTime { @@ -14,10 +14,5 @@ namespace SeniorAssistant.Models public DateTime Time { get; set; } public double Value { get; set; } - - /* - [Association(ThisKey = nameof(Username), OtherKey = nameof(User.Username), CanBeNull = false)] - public User UserObj { get; set; } - */ } } diff --git a/SeniorAssistant/Models/Sleep.cs b/SeniorAssistant/Models/Data/Sleep.cs similarity index 88% rename from SeniorAssistant/Models/Sleep.cs rename to SeniorAssistant/Models/Data/Sleep.cs index b542d99..afdac3e 100644 --- a/SeniorAssistant/Models/Sleep.cs +++ b/SeniorAssistant/Models/Data/Sleep.cs @@ -1,7 +1,7 @@ using LinqToDB.Mapping; using System; -namespace SeniorAssistant.Models +namespace SeniorAssistant.Models.Data { public class Sleep : IHasTime { diff --git a/SeniorAssistant/Models/Step.cs b/SeniorAssistant/Models/Data/Step.cs similarity index 88% rename from SeniorAssistant/Models/Step.cs rename to SeniorAssistant/Models/Data/Step.cs index 3385092..1cf35b1 100644 --- a/SeniorAssistant/Models/Step.cs +++ b/SeniorAssistant/Models/Data/Step.cs @@ -1,7 +1,7 @@ using LinqToDB.Mapping; using System; -namespace SeniorAssistant.Models +namespace SeniorAssistant.Models.Data { public class Step : IHasTime { diff --git a/SeniorAssistant/Models/Interfaces/IHasMessage.cs b/SeniorAssistant/Models/Interfaces/IHasMessage.cs new file mode 100644 index 0000000..3758df1 --- /dev/null +++ b/SeniorAssistant/Models/Interfaces/IHasMessage.cs @@ -0,0 +1,14 @@ +using LinqToDB.Mapping; +using System; + +namespace SeniorAssistant.Models +{ + public interface IHasMessage : IHasTime + { + string Receiver { get; set; } + + string Body { get; set; } + + DateTime Seen { get; set; } + } +} diff --git a/SeniorAssistant/Models/IHasTime.cs b/SeniorAssistant/Models/Interfaces/IHasTime.cs similarity index 100% rename from SeniorAssistant/Models/IHasTime.cs rename to SeniorAssistant/Models/Interfaces/IHasTime.cs diff --git a/SeniorAssistant/Models/IHasUsername.cs b/SeniorAssistant/Models/Interfaces/IHasUsername.cs similarity index 100% rename from SeniorAssistant/Models/IHasUsername.cs rename to SeniorAssistant/Models/Interfaces/IHasUsername.cs diff --git a/SeniorAssistant/Models/Message.cs b/SeniorAssistant/Models/Message.cs index e76f056..a5cd448 100644 --- a/SeniorAssistant/Models/Message.cs +++ b/SeniorAssistant/Models/Message.cs @@ -3,7 +3,7 @@ using System; namespace SeniorAssistant.Models { - public class Message : IHasTime + public class Message : IHasMessage { [Column(IsPrimaryKey = true, CanBeNull = false, IsIdentity = true)] public int Id { get; set; } @@ -13,14 +13,12 @@ namespace SeniorAssistant.Models [NotNull] public string Username { get; set; } - + [NotNull] - public string Reciver { get; set; } + public string Receiver { get; set; } - [NotNull] public string Body { get; set; } - public bool Seen { get; set; } - + public DateTime Seen { get; set; } } } diff --git a/SeniorAssistant/Models/Notification.cs b/SeniorAssistant/Models/Notification.cs index b5c16a0..9d30aaa 100644 --- a/SeniorAssistant/Models/Notification.cs +++ b/SeniorAssistant/Models/Notification.cs @@ -3,7 +3,7 @@ using System; namespace SeniorAssistant.Models { - public class Notification : IHasTime + public class Notification : IHasMessage { [Column(IsPrimaryKey = true, CanBeNull = false, IsIdentity = true)] public int Id { get; set; } @@ -13,9 +13,14 @@ namespace SeniorAssistant.Models [NotNull] public DateTime Time { get; set; } - - public bool Seen { get; set; } - - public string Message { get; set; } + + [NotNull] + public string Receiver { get; set; } + + public string Body { get; set; } + + public string Url { get; set; } + + public DateTime Seen { get; set; } } } diff --git a/SeniorAssistant/Models/Users/Doctor.cs b/SeniorAssistant/Models/Users/Doctor.cs index f2f0a7a..7abcf3e 100644 --- a/SeniorAssistant/Models/Users/Doctor.cs +++ b/SeniorAssistant/Models/Users/Doctor.cs @@ -7,11 +7,10 @@ namespace SeniorAssistant.Models.Users [Column(IsPrimaryKey = true, CanBeNull = false)] public string Username { get; set; } - [Association(ThisKey = "Username", OtherKey = nameof(User.Username), CanBeNull = false)] - public User UserData { get; set; } - public string Location { get; set; } public string Schedule { get; set; } + + public string PhoneNumber { get; set; } } } diff --git a/SeniorAssistant/Models/Users/Patient.cs b/SeniorAssistant/Models/Users/Patient.cs index 6a326e9..680f404 100644 --- a/SeniorAssistant/Models/Users/Patient.cs +++ b/SeniorAssistant/Models/Users/Patient.cs @@ -7,12 +7,13 @@ namespace SeniorAssistant.Models.Users [Column(IsPrimaryKey = true, CanBeNull = false)] public string Username { get; set; } - [Association(ThisKey = "Username", OtherKey = nameof(User.Username), CanBeNull = false)] - public User UserData { get; set; } - [NotNull] public string Doctor { get; set; } public string Notes { get; set; } + + public int MaxHeart { get; set; } + + public int MinHeart { get; set; } } } diff --git a/SeniorAssistant/SeniorAssistant.csproj b/SeniorAssistant/SeniorAssistant.csproj index 3fb5c1f..4259bee 100644 --- a/SeniorAssistant/SeniorAssistant.csproj +++ b/SeniorAssistant/SeniorAssistant.csproj @@ -4,6 +4,7 @@ netcoreapp2.1 Windows 7506fc54-8ed1-4c9b-9004-35d36db99964 + 7.1 diff --git a/SeniorAssistant/Startup.cs b/SeniorAssistant/Startup.cs index cbf2c20..fece8f5 100644 --- a/SeniorAssistant/Startup.cs +++ b/SeniorAssistant/Startup.cs @@ -10,10 +10,11 @@ using Microsoft.Extensions.DependencyInjection; using SeniorAssistant.Configuration; using SeniorAssistant.Data; using SeniorAssistant.Models; +using SeniorAssistant.Models.Data; +using SeniorAssistant.Models.Users; using SeniorAssistant.Extensions; using Swashbuckle.AspNetCore.Swagger; using Microsoft.Extensions.DependencyInjection.Extensions; -using SeniorAssistant.Models.Users; namespace SeniorAssistant { @@ -71,21 +72,6 @@ namespace SeniorAssistant // .AddEntityFrameworkStores(); services.TryAddSingleton(); - services.AddSingleton>(new List - { - new MenuItem("Index", "/"), - new SubMenu() - { - Text = "Raw Data", - Items = new MenuItem[] - { - new MenuItem("Users", "/users"), - new MenuItem("Heartbeat", "/heartbeat"), - new MenuItem("Sleep", "/sleep"), - new MenuItem("Step", "/step") - } - } - }); var dbFactory = new SeniorDataContextFactory( dataProvider: SQLiteTools.GetDataProvider(), @@ -131,7 +117,7 @@ namespace SeniorAssistant app.Run(async (context) => { - await context.Response.WriteAsync("Oops, something went wrong"); + await context.Response.WriteAsync("Oops, this page doesn't exist"); }); } diff --git a/SeniorAssistant/Views/Home/Data.cshtml b/SeniorAssistant/Views/Home/Data.cshtml index 12a6df6..5426a41 100644 --- a/SeniorAssistant/Views/Home/Data.cshtml +++ b/SeniorAssistant/Views/Home/Data.cshtml @@ -1,154 +1,57 @@ -@inject IHttpContextAccessor HttpContextAccessor -@inject IDataContextFactory dbFactory -@model string - +@model string +@inject IHttpContextAccessor Http @{ ViewBag.Title = "Hello Razor"; - var session = HttpContextAccessor.HttpContext.Session; - var username = session.GetString("username"); - - bool auth = username.Equals(Model); - 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) && p.Doctor.Equals(username) - select p).ToArray().FirstOrDefault(); - auth = auth || patient != null; - } + var session = Http.HttpContext.Session.GetString("username"); } -@if (!auth) -{ -

Non sei autorizzato a vedere i dati di @Model

-} -else -{ - // Aggiungere un qualcosa per scegliere le ore da vedere (Max 48?) -
- - -
-
- @if(isDoc && patient != null) - { -
- - -

-
- - } - - - - -} \ No newline at end of file + }); + \ No newline at end of file diff --git a/SeniorAssistant/Views/Home/Heartbeat.cshtml b/SeniorAssistant/Views/Home/Heartbeat.cshtml deleted file mode 100644 index 64ad73f..0000000 --- a/SeniorAssistant/Views/Home/Heartbeat.cshtml +++ /dev/null @@ -1,42 +0,0 @@ -@{ - ViewBag.Title = "Hello Razor"; -} - -
- - \ No newline at end of file diff --git a/SeniorAssistant/Views/Home/Message.cshtml b/SeniorAssistant/Views/Home/Message.cshtml index df40cae..57746bb 100644 --- a/SeniorAssistant/Views/Home/Message.cshtml +++ b/SeniorAssistant/Views/Home/Message.cshtml @@ -1,4 +1,4 @@ -@model int +@model string @inject IHttpContextAccessor HttpContextAccessor @inject IDataContextFactory dbFactory @using LinqToDB; @@ -7,24 +7,81 @@ ViewBag.Title = "Hello Razor"; string username = HttpContextAccessor.HttpContext.Session.GetString("username"); var db = dbFactory.Create(); - var message = (from m in db.Messages - where m.Id.Equals(Model) && m.Reciver.Equals(username) - select m).ToArray().FirstOrDefault(); + 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)) + orderby m.Time ascending + select m).ToArray(); }
- @if (message == null) + @if (messages.Count() == 0) { -

Non hai il permesso

+

Non hai messaggi

} else { - message.Seen = true; - db.Update(message); -

Messaggio da @message.Username

-

Inviato il @message.Time

-
- @message.Body -
+

Messaggi con @user.Name @user.LastName

+ + foreach (var message in messages) + { + if (message.Seen == default && message.Receiver.Equals(username)) + { + message.Seen = DateTime.Now; + db.Update(message); + } +
+ @if (message.Receiver.Equals(username)) + { +
+
+ @message.Body +

@message.Seen

+
+ } + else + { +
+
@message.Body
+

@message.Seen

+
+
+ } +
+ } } +
+ + +

+
+
diff --git a/SeniorAssistant/Views/Home/Sleep.cshtml b/SeniorAssistant/Views/Home/Sleep.cshtml deleted file mode 100644 index 90454bf..0000000 --- a/SeniorAssistant/Views/Home/Sleep.cshtml +++ /dev/null @@ -1,42 +0,0 @@ -@{ - ViewBag.Title = "Hello Razor"; -} - -
- - \ No newline at end of file diff --git a/SeniorAssistant/Views/Home/Step.cshtml b/SeniorAssistant/Views/Home/Step.cshtml deleted file mode 100644 index 7534cf2..0000000 --- a/SeniorAssistant/Views/Home/Step.cshtml +++ /dev/null @@ -1,42 +0,0 @@ -@{ - ViewBag.Title = "Hello Razor"; -} - -
- - \ No newline at end of file diff --git a/SeniorAssistant/Views/Home/User.cshtml b/SeniorAssistant/Views/Home/User.cshtml new file mode 100644 index 0000000..5eeac2a --- /dev/null +++ b/SeniorAssistant/Views/Home/User.cshtml @@ -0,0 +1,184 @@ +@inject IHttpContextAccessor HttpContextAccessor +@inject IDataContextFactory dbFactory +@model User + +@{ + ViewBag.Title = "Hello Razor"; + var session = HttpContextAccessor.HttpContext.Session; + 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; + } +} + +@if (!auth) +{ +

Non sei autorizzato a vedere i dati di @Model.Name @Model.LastName

+} +else +{ +
+ + +
+
+ @if (isDoc && patient != null) + { +
+ + +

+
+ 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

+ + + + +
+ + } + + +} \ No newline at end of file diff --git a/SeniorAssistant/Views/Shared/Logout.cshtml b/SeniorAssistant/Views/Shared/Logout.cshtml index f65e781..14f9a66 100644 --- a/SeniorAssistant/Views/Shared/Logout.cshtml +++ b/SeniorAssistant/Views/Shared/Logout.cshtml @@ -11,7 +11,7 @@ -