Refactoring
* refactoring * fixes * messages * new interface
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user