From 9b31c9334185edc10104e89849de09e556ebf727 Mon Sep 17 00:00:00 2001 From: Giacomo Bertolazzi <20015159@studenti.uniupo.it> Date: Mon, 21 Jan 2019 17:47:42 +0100 Subject: [PATCH] + Avatar + Range Data Scheme --- .../Controllers/AccountController.cs | 104 +++++++++++++----- SeniorAssistant/SeniorAssistant.csproj | 4 - SeniorAssistant/Views/Home/User.cshtml | 30 ++++- SeniorAssistant/Views/Shared/Login.cshtml | 7 +- SeniorAssistant/Views/Shared/Logout.cshtml | 10 +- SeniorAssistant/senior.db | Bin 114688 -> 180224 bytes SeniorAssistant/wwwroot/uploads/default.jpg | Bin 0 -> 10716 bytes SeniorAssistant/wwwroot/uploads/vecchio0.jpg | Bin 0 -> 418141 bytes 8 files changed, 112 insertions(+), 43 deletions(-) create mode 100644 SeniorAssistant/wwwroot/uploads/default.jpg create mode 100644 SeniorAssistant/wwwroot/uploads/vecchio0.jpg diff --git a/SeniorAssistant/Controllers/AccountController.cs b/SeniorAssistant/Controllers/AccountController.cs index 37e7d1f..3f5ac16 100644 --- a/SeniorAssistant/Controllers/AccountController.cs +++ b/SeniorAssistant/Controllers/AccountController.cs @@ -6,10 +6,10 @@ using LinqToDB; using System.Linq; using System; using SeniorAssistant.Models.Users; -using SeniorAssistant.Data; using System.Threading.Tasks; -using System.Web; using System.IO; +using System.Collections.Generic; +using System.Net.Http.Headers; namespace IdentityDemo.Controllers { @@ -25,35 +25,48 @@ namespace IdentityDemo.Controllers 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: "; + private static readonly string DefaultImage = "/uploads/default.jpg"; + private static readonly string UploadsDirec = "/uploads/"; [HttpPost] public async Task _login(string username, string password) { - var result = await (from u in Db.Users - where u.Username.Equals(username) - && u.Password.Equals(password) - select u).ToListAsync(); - - if (result.Count == 1) + try { - 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 user = await (from u in Db.Users + where u.Username.Equals(username) + && u.Password.Equals(password) + select u).FirstOrDefaultAsync(); - return Json(OkJson); + if (user != null) + { + HttpContext.Session.SetString(Username, username); + HttpContext.Session.SetString("email", user.Email); + HttpContext.Session.SetString("name", user.Name); + HttpContext.Session.SetString("lastname", user.LastName); + HttpContext.Session.SetString("avatar", user.Avatar ?? DefaultImage); + + var isDoc = (from d in Db.Doctors + where d.Username.Equals(username) + select d).ToArray().FirstOrDefault() != null; + HttpContext.Session.SetString("role", isDoc ? "doctor" : "patient"); + + return Json(OkJson); + } + return Json(new JsonResponse() + { + Success = false, + Message = InvalidLogIn + }); } - return Json(new JsonResponse() + catch (Exception e) { - Success = false, - Message = InvalidLogIn - }); + return Json(new JsonResponse() + { + Success = false, + Message = e.Message + " " +e.Source + "
"+ e.StackTrace + }); + } } [HttpPost] @@ -68,6 +81,7 @@ namespace IdentityDemo.Controllers { try { + user.Avatar = DefaultImage; Db.Insert(user); if (code != null && code.Equals("444442220")) { @@ -218,18 +232,51 @@ namespace IdentityDemo.Controllers return Json(OkJson); }); } - [HttpPost] - public async Task _save(IFormFile file) + public async Task _save(IEnumerable files) { - return LoggedAction(() => + return await LoggedAction(() => { - var loggedUser = HttpContext.Session.GetString(Username); + if (files != null) + { + var loggedUser = HttpContext.Session.GetString(Username); + foreach (var file in files) + { + var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition); + + // We are only interested in the file name. + var fileName = loggedUser + Path.GetExtension(fileContent.FileName.ToString().Trim('"')); + + var physicalPath = "wwwroot" + UploadsDirec; + Directory.CreateDirectory(physicalPath); + + physicalPath = Path.Combine(physicalPath, fileName); + var externalPath = Path.Combine(UploadsDirec, fileName); + + using (var fileStream = new FileStream(physicalPath, FileMode.Create)) + { + file.CopyTo(fileStream); + } + + var user = (from u in Db.Users + where u.Username.Equals(loggedUser) + select u).FirstOrDefault(); + user.Avatar = externalPath; + HttpContext.Session.SetString("avatar", externalPath); + Db.Update(user); + } + } + + return Json(OkJson); + /* + if (file.Length > 0) { + var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition); + var name = loggedUser + ".jpg"; - var path = Path.Combine(("~/uploads/"), name); + var path = Path.Combine(("/uploads/"), name); var stream = new FileStream(path, FileMode.Create); file.CopyTo(stream); var user = (from u in Db.Users @@ -261,6 +308,7 @@ namespace IdentityDemo.Controllers } return Json(new JsonResponse()); */ + }); } } } \ No newline at end of file diff --git a/SeniorAssistant/SeniorAssistant.csproj b/SeniorAssistant/SeniorAssistant.csproj index 413dddb..89297d0 100644 --- a/SeniorAssistant/SeniorAssistant.csproj +++ b/SeniorAssistant/SeniorAssistant.csproj @@ -7,10 +7,6 @@ 7.1 - - - - diff --git a/SeniorAssistant/Views/Home/User.cshtml b/SeniorAssistant/Views/Home/User.cshtml index 5eeac2a..51e5d1b 100644 --- a/SeniorAssistant/Views/Home/User.cshtml +++ b/SeniorAssistant/Views/Home/User.cshtml @@ -6,6 +6,7 @@ ViewBag.Title = "Hello Razor"; var session = HttpContextAccessor.HttpContext.Session; var username = session.GetString("username"); + bool filter = HttpContextAccessor.HttpContext.Request.Query["from"] != (String)null; bool auth = username.Equals(Model.Username); bool isDoc = session.GetString("role").Equals("doctor"); @@ -29,6 +30,14 @@ else
+ + + + + + + +
@if (isDoc && patient != null) @@ -89,9 +98,20 @@ else }); $("#refresh-hours").on("click", function () { var hours = $("#hours-data").val(); - var base_url = "@Url.Content("~/api/")"; var end_url = "/@Model.Username/last/" + hours; + kendoUpdate(end_url); + }); + $("#refresh-date").on("click", function () { + var from = $("#date-from").val(); + var to = $("#date-to").val(); + var end_url = "/@Model.Username/"+from+"/"+to; + kendoUpdate(end_url); + }); + var toRefresh = "@if (filter) { @Html.Raw("#refresh-date") } else { @Html.Raw("#refresh-hours") }"; + $(toRefresh).click(); + + function kendoUpdate(end_url, base_url = "@Url.Content("~/api/")") { $.getJSON(base_url + "heartbeat" + end_url, function (heartbeat) { $.getJSON(base_url + "step" + end_url, function (steps) { $.getJSON(base_url + "sleep" + end_url, function (sleep) { @@ -110,7 +130,9 @@ else && Object.keys(steps).length == 0 && Object.keys(sleep).length == 0) $("#chart-data").html("Nessun dato"); - else + else { + /* se checked #show-table allora crea dati come data.cshtml */ + /**/ $("#chart-data").kendoChart({ title: { text: "Visualizzazione attivita' di @Model.Name @Model.LastName" }, legend: { position: "bottom" }, @@ -175,10 +197,10 @@ else min: 0 }] }); /* Kendo */ + } /* else */ }); /* sleep */ }); /* steps */ }); /* heart */ - }); /* click */ - $("#refresh-hours").click(); + } } \ No newline at end of file diff --git a/SeniorAssistant/Views/Shared/Login.cshtml b/SeniorAssistant/Views/Shared/Login.cshtml index ec8c4d0..c23a2fc 100644 --- a/SeniorAssistant/Views/Shared/Login.cshtml +++ b/SeniorAssistant/Views/Shared/Login.cshtml @@ -11,11 +11,11 @@