Refactoring

* refactoring
* fixes
* messages
* new interface
This commit is contained in:
2019-01-18 23:22:03 +01:00
parent 1246116804
commit b7460cfd78
34 changed files with 746 additions and 665 deletions

View File

@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using SeniorAssistant.Models;
using SeniorAssistant.Models.Data;
using SeniorAssistant.Models.Users;
namespace SeniorAssistant.Controllers.Services

View File

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

View File

@@ -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);
});
}
}
}

View File

@@ -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]