Refactoring api
This commit is contained in:
@@ -13,7 +13,7 @@ namespace IdentityDemo.Controllers
|
||||
[Route("[controller]/[action]")]
|
||||
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]
|
||||
public ActionResult _login(string username, string password)
|
||||
@@ -35,8 +35,7 @@ namespace IdentityDemo.Controllers
|
||||
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("lastname", user.LastName);
|
||||
|
||||
var isDoc = (from d in Db.Doctors
|
||||
where d.Username.Equals(username)
|
||||
@@ -168,5 +167,19 @@ namespace IdentityDemo.Controllers
|
||||
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 NoAuthorized = "Non sei autorizzato a vedere questi dati";
|
||||
protected static readonly string Username = "username";
|
||||
protected readonly JsonResponse OkJson = new JsonResponse();
|
||||
|
||||
IDataContextFactory<SeniorDataContext> dbFactory;
|
||||
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(() =>
|
||||
{
|
||||
var loggedUser = HttpContext.Session.GetString(Username);
|
||||
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)
|
||||
select patient).ToArray().FirstOrDefault() != null;
|
||||
select patient).ToArray().FirstOrDefault() != null);
|
||||
|
||||
return condition ?
|
||||
success.Invoke() :
|
||||
|
||||
@@ -10,24 +10,24 @@ namespace SeniorAssistant.Controllers.Services
|
||||
public abstract class CrudController<TEntity> : BaseController
|
||||
where TEntity : class, IHasUsername
|
||||
{
|
||||
[HttpGet]
|
||||
public async Task<IEnumerable<TEntity>> Read() => await Db.GetTable<TEntity>().ToListAsync();
|
||||
|
||||
[HttpGet("{username}")]
|
||||
public async Task<TEntity> Read(string username) => await Db.GetTable<TEntity>().FirstOrDefaultAsync(c => c.Username.Equals(username));
|
||||
|
||||
[HttpPost]
|
||||
public async Task Create([FromBody]TEntity item) => await Db.InsertAsync(item);
|
||||
public async Task<IActionResult> Read(string username)
|
||||
{
|
||||
return LoggedAccessDataOf(username, () =>
|
||||
{
|
||||
return Json(Db.GetTable<TEntity>().Where((u) => u.Username.Equals(username)).ToArray());
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPut("{username}")]
|
||||
public async Task Update(string username, [FromBody]TEntity item)
|
||||
public async Task<IActionResult> Update(string username, [FromBody] TEntity entity)
|
||||
{
|
||||
item.Username = username;
|
||||
|
||||
await Db.UpdateAsync(item);
|
||||
}
|
||||
|
||||
[HttpDelete("{username}")]
|
||||
public async Task Delete(string username) => await Db.GetTable<TEntity>().Where(c => c.Username.Equals(username)).DeleteAsync();
|
||||
return LoggedAccessDataOf(username, () =>
|
||||
{
|
||||
entity.Username = username;
|
||||
Db.Update(entity);
|
||||
return Json(OkJson);
|
||||
}, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,74 +2,86 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SeniorAssistant.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SeniorAssistant.Controllers.Services
|
||||
{
|
||||
public class CrudTimeController<TEntity> : BaseController
|
||||
public class CrudTimeController<TEntity> : CrudController<TEntity>
|
||||
where TEntity : class, IHasTime
|
||||
{
|
||||
static readonly object Empty = new { };
|
||||
|
||||
[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();
|
||||
private static readonly string DateNotCorrect = "Il formato della data non e' corretto";
|
||||
|
||||
[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)?}")]
|
||||
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
|
||||
{
|
||||
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));
|
||||
|
||||
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
|
||||
{
|
||||
return new List<TEntity>();
|
||||
return Json(new JsonResponse(false, DateNotCorrect));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[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);
|
||||
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]
|
||||
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]
|
||||
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)
|
||||
{
|
||||
await Create(item);
|
||||
Create(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
await Db.UpdateAsync(item);
|
||||
Db.UpdateAsync(item);
|
||||
}
|
||||
|
||||
return Empty;
|
||||
return Json(OkJson);
|
||||
}, false);
|
||||
}
|
||||
|
||||
/*
|
||||
[HttpDelete("{username}")]
|
||||
public async Task Delete(string username) => await Db.GetTable<TEntity>().Where(c => c.Username.Equals(username)).DeleteAsync();
|
||||
*/
|
||||
[NonAction]
|
||||
private TEntity Read(string username, DateTime date) => Db.GetTable<TEntity>().FirstOrDefault(e => e.Username.Equals(username) && date == e.Time);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,13 +8,15 @@
|
||||
var username = session.GetString("username");
|
||||
|
||||
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 isDocPatient = (from p in db.Patients
|
||||
patient = (from p in db.Patients
|
||||
where p.Username.Equals(Model) && p.Doctor.Equals(username)
|
||||
select p).ToArray().FirstOrDefault() != null;
|
||||
auth = auth || isDocPatient;
|
||||
select p).ToArray().FirstOrDefault();
|
||||
auth = auth || patient != null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,9 +27,38 @@
|
||||
else
|
||||
{
|
||||
// Aggiungere un qualcosa per scegliere le ore da vedere (Max 48?)
|
||||
<div>
|
||||
<input id="hours-data" type="text" placeholder="hours" value="24" />
|
||||
<button id="refresh-hours" class="fc-button">Cambia ora</button>
|
||||
<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>
|
||||
$("#hours-data").on("change keyup paste click", function () {
|
||||
var t = $(this);
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
@model string
|
||||
@inject IHttpContextAccessor HttpContextAccessor
|
||||
|
||||
@{
|
||||
var session = HttpContextAccessor.HttpContext.Session;
|
||||
}
|
||||
|
||||
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
|
||||
<!-- The user image in the navbar-->
|
||||
<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">
|
||||
<img src="~/AdminLTE-2.4.3/dist/img/user2-160x160.jpg" class="img-circle" alt="User Image">
|
||||
<p>
|
||||
Alexander Pierce - Web Developer
|
||||
<small>Member since Nov. 2012</small>
|
||||
@session.GetString("name") @session.GetString("lastname") - @session.GetString("role")
|
||||
<small>@session.GetString("email")</small>
|
||||
</p>
|
||||
</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-->
|
||||
<li class="user-footer">
|
||||
<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 class="pull-right">
|
||||
<a href="#" id="logout-btn" class="btn btn-default btn-flat">Logout</a>
|
||||
|
||||
@@ -27,13 +27,13 @@
|
||||
where d.Username.Equals(patientData.Doctor)
|
||||
select new { u.Username, u.Name, u.LastName, d.Location }).ToArray().First();
|
||||
|
||||
<p class="text-bold">@doctor.Name @doctor.LastName</p>
|
||||
<p class="text-fuchsia">@doctor.Location</p>
|
||||
<p class="text-bold">Dottore: @doctor.Name @doctor.LastName</p>
|
||||
<p class="text-fuchsia">Dove mi puoi trovare? @doctor.Location</p>
|
||||
<textarea class="progress-text" placeholder="Nessuna nuova nota" readonly>@patientData.Notes</textarea>
|
||||
|
||||
<div id="send-doc-message">
|
||||
<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>
|
||||
<p id="message-error" class="text-red"></p>
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
@using SeniorAssistant.Models;
|
||||
@using SeniorAssistant.Models.Users;
|
||||
@using SeniorAssistant.Data;
|
||||
@using Microsoft.AspNetCore.Mvc;
|
||||
@using Microsoft.AspNetCore.Http;
|
||||
Binary file not shown.
Reference in New Issue
Block a user