Refactoring
* refactoring * fixes * messages * new interface
This commit is contained in:
@@ -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<ActionResult> _login(string username, string password)
|
||||
{
|
||||
JsonResponse response = new JsonResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = "Username or password is invalid."
|
||||
};
|
||||
|
||||
var result = Db.GetTable<User>().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<ActionResult> _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<ActionResult> _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<ActionResult> _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<ActionResult> _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<ActionResult> _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<ActionResult> _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<ActionResult> _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<ActionResult> _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);
|
||||
});
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SeniorAssistant.Models;
|
||||
using SeniorAssistant.Models.Data;
|
||||
using SeniorAssistant.Models.Users;
|
||||
|
||||
namespace SeniorAssistant.Controllers.Services
|
||||
|
||||
@@ -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<SeniorDataContext> dbFactory;
|
||||
SeniorDataContext db;
|
||||
private IDataContextFactory<SeniorDataContext> dbFactory;
|
||||
private SeniorDataContext db;
|
||||
|
||||
protected T TryResolve<T>() => (T)HttpContext.RequestServices.GetService(typeof(T));
|
||||
|
||||
@@ -34,42 +37,45 @@ namespace SeniorAssistant.Controllers
|
||||
{
|
||||
return HttpContext.Session.GetString(Username) != null;
|
||||
}
|
||||
|
||||
protected ActionResult Action(Func<ActionResult> success)
|
||||
|
||||
protected async Task<ActionResult> LoggedAction(Func<ActionResult> 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<ActionResult> success)
|
||||
protected async Task<ActionResult> LoggedAccessDataOf(string username, bool patients, Func<ActionResult> success)
|
||||
{
|
||||
return Action(() =>
|
||||
return await LoggedAction(() =>
|
||||
{
|
||||
return IsLogged() ?
|
||||
success.Invoke() :
|
||||
Json(new JsonResponse()
|
||||
{
|
||||
Success = false,
|
||||
Message = MustBeLogged
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
protected ActionResult LoggedAccessDataOf(string username, Func<ActionResult> 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<JsonResponse> 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<JsonResponse> 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
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace SeniorAssistant.Controllers.Services
|
||||
[HttpGet("{username}")]
|
||||
public async Task<IActionResult> Read(string username)
|
||||
{
|
||||
return LoggedAccessDataOf(username, () =>
|
||||
return await LoggedAccessDataOf(username, true, () =>
|
||||
{
|
||||
return Json(Db.GetTable<TEntity>().Where((u) => u.Username.Equals(username)).ToArray());
|
||||
});
|
||||
@@ -22,12 +22,12 @@ namespace SeniorAssistant.Controllers.Services
|
||||
[HttpPut("{username}")]
|
||||
public async Task<IActionResult> Update(string username, [FromBody] TEntity entity)
|
||||
{
|
||||
return LoggedAccessDataOf(username, () =>
|
||||
return await LoggedAccessDataOf(username, false, () =>
|
||||
{
|
||||
entity.Username = username;
|
||||
Db.Update(entity);
|
||||
return Json(OkJson);
|
||||
}, false);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<IActionResult> 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<IActionResult> 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<IActionResult> 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<TEntity>()
|
||||
@@ -53,24 +56,42 @@ namespace SeniorAssistant.Controllers.Services
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create([FromBody]TEntity item)
|
||||
public async Task<IActionResult> 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<IActionResult> Update([FromBody]TEntity item)
|
||||
public async Task<IActionResult> 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]
|
||||
|
||||
Reference in New Issue
Block a user