Added various
- Added DOC - Added Patient - Added Notifications - Added Messages - Various Refactoring
This commit is contained in:
@@ -4,7 +4,8 @@ using SeniorAssistant.Models;
|
||||
using SeniorAssistant.Controllers;
|
||||
using LinqToDB;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using SeniorAssistant.Models.Users;
|
||||
|
||||
namespace IdentityDemo.Controllers
|
||||
{
|
||||
@@ -12,6 +13,8 @@ namespace IdentityDemo.Controllers
|
||||
[Route("[controller]/[action]")]
|
||||
public class AccountController : BaseController
|
||||
{
|
||||
private readonly JsonResponse OkJson = new JsonResponse();
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult _login(string username, string password)
|
||||
{
|
||||
@@ -26,15 +29,20 @@ namespace IdentityDemo.Controllers
|
||||
if (result.Count == 1)
|
||||
{
|
||||
var loggedUser = HttpContext.Session.GetString(Username);
|
||||
if (loggedUser==null || !loggedUser.Equals(username))
|
||||
if (loggedUser==null || !loggedUser.Equals(username)) // non ha senso
|
||||
{
|
||||
User user = result.First();
|
||||
HttpContext.Session.SetString(Username, username);
|
||||
HttpContext.Session.SetString("email", user.Email);
|
||||
HttpContext.Session.SetString("name", user.Name);
|
||||
HttpContext.Session.SetString("role", user.Role);
|
||||
//HttpContext.Session.SetString("lastname", user.LastName);
|
||||
|
||||
|
||||
var isDoc = (from d in Db.Doctors
|
||||
where d.Username.Equals(username)
|
||||
select d).ToArray().FirstOrDefault() != null;
|
||||
HttpContext.Session.SetString("role", isDoc? "doctor":"patient");
|
||||
|
||||
response.Success = true;
|
||||
response.Message = Request.Query["ReturnUrl"];
|
||||
}
|
||||
@@ -56,34 +64,109 @@ namespace IdentityDemo.Controllers
|
||||
[HttpPost]
|
||||
public ActionResult _register(User user)
|
||||
{
|
||||
JsonResponse response = new JsonResponse() { Success = true };
|
||||
|
||||
if(ModelState.IsValid)
|
||||
return Action(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Db.Insert(user);
|
||||
_login(user.Username, user.Password);
|
||||
return _login(user.Username, user.Password);
|
||||
}
|
||||
catch
|
||||
{
|
||||
response.Success = false;
|
||||
response.Message = "Username already exists";
|
||||
return Json(new JsonResponse(false, "Username already exists"));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
response.Success = false;
|
||||
response.Message = "Modello non valido";
|
||||
}
|
||||
|
||||
return Json(response);
|
||||
});
|
||||
}
|
||||
|
||||
internal class JsonResponse
|
||||
[HttpPost]
|
||||
public ActionResult _notification(string username, string message)
|
||||
{
|
||||
public bool Success { get; internal set; }
|
||||
public string Message { get; internal set; }
|
||||
return LoggedAction(() =>
|
||||
{
|
||||
Db.Insert(new Notification()
|
||||
{
|
||||
Message = message,
|
||||
Username = username,
|
||||
Time = DateTime.Now,
|
||||
Seen = false
|
||||
});
|
||||
return Json(OkJson);
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public ActionResult _notification(int id)
|
||||
{
|
||||
return LoggedAction(() =>
|
||||
{
|
||||
JsonResponse response = OkJson;
|
||||
|
||||
Notification note = Db.Notifications.Where(n => n.Id == id).ToArray().FirstOrDefault();
|
||||
if(note != null)
|
||||
{
|
||||
note.Seen = true;
|
||||
Db.Update(note);
|
||||
}
|
||||
else
|
||||
{
|
||||
response.Success = false;
|
||||
response.Message = "La notifica da modificare non esiste";
|
||||
}
|
||||
return Json(response);
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult _addDoc(string doctor)
|
||||
{
|
||||
return LoggedAction(() =>
|
||||
{
|
||||
string username = HttpContext.Session.GetString(Username);
|
||||
var isAlreadyPatient = Db.Patients.Where(p => p.Username.Equals(username)).ToArray().FirstOrDefault() != null;
|
||||
if (isAlreadyPatient)
|
||||
return Json(new JsonResponse()
|
||||
{
|
||||
Success = false,
|
||||
Message = "You are already a patient"
|
||||
});
|
||||
|
||||
var docExist = Db.Doctors.Where(d => d.Username.Equals(doctor)).ToArray().FirstOrDefault() != null;
|
||||
if(!docExist)
|
||||
return Json(new JsonResponse()
|
||||
{
|
||||
Success = false,
|
||||
Message = "Doctor doesn't exist"
|
||||
});
|
||||
|
||||
Db.Insert(new Patient()
|
||||
{
|
||||
Doctor = doctor,
|
||||
Username = username
|
||||
});
|
||||
|
||||
_notification(doctor, "L'utente "+username+" ti ha inserito come il suo dottore.");
|
||||
return Json(new JsonResponse());
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult _sendMessage(string reciver, string body)
|
||||
{
|
||||
return LoggedAction(() => {
|
||||
string username = HttpContext.Session.GetString(Username);
|
||||
Message message = new Message()
|
||||
{
|
||||
Reciver = reciver,
|
||||
Body = body,
|
||||
Time = DateTime.Now,
|
||||
Username = username,
|
||||
Seen = false
|
||||
};
|
||||
|
||||
Db.Insert(message);
|
||||
|
||||
return Json(new JsonResponse());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,4 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace SeniorAssistant.Controllers
|
||||
{
|
||||
@@ -45,9 +43,15 @@ namespace SeniorAssistant.Controllers
|
||||
return CheckAuthorized("Data", user);
|
||||
}
|
||||
|
||||
[Route("Message/{Id}")]
|
||||
public IActionResult Message(int id)
|
||||
{
|
||||
return CheckAuthorized("Message", id);
|
||||
}
|
||||
|
||||
private IActionResult CheckAuthorized(string view, object model = null)
|
||||
{
|
||||
if (HttpContext.Session.GetString("username") == null)
|
||||
if (!IsLogged())
|
||||
{
|
||||
model = "/" + view;
|
||||
view = "Index";
|
||||
|
||||
@@ -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,11 +1,16 @@
|
||||
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";
|
||||
|
||||
IDataContextFactory<SeniorDataContext> dbFactory;
|
||||
@@ -28,5 +33,63 @@ namespace SeniorAssistant.Controllers
|
||||
{
|
||||
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)
|
||||
{
|
||||
return LoggedAction(() =>
|
||||
{
|
||||
var loggedUser = HttpContext.Session.GetString(Username);
|
||||
var condition = username.Equals(loggedUser);
|
||||
|
||||
condition = condition || (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; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user