Refactoring api
This commit is contained in:
@@ -13,7 +13,7 @@ namespace IdentityDemo.Controllers
|
|||||||
[Route("[controller]/[action]")]
|
[Route("[controller]/[action]")]
|
||||||
public class AccountController : BaseController
|
public class AccountController : BaseController
|
||||||
{
|
{
|
||||||
private readonly JsonResponse OkJson = new JsonResponse();
|
private static readonly string NoteModified = "Il tuo dottore ha modificato la nota per te";
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult _login(string username, string password)
|
public ActionResult _login(string username, string password)
|
||||||
@@ -35,8 +35,7 @@ namespace IdentityDemo.Controllers
|
|||||||
HttpContext.Session.SetString(Username, username);
|
HttpContext.Session.SetString(Username, username);
|
||||||
HttpContext.Session.SetString("email", user.Email);
|
HttpContext.Session.SetString("email", user.Email);
|
||||||
HttpContext.Session.SetString("name", user.Name);
|
HttpContext.Session.SetString("name", user.Name);
|
||||||
//HttpContext.Session.SetString("lastname", user.LastName);
|
HttpContext.Session.SetString("lastname", user.LastName);
|
||||||
|
|
||||||
|
|
||||||
var isDoc = (from d in Db.Doctors
|
var isDoc = (from d in Db.Doctors
|
||||||
where d.Username.Equals(username)
|
where d.Username.Equals(username)
|
||||||
@@ -168,5 +167,19 @@ namespace IdentityDemo.Controllers
|
|||||||
return Json(new JsonResponse());
|
return Json(new JsonResponse());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPut]
|
||||||
|
public ActionResult _addNote(string patient, string text)
|
||||||
|
{
|
||||||
|
return LoggedAccessDataOf(patient, () =>
|
||||||
|
{
|
||||||
|
var pat = Db.Patients.Where((p) => p.Username.Equals(patient)).FirstOrDefault();
|
||||||
|
pat.Notes = text;
|
||||||
|
Db.Update(pat);
|
||||||
|
_notification(patient, NoteModified);
|
||||||
|
|
||||||
|
return Json(OkJson);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,6 +12,7 @@ namespace SeniorAssistant.Controllers
|
|||||||
protected static readonly string InvalidModel = "Modello non valido";
|
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 a vedere questi dati";
|
||||||
protected static readonly string Username = "username";
|
protected static readonly string Username = "username";
|
||||||
|
protected readonly JsonResponse OkJson = new JsonResponse();
|
||||||
|
|
||||||
IDataContextFactory<SeniorDataContext> dbFactory;
|
IDataContextFactory<SeniorDataContext> dbFactory;
|
||||||
SeniorDataContext db;
|
SeniorDataContext db;
|
||||||
@@ -59,16 +60,16 @@ namespace SeniorAssistant.Controllers
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ActionResult LoggedAccessDataOf(string username, Func<ActionResult> success)
|
protected ActionResult LoggedAccessDataOf(string username, Func<ActionResult> success, bool patients = true)
|
||||||
{
|
{
|
||||||
return LoggedAction(() =>
|
return LoggedAction(() =>
|
||||||
{
|
{
|
||||||
var loggedUser = HttpContext.Session.GetString(Username);
|
var loggedUser = HttpContext.Session.GetString(Username);
|
||||||
var condition = username.Equals(loggedUser);
|
var condition = username.Equals(loggedUser);
|
||||||
|
|
||||||
condition = condition || (from patient in Db.Patients
|
condition = condition || (patients && (from patient in Db.Patients
|
||||||
where patient.Doctor.Equals(loggedUser) && patient.Username.Equals(username)
|
where patient.Doctor.Equals(loggedUser) && patient.Username.Equals(username)
|
||||||
select patient).ToArray().FirstOrDefault() != null;
|
select patient).ToArray().FirstOrDefault() != null);
|
||||||
|
|
||||||
return condition ?
|
return condition ?
|
||||||
success.Invoke() :
|
success.Invoke() :
|
||||||
|
|||||||
@@ -10,24 +10,24 @@ namespace SeniorAssistant.Controllers.Services
|
|||||||
public abstract class CrudController<TEntity> : BaseController
|
public abstract class CrudController<TEntity> : BaseController
|
||||||
where TEntity : class, IHasUsername
|
where TEntity : class, IHasUsername
|
||||||
{
|
{
|
||||||
[HttpGet]
|
|
||||||
public async Task<IEnumerable<TEntity>> Read() => await Db.GetTable<TEntity>().ToListAsync();
|
|
||||||
|
|
||||||
[HttpGet("{username}")]
|
[HttpGet("{username}")]
|
||||||
public async Task<TEntity> Read(string username) => await Db.GetTable<TEntity>().FirstOrDefaultAsync(c => c.Username.Equals(username));
|
public async Task<IActionResult> Read(string username)
|
||||||
|
{
|
||||||
[HttpPost]
|
return LoggedAccessDataOf(username, () =>
|
||||||
public async Task Create([FromBody]TEntity item) => await Db.InsertAsync(item);
|
{
|
||||||
|
return Json(Db.GetTable<TEntity>().Where((u) => u.Username.Equals(username)).ToArray());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPut("{username}")]
|
[HttpPut("{username}")]
|
||||||
public async Task Update(string username, [FromBody]TEntity item)
|
public async Task<IActionResult> Update(string username, [FromBody] TEntity entity)
|
||||||
{
|
{
|
||||||
item.Username = username;
|
return LoggedAccessDataOf(username, () =>
|
||||||
|
{
|
||||||
await Db.UpdateAsync(item);
|
entity.Username = username;
|
||||||
}
|
Db.Update(entity);
|
||||||
|
return Json(OkJson);
|
||||||
[HttpDelete("{username}")]
|
}, false);
|
||||||
public async Task Delete(string username) => await Db.GetTable<TEntity>().Where(c => c.Username.Equals(username)).DeleteAsync();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,74 +2,86 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using SeniorAssistant.Models;
|
using SeniorAssistant.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace SeniorAssistant.Controllers.Services
|
namespace SeniorAssistant.Controllers.Services
|
||||||
{
|
{
|
||||||
public class CrudTimeController<TEntity> : BaseController
|
public class CrudTimeController<TEntity> : CrudController<TEntity>
|
||||||
where TEntity : class, IHasTime
|
where TEntity : class, IHasTime
|
||||||
{
|
{
|
||||||
static readonly object Empty = new { };
|
private static readonly string DateNotCorrect = "Il formato della data non e' corretto";
|
||||||
|
|
||||||
[HttpGet]
|
|
||||||
public async Task<IEnumerable<TEntity>> Read() => await Db.GetTable<TEntity>().ToListAsync();
|
|
||||||
|
|
||||||
[HttpGet("{username}")]
|
|
||||||
public async Task<IEnumerable<TEntity>> Read(string username) => await Db.GetTable<TEntity>().Where(e => e.Username.Equals(username)).ToListAsync();
|
|
||||||
|
|
||||||
[HttpGet("{username}/{date:regex((today|\\d{{4}}-\\d{{2}}-\\d{{2}}))}/{hour:range(0, 23)?}")]
|
[HttpGet("{username}/{date:regex((today|\\d{{4}}-\\d{{2}}-\\d{{2}}))}/{hour:range(0, 23)?}")]
|
||||||
public async Task<IEnumerable<TEntity>> Read(string username, string date, int hour = -1) => await Read(username, date, date, hour);
|
public async Task<IActionResult> Read(string username, string date, int hour = -1) => await Read(username, date, date, hour);
|
||||||
|
|
||||||
[HttpGet("{username}/{from:regex((today|\\d{{4}}-\\d{{2}}-\\d{{2}}))}/{to:regex((today|\\d{{4}}-\\d{{2}}-\\d{{2}}))}/{hour:range(0, 23)?}")]
|
[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<IEnumerable<TEntity>> Read(string username, string from, string to, int hour = -1)
|
public async Task<IActionResult> Read(string username, string from, string to, int hour = -1)
|
||||||
|
{
|
||||||
|
return LoggedAccessDataOf(username, () =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
DateTime dateFrom = (from.Equals("today") ? DateTime.Now : DateTime.ParseExact(from, "yyyy-MM-dd", null));
|
DateTime dateFrom = (from.Equals("today") ? DateTime.Now : DateTime.ParseExact(from, "yyyy-MM-dd", null));
|
||||||
DateTime dateTo = (to.Equals("today") ? DateTime.Now : DateTime.ParseExact(to, "yyyy-MM-dd", null));
|
DateTime dateTo = (to.Equals("today") ? DateTime.Now : DateTime.ParseExact(to, "yyyy-MM-dd", null));
|
||||||
|
|
||||||
return await Db.GetTable<TEntity>().Where(e => e.Username.Equals(username) && dateFrom.Date<=e.Time.Date && dateTo.Date>=e.Time.Date && (hour < 0 || e.Time.Hour == hour)).ToListAsync();
|
return Json((from entity in Db.GetTable<TEntity>()
|
||||||
|
where entity.Username.Equals(username)
|
||||||
|
&& dateFrom.Date <= entity.Time.Date
|
||||||
|
&& dateTo.Date >= entity.Time.Date
|
||||||
|
&& (hour < 0 || entity.Time.Hour == hour)
|
||||||
|
select entity).ToArray());
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
return new List<TEntity>();
|
return Json(new JsonResponse(false, DateNotCorrect));
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{username}/last/{hour:min(1)}")]
|
[HttpGet("{username}/last/{hour:min(1)}")]
|
||||||
public async Task<IEnumerable<TEntity>> Read(string username, int hour)
|
public async Task<IActionResult> Read(string username, int hour)
|
||||||
|
{
|
||||||
|
return LoggedAccessDataOf(username, () =>
|
||||||
{
|
{
|
||||||
DateTime date = DateTime.Now.AddHours(-hour);
|
DateTime date = DateTime.Now.AddHours(-hour);
|
||||||
return await Db.GetTable<TEntity>().Where(e => e.Username.Equals(username) && date <= e.Time).ToListAsync();
|
return Json((from entity in Db.GetTable<TEntity>()
|
||||||
|
where entity.Username.Equals(username)
|
||||||
|
&& date <= entity.Time
|
||||||
|
select entity).ToArray());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
[NonAction]
|
|
||||||
public async Task<TEntity> Read(string username, DateTime date) => await Db.GetTable<TEntity>().FirstOrDefaultAsync(e => e.Username.Equals(username) && date == e.Time);
|
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task Create([FromBody]TEntity item) => await Db.InsertAsync(item);
|
public async Task<IActionResult> Create([FromBody]TEntity item)
|
||||||
|
{
|
||||||
|
return Action(() =>
|
||||||
|
{
|
||||||
|
Db.Insert(item);
|
||||||
|
return Json(OkJson);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPut]
|
[HttpPut]
|
||||||
public async Task<object> Update([FromBody]TEntity item)
|
public async Task<IActionResult> Update([FromBody]TEntity item)
|
||||||
{
|
{
|
||||||
var e = await Read(item.Username, item.Time);
|
return LoggedAccessDataOf(item.Username, () =>
|
||||||
|
{
|
||||||
|
var e = Read(item.Username, item.Time);
|
||||||
if (e == null)
|
if (e == null)
|
||||||
{
|
{
|
||||||
await Create(item);
|
Create(item);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await Db.UpdateAsync(item);
|
Db.UpdateAsync(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Empty;
|
return Json(OkJson);
|
||||||
|
}, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
[NonAction]
|
||||||
[HttpDelete("{username}")]
|
private TEntity Read(string username, DateTime date) => Db.GetTable<TEntity>().FirstOrDefault(e => e.Username.Equals(username) && date == e.Time);
|
||||||
public async Task Delete(string username) => await Db.GetTable<TEntity>().Where(c => c.Username.Equals(username)).DeleteAsync();
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,13 +8,15 @@
|
|||||||
var username = session.GetString("username");
|
var username = session.GetString("username");
|
||||||
|
|
||||||
bool auth = username.Equals(Model);
|
bool auth = username.Equals(Model);
|
||||||
if (session.GetString("role").Equals("doctor"))
|
bool isDoc = session.GetString("role").Equals("doctor");
|
||||||
|
Patient patient = null;
|
||||||
|
if (isDoc)
|
||||||
{
|
{
|
||||||
var db = dbFactory.Create();
|
var db = dbFactory.Create();
|
||||||
var isDocPatient = (from p in db.Patients
|
patient = (from p in db.Patients
|
||||||
where p.Username.Equals(Model) && p.Doctor.Equals(username)
|
where p.Username.Equals(Model) && p.Doctor.Equals(username)
|
||||||
select p).ToArray().FirstOrDefault() != null;
|
select p).ToArray().FirstOrDefault();
|
||||||
auth = auth || isDocPatient;
|
auth = auth || patient != null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,9 +27,38 @@
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Aggiungere un qualcosa per scegliere le ore da vedere (Max 48?)
|
// Aggiungere un qualcosa per scegliere le ore da vedere (Max 48?)
|
||||||
|
<div>
|
||||||
<input id="hours-data" type="text" placeholder="hours" value="24" />
|
<input id="hours-data" type="text" placeholder="hours" value="24" />
|
||||||
<button id="refresh-hours" class="fc-button">Cambia ora</button>
|
<button id="refresh-hours" class="fc-button">Cambia ora</button>
|
||||||
<div id="chart-data"></div>
|
<div id="chart-data"></div>
|
||||||
|
</div>
|
||||||
|
@if(isDoc && patient != null)
|
||||||
|
{
|
||||||
|
<div>
|
||||||
|
<p>NOTEEEEEEEEEEEE: l'alunno dorme durante la lezione</p>
|
||||||
|
<textarea id="note-area" placeholder="Scrivi una nota..">@patient.Notes</textarea>
|
||||||
|
<button id="send-note" class="btn">Salva</button>
|
||||||
|
<p id="note-error"></p>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
$("#send-note").on("click", function () {
|
||||||
|
var text = $("#note-area").val().trim();
|
||||||
|
$.ajax({
|
||||||
|
url: "/Account/_addNote",
|
||||||
|
type: "PUT",
|
||||||
|
data: {
|
||||||
|
Patient: "@Model", Text: text
|
||||||
|
},
|
||||||
|
success: function (data) {
|
||||||
|
$("#note-error").html(data.success?"Nota salvata":data.message);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
$("#hours-data").on("change keyup paste click", function () {
|
$("#hours-data").on("change keyup paste click", function () {
|
||||||
var t = $(this);
|
var t = $(this);
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
@model string
|
@model string
|
||||||
|
@inject IHttpContextAccessor HttpContextAccessor
|
||||||
|
|
||||||
|
@{
|
||||||
|
var session = HttpContextAccessor.HttpContext.Session;
|
||||||
|
}
|
||||||
|
|
||||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
|
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
|
||||||
<!-- The user image in the navbar-->
|
<!-- The user image in the navbar-->
|
||||||
<img src="~/AdminLTE-2.4.3/dist/img/user2-160x160.jpg" class="user-image" alt="User Image">
|
<img src="~/AdminLTE-2.4.3/dist/img/user2-160x160.jpg" class="user-image" alt="User Image">
|
||||||
@@ -10,29 +16,14 @@
|
|||||||
<li class="user-header">
|
<li class="user-header">
|
||||||
<img src="~/AdminLTE-2.4.3/dist/img/user2-160x160.jpg" class="img-circle" alt="User Image">
|
<img src="~/AdminLTE-2.4.3/dist/img/user2-160x160.jpg" class="img-circle" alt="User Image">
|
||||||
<p>
|
<p>
|
||||||
Alexander Pierce - Web Developer
|
@session.GetString("name") @session.GetString("lastname") - @session.GetString("role")
|
||||||
<small>Member since Nov. 2012</small>
|
<small>@session.GetString("email")</small>
|
||||||
</p>
|
</p>
|
||||||
</li>
|
</li>
|
||||||
<!-- Menu Body -->
|
|
||||||
<li class="user-body">
|
|
||||||
<div class="row">
|
|
||||||
<div class="col-xs-4 text-center">
|
|
||||||
<a href="#">Followers</a>
|
|
||||||
</div>
|
|
||||||
<div class="col-xs-4 text-center">
|
|
||||||
<a href="#">Sales</a>
|
|
||||||
</div>
|
|
||||||
<div class="col-xs-4 text-center">
|
|
||||||
<a href="#">Friends</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<!-- /.row -->
|
|
||||||
</li>
|
|
||||||
<!-- Menu Footer-->
|
<!-- Menu Footer-->
|
||||||
<li class="user-footer">
|
<li class="user-footer">
|
||||||
<div class="pull-left">
|
<div class="pull-left">
|
||||||
<a href="#" class="btn btn-default btn-flat">Profile</a>
|
<a href="/" class="btn btn-default btn-flat">Profile</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="pull-right">
|
<div class="pull-right">
|
||||||
<a href="#" id="logout-btn" class="btn btn-default btn-flat">Logout</a>
|
<a href="#" id="logout-btn" class="btn btn-default btn-flat">Logout</a>
|
||||||
|
|||||||
@@ -27,13 +27,13 @@
|
|||||||
where d.Username.Equals(patientData.Doctor)
|
where d.Username.Equals(patientData.Doctor)
|
||||||
select new { u.Username, u.Name, u.LastName, d.Location }).ToArray().First();
|
select new { u.Username, u.Name, u.LastName, d.Location }).ToArray().First();
|
||||||
|
|
||||||
<p class="text-bold">@doctor.Name @doctor.LastName</p>
|
<p class="text-bold">Dottore: @doctor.Name @doctor.LastName</p>
|
||||||
<p class="text-fuchsia">@doctor.Location</p>
|
<p class="text-fuchsia">Dove mi puoi trovare? @doctor.Location</p>
|
||||||
<textarea class="progress-text" placeholder="Nessuna nuova nota" readonly>@patientData.Notes</textarea>
|
<textarea class="progress-text" placeholder="Nessuna nuova nota" readonly>@patientData.Notes</textarea>
|
||||||
|
|
||||||
<div id="send-doc-message">
|
<div id="send-doc-message">
|
||||||
<p>Invia un messaggio al tuo dottore</p>
|
<p>Invia un messaggio al tuo dottore</p>
|
||||||
<textarea id="doc-message" class="progress-text" placeholder="scrivi qui">@patientData.Notes</textarea>
|
<textarea id="doc-message" class="progress-text" placeholder="scrivi qui"></textarea>
|
||||||
<button id="btn-send-message">Invia</button>
|
<button id="btn-send-message">Invia</button>
|
||||||
<p id="message-error" class="text-red"></p>
|
<p id="message-error" class="text-red"></p>
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
@using SeniorAssistant.Models;
|
@using SeniorAssistant.Models;
|
||||||
|
@using SeniorAssistant.Models.Users;
|
||||||
@using SeniorAssistant.Data;
|
@using SeniorAssistant.Data;
|
||||||
@using Microsoft.AspNetCore.Mvc;
|
@using Microsoft.AspNetCore.Mvc;
|
||||||
@using Microsoft.AspNetCore.Http;
|
@using Microsoft.AspNetCore.Http;
|
||||||
Binary file not shown.
Reference in New Issue
Block a user