diff --git a/SeniorAssistant/Controllers/AccountController.cs b/SeniorAssistant/Controllers/AccountController.cs index 3f5ac16..a10c4ce 100644 --- a/SeniorAssistant/Controllers/AccountController.cs +++ b/SeniorAssistant/Controllers/AccountController.cs @@ -29,7 +29,7 @@ namespace IdentityDemo.Controllers private static readonly string UploadsDirec = "/uploads/"; [HttpPost] - public async Task _login(string username, string password) + public async Task _login(string username, string password) { try { @@ -70,19 +70,21 @@ namespace IdentityDemo.Controllers } [HttpPost] - public ActionResult _logout() + public IActionResult _logout() { HttpContext.Session.Clear(); return Json(OkJson); } [HttpPost] - public async Task _register(User user, string code = "") + public async Task _register(User user, Forgot forgot, string code = "") { try { user.Avatar = DefaultImage; + forgot.Username = user.Username; Db.Insert(user); + Db.Insert(forgot); if (code != null && code.Equals("444442220")) { Db.Insert(new Doctor @@ -103,7 +105,41 @@ namespace IdentityDemo.Controllers } [HttpPost] - public async Task _notification(string username, string message, string redirectUrl = "#") + public async Task _modify(User user) + { + return await LoggedAccessDataOf(user.Username, false, () => { + var usr = Db.Users.Where(u => u.Username.Equals(user.Username)).FirstOrDefault(); + + if (user.Password.Equals("")) + user.Password = usr.Password; + if (user.Avatar.Equals("")) + user.Avatar = usr.Avatar; + if (user.Email.Equals("")) + user.Email = usr.Email; + if (user.LastName.Equals("")) + user.LastName = usr.LastName; + if (user.Name.Equals("")) + user.Name = usr.Name; + + Db.UpdateAsync(user); + return Json(OkJson); + }); + } + + [HttpPost] + public async Task _checkQuestion(string username, string answer) + { + var forgot = Db.Forgot.Where(f => f.Username.Equals(username) && f.Answer.Equals(answer)).FirstOrDefault(); + if(forgot != null) + { + var user = (from u in Db.Users where u.Username.Equals(forgot.Username) select u).FirstOrDefault(); + return await _login(user.Username, user.Password); + } + return Json(new JsonResponse(false, "Risposta sbagliata")); + } + + [HttpPost] + public async Task _notification(string username, string message, string redirectUrl = "#") { return await LoggedAction(() => { @@ -120,7 +156,7 @@ namespace IdentityDemo.Controllers } [HttpPut] - public async Task _notification(int id) + public async Task _notification(int id) { return await LoggedAction(() => { @@ -142,7 +178,7 @@ namespace IdentityDemo.Controllers } [HttpPost] - public async Task _addDoc(string doctor) + public async Task _addDoc(string doctor) { return await LoggedAction(() => { @@ -175,7 +211,7 @@ namespace IdentityDemo.Controllers } [HttpPost] - public async Task _sendMessage(string receiver, string body) + public async Task _sendMessage(string receiver, string body) { return await LoggedAction(() => { string username = HttpContext.Session.GetString(Username); @@ -194,7 +230,7 @@ namespace IdentityDemo.Controllers } [HttpPut] - public async Task _addNote(string patient, string text) + public async Task _addNote(string patient, string text) { return await LoggedAccessDataOf(patient, true, () => { @@ -208,7 +244,7 @@ namespace IdentityDemo.Controllers } [HttpPut] - public async Task _minHeartToPatient(string patient, int value) + public async Task _minHeartToPatient(string patient, int value) { return await LoggedAccessDataOf(patient, true, () => { @@ -221,7 +257,7 @@ namespace IdentityDemo.Controllers } [HttpPut] - public async Task _maxHeartToPatient(string patient, int value) + public async Task _maxHeartToPatient(string patient, int value) { return await LoggedAccessDataOf(patient, true, () => { @@ -234,7 +270,7 @@ namespace IdentityDemo.Controllers } [HttpPost] - public async Task _save(IEnumerable files) + public async Task _save(IEnumerable files) { return await LoggedAction(() => { diff --git a/SeniorAssistant/Controllers/HomeController.cs b/SeniorAssistant/Controllers/HomeController.cs index 60d3b07..a92bf6c 100644 --- a/SeniorAssistant/Controllers/HomeController.cs +++ b/SeniorAssistant/Controllers/HomeController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Mvc; +using LinqToDB; +using Microsoft.AspNetCore.Mvc; using System.Linq; namespace SeniorAssistant.Controllers @@ -9,8 +10,10 @@ namespace SeniorAssistant.Controllers [Route("")] [Route("Home")] [Route("Index")] - public IActionResult Index() + public IActionResult Login() { + if (IsLogged()) + return View("Profile"); return View(); } @@ -52,13 +55,36 @@ namespace SeniorAssistant.Controllers { return CheckAuthorized("Message", user); } + + [Route("Profile")] + public IActionResult Profile() + { + return CheckAuthorized("Profile"); + } + + [Route("Register")] + public IActionResult Register() + { + if (IsLogged()) + return View("Profile"); + return View(); + } - private IActionResult CheckAuthorized(string view, object model = null) + [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); + } + + protected IActionResult CheckAuthorized(string view, object model = null) { if (!IsLogged()) { + view = "Login"; model = "/" + view; - view = "Index"; } return View(view, model); } diff --git a/SeniorAssistant/Controllers/Services/BaseController.cs b/SeniorAssistant/Controllers/Services/BaseController.cs index db95dbf..aca0d2e 100644 --- a/SeniorAssistant/Controllers/Services/BaseController.cs +++ b/SeniorAssistant/Controllers/Services/BaseController.cs @@ -5,6 +5,7 @@ using SeniorAssistant.Models.Users; using System.Linq; using System; using System.Threading.Tasks; +using SeniorAssistant.Models; namespace SeniorAssistant.Controllers { @@ -37,8 +38,8 @@ namespace SeniorAssistant.Controllers { return HttpContext.Session.GetString(Username) != null; } - - protected async Task LoggedAction(Func success) + + protected async Task LoggedAction(Func success) { try { @@ -56,12 +57,12 @@ namespace SeniorAssistant.Controllers return Json(new JsonResponse() { Success = false, - Message = e.Message + Environment.NewLine + Message = e.Message }); } } - protected async Task LoggedAccessDataOf(string username, bool patients, Func success) + protected async Task LoggedAccessDataOf(string username, bool patients, Func success) { return await LoggedAction(() => { diff --git a/SeniorAssistant/Data/SeniorDataContext.cs b/SeniorAssistant/Data/SeniorDataContext.cs index 389283a..5128ded 100644 --- a/SeniorAssistant/Data/SeniorDataContext.cs +++ b/SeniorAssistant/Data/SeniorDataContext.cs @@ -23,6 +23,7 @@ namespace SeniorAssistant.Data public ITable Patients => GetTable(); public ITable Notifications => GetTable(); public ITable Messages => GetTable(); + public ITable Forgot => GetTable(); public T[] GetLastMessages(ITable table, string receiver, ref int numNotSeen, int max = 10) where T : IHasMessage diff --git a/SeniorAssistant/Models/Forgot.cs b/SeniorAssistant/Models/Forgot.cs new file mode 100644 index 0000000..b31a834 --- /dev/null +++ b/SeniorAssistant/Models/Forgot.cs @@ -0,0 +1,16 @@ +using LinqToDB.Mapping; + +namespace SeniorAssistant.Models +{ + public class Forgot : IHasUsername + { + [Column(IsPrimaryKey = true, CanBeNull = false)] + public string Username { get; set; } + + [Column(CanBeNull = false)] + public string Question { get; set; } + + [Column(CanBeNull = false)] + public string Answer { get; set; } + } +} diff --git a/SeniorAssistant/Startup.cs b/SeniorAssistant/Startup.cs index fece8f5..8e712ae 100644 --- a/SeniorAssistant/Startup.cs +++ b/SeniorAssistant/Startup.cs @@ -133,6 +133,7 @@ namespace SeniorAssistant db.CreateTableIfNotExists(); db.CreateTableIfNotExists(); db.CreateTableIfNotExists(); + db.CreateTableIfNotExists(); } } @@ -175,8 +176,17 @@ namespace SeniorAssistant db.InsertOrReplace(patient); } + var forgot = new Forgot() + { + Question = "Quale animale ti piace di piu'?", + Answer = "Rayquaza" + }; foreach (var user in users) + { + forgot.Username = user.Username; + db.InsertOrReplace(forgot); db.InsertOrReplace(user); + } DateTime now = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); now = now.AddHours(DateTime.Now.Hour).AddMinutes(30); diff --git a/SeniorAssistant/Views/Home/Forgot.cshtml b/SeniorAssistant/Views/Home/Forgot.cshtml new file mode 100644 index 0000000..3dc282f --- /dev/null +++ b/SeniorAssistant/Views/Home/Forgot.cshtml @@ -0,0 +1,29 @@ +@model Forgot + +

Se indovini la risposta allora verrai loggato. Li poi potrai modificare la password.

+

@Model.Question

+ +

+ + + diff --git a/SeniorAssistant/Views/Home/Login.cshtml b/SeniorAssistant/Views/Home/Login.cshtml new file mode 100644 index 0000000..81a47c9 --- /dev/null +++ b/SeniorAssistant/Views/Home/Login.cshtml @@ -0,0 +1,56 @@ +@model string + +
+ @if (Model != null) + { +

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

+ } +
    +
  • + + +
    + +
    + +
  • +
+ + + + @Html.ActionLink("Oppure registrati", "Register") +
+ + diff --git a/SeniorAssistant/Views/Shared/Profile.cshtml b/SeniorAssistant/Views/Home/Profile.cshtml similarity index 100% rename from SeniorAssistant/Views/Shared/Profile.cshtml rename to SeniorAssistant/Views/Home/Profile.cshtml diff --git a/SeniorAssistant/Views/Shared/Profile.cshtml.cs b/SeniorAssistant/Views/Home/Profile.cshtml.cs similarity index 100% rename from SeniorAssistant/Views/Shared/Profile.cshtml.cs rename to SeniorAssistant/Views/Home/Profile.cshtml.cs diff --git a/SeniorAssistant/Views/Shared/Register.cshtml b/SeniorAssistant/Views/Home/Register.cshtml similarity index 80% rename from SeniorAssistant/Views/Shared/Register.cshtml rename to SeniorAssistant/Views/Home/Register.cshtml index 069e3f7..c5e6548 100644 --- a/SeniorAssistant/Views/Shared/Register.cshtml +++ b/SeniorAssistant/Views/Home/Register.cshtml @@ -6,6 +6,8 @@ + +
@@ -13,6 +15,8 @@ +@Html.ActionLink("Oppure fai il login", "Login") + diff --git a/SeniorAssistant/Views/Shared/SidebarMenu.cshtml b/SeniorAssistant/Views/Shared/SidebarMenu.cshtml index 94ac780..f25b387 100644 --- a/SeniorAssistant/Views/Shared/SidebarMenu.cshtml +++ b/SeniorAssistant/Views/Shared/SidebarMenu.cshtml @@ -5,10 +5,12 @@ var session = HttpContextAccessor.HttpContext.Session; string search = HttpContextAccessor.HttpContext.Request.Query["q"]; string username = session.GetString("username"); + if (username != null) { var isDoc = session.GetString("role").Equals("doctor"); + var MaxPatients = 30; var Menu = new List(); Menu.Add(new MenuItem("Profilo", "/")); Menu.Add(new MenuItem("Dati personali", "/user/" + username)); @@ -19,7 +21,18 @@ where p.Doctor.Equals(username) join u in db.Users on p.Username equals u.Username select new { Username = p.Username, Name = u.Name + " " + u.LastName }).ToArray(); - var sub = new SubMenu() { Text = "Pazienti", Items = new List() }; + + if(search != null) + { + patients = (from p in patients + where p.Name.StartsWith(search) + select p).ToArray(); + } + + patients.Take(MaxPatients); + + var num = patients.Count(); + var sub = new SubMenu() { Text = num + " pazienti link rapido", Items = new List() }; foreach (var p in patients) { sub.Items.Add(new MenuItem(p.Name, "/user/" + p.Username)); @@ -32,7 +45,10 @@ var patient = (from p in db.Patients where p.Username.Equals(username) select p).FirstOrDefault(); - Menu.Add(new MenuItem("Invia un messaggio al dottore", "/Message/" + patient.Doctor)); + if (patient != null) + { + Menu.Add(new MenuItem("Invia un messaggio al dottore", "/Message/" + patient.Doctor)); + } }