Besciamello (#1)
* Fixed login & auth * Added dynamic breadcrumb * Added DOC * Added Patient * Added Notifications * Added Messages * Refactoring api * Re-writed menu * Removed unused things * Created README
This commit was merged in pull request #1.
This commit is contained in:
committed by
GitHub
parent
191daf8218
commit
1246116804
@@ -1,5 +1,6 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SeniorAssistant.Models;
|
||||
using SeniorAssistant.Models.Users;
|
||||
|
||||
namespace SeniorAssistant.Controllers.Services
|
||||
{
|
||||
@@ -18,4 +19,12 @@ namespace SeniorAssistant.Controllers.Services
|
||||
[Route("api/[controller]")]
|
||||
public class UserController : CrudController<User>
|
||||
{ }
|
||||
|
||||
[Route("api/[controller]")]
|
||||
public class PatientController : CrudController<Patient>
|
||||
{ }
|
||||
|
||||
[Route("api/[controller]")]
|
||||
public class DoctorController : CrudController<Doctor>
|
||||
{ }
|
||||
}
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SeniorAssistant.Data;
|
||||
using System.Linq;
|
||||
using System;
|
||||
|
||||
namespace SeniorAssistant.Controllers
|
||||
{
|
||||
public abstract class BaseController : Controller
|
||||
{
|
||||
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 Username = "username";
|
||||
protected readonly JsonResponse OkJson = new JsonResponse();
|
||||
|
||||
IDataContextFactory<SeniorDataContext> dbFactory;
|
||||
SeniorDataContext db;
|
||||
|
||||
@@ -20,5 +29,68 @@ namespace SeniorAssistant.Controllers
|
||||
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
protected bool IsLogged()
|
||||
{
|
||||
return HttpContext.Session.GetString(Username) != null;
|
||||
}
|
||||
|
||||
protected ActionResult Action(Func<ActionResult> success)
|
||||
{
|
||||
return ModelState.IsValid ?
|
||||
success.Invoke() :
|
||||
Json(new JsonResponse()
|
||||
{
|
||||
Success = false,
|
||||
Message = InvalidModel
|
||||
});
|
||||
}
|
||||
|
||||
protected ActionResult LoggedAction(Func<ActionResult> success)
|
||||
{
|
||||
return Action(() =>
|
||||
{
|
||||
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);
|
||||
|
||||
return condition ?
|
||||
success.Invoke() :
|
||||
Json(new JsonResponse()
|
||||
{
|
||||
Success = false,
|
||||
Message = NoAuthorized
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public class JsonResponse
|
||||
{
|
||||
public JsonResponse(bool success=true, string message="")
|
||||
{
|
||||
Success = success;
|
||||
Message = message;
|
||||
}
|
||||
|
||||
public bool Success { get; set; }
|
||||
public string Message { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
[HttpPut("{username}")]
|
||||
public async Task Update(string username, [FromBody]TEntity item)
|
||||
public async Task<IActionResult> Read(string username)
|
||||
{
|
||||
item.Username = username;
|
||||
|
||||
await Db.UpdateAsync(item);
|
||||
return LoggedAccessDataOf(username, () =>
|
||||
{
|
||||
return Json(Db.GetTable<TEntity>().Where((u) => u.Username.Equals(username)).ToArray());
|
||||
});
|
||||
}
|
||||
|
||||
[HttpDelete("{username}")]
|
||||
public async Task Delete(string username) => await Db.GetTable<TEntity>().Where(c => c.Username.Equals(username)).DeleteAsync();
|
||||
[HttpPut("{username}")]
|
||||
public async Task<IActionResult> Update(string username, [FromBody] TEntity entity)
|
||||
{
|
||||
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)
|
||||
{
|
||||
try
|
||||
return LoggedAccessDataOf(username, () =>
|
||||
{
|
||||
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));
|
||||
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();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new List<TEntity>();
|
||||
}
|
||||
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 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)
|
||||
{
|
||||
DateTime date = DateTime.Now.AddHours(-hour);
|
||||
return await Db.GetTable<TEntity>().Where(e => e.Username.Equals(username) && date <= e.Time).ToListAsync();
|
||||
return LoggedAccessDataOf(username, () =>
|
||||
{
|
||||
DateTime date = DateTime.Now.AddHours(-hour);
|
||||
return Json((from entity in Db.GetTable<TEntity>()
|
||||
where entity.Username.Equals(username)
|
||||
&& date <= entity.Time
|
||||
select entity).ToArray());
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create([FromBody]TEntity item)
|
||||
{
|
||||
return Action(() =>
|
||||
{
|
||||
Db.Insert(item);
|
||||
return Json(OkJson);
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public async Task<IActionResult> Update([FromBody]TEntity item)
|
||||
{
|
||||
return LoggedAccessDataOf(item.Username, () =>
|
||||
{
|
||||
var e = Read(item.Username, item.Time);
|
||||
if (e == null)
|
||||
{
|
||||
Create(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
Db.UpdateAsync(item);
|
||||
}
|
||||
|
||||
return Json(OkJson);
|
||||
}, false);
|
||||
}
|
||||
|
||||
[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);
|
||||
|
||||
[HttpPut]
|
||||
public async Task<object> Update([FromBody]TEntity item)
|
||||
{
|
||||
var e = await Read(item.Username, item.Time);
|
||||
if (e == null)
|
||||
{
|
||||
await Create(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
await Db.UpdateAsync(item);
|
||||
}
|
||||
|
||||
return Empty;
|
||||
}
|
||||
|
||||
/*
|
||||
[HttpDelete("{username}")]
|
||||
public async Task Delete(string username) => await Db.GetTable<TEntity>().Where(c => c.Username.Equals(username)).DeleteAsync();
|
||||
*/
|
||||
private TEntity Read(string username, DateTime date) => Db.GetTable<TEntity>().FirstOrDefault(e => e.Username.Equals(username) && date == e.Time);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user