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
@@ -4,65 +4,46 @@ using SeniorAssistant.Models;
|
||||
using SeniorAssistant.Controllers;
|
||||
using LinqToDB;
|
||||
using System.Linq;
|
||||
using System;
|
||||
using SeniorAssistant.Models.Users;
|
||||
|
||||
namespace IdentityDemo.Controllers
|
||||
{
|
||||
|
||||
[ApiExplorerSettings(IgnoreApi = true)]
|
||||
[Route("[controller]/[action]")]
|
||||
public class AccountController : BaseController
|
||||
{
|
||||
/*
|
||||
private readonly UserManager<User> _userManager;
|
||||
private readonly SignInManager<User> _signInManager;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public AccountController(
|
||||
UserManager<User> userManager,
|
||||
SignInManager<User> signInManager,
|
||||
ILogger<AccountController> logger)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_signInManager = signInManager;
|
||||
_logger = logger;
|
||||
}
|
||||
/*
|
||||
[TempData]
|
||||
public string ErrorMessage { get; set; }
|
||||
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> Login(string returnUrl = null)
|
||||
{
|
||||
// Clear the existing external cookie to ensure a clean login process
|
||||
await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
|
||||
|
||||
ViewData["ReturnUrl"] = returnUrl;
|
||||
return View();
|
||||
}
|
||||
*/
|
||||
private static readonly string NoteModified = "Il tuo dottore ha modificato la nota per te";
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult _login(string username, string password)
|
||||
{
|
||||
JsonResponse response = new JsonResponse();
|
||||
response.Success = false;
|
||||
response.Message = "Username or password is invalid.";
|
||||
|
||||
var strunz = Db.GetTable<User>().Where(user => user.Username.Equals(username) && user.Password.Equals(password)).ToListAsync().Result;
|
||||
|
||||
if (strunz.Count == 1)
|
||||
JsonResponse response = new JsonResponse
|
||||
{
|
||||
var loggedUser = HttpContext.Session.GetString("username");
|
||||
if (loggedUser==null || !loggedUser.Equals(username))
|
||||
Success = false,
|
||||
Message = "Username or password is invalid."
|
||||
};
|
||||
|
||||
var result = Db.GetTable<User>().Where(user => user.Username.Equals(username) && user.Password.Equals(password)).ToListAsync().Result;
|
||||
|
||||
if (result.Count == 1)
|
||||
{
|
||||
var loggedUser = HttpContext.Session.GetString(Username);
|
||||
if (loggedUser==null || !loggedUser.Equals(username)) // non ha senso
|
||||
{
|
||||
HttpContext.Session.SetString("username", username);
|
||||
HttpContext.Session.SetString("email", strunz.First().Email);
|
||||
HttpContext.Session.SetString("name", strunz.First().Name);
|
||||
HttpContext.Session.SetString("isdoc", strunz.First().Doctor?"true":"false");
|
||||
//HttpContext.Session.SetString("lastname", strunz.First().LastName);
|
||||
User user = result.First();
|
||||
HttpContext.Session.SetString(Username, username);
|
||||
HttpContext.Session.SetString("email", user.Email);
|
||||
HttpContext.Session.SetString("name", user.Name);
|
||||
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 = "";
|
||||
response.Message = Request.Query["ReturnUrl"];
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -80,28 +61,125 @@ namespace IdentityDemo.Controllers
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult _register(Register register)
|
||||
public ActionResult _register(User user)
|
||||
{
|
||||
if(ModelState.IsValid)
|
||||
return Action(() =>
|
||||
{
|
||||
User user = new User() { Username = register.Username, Email = register.Email, Password = register.Password};
|
||||
try
|
||||
{
|
||||
Db.Insert(user);
|
||||
return _login(user.Username, user.Password);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Json(new JsonResponse() { Success = false, Message = "Username already exist" });
|
||||
return Json(new JsonResponse(false, "Username already exists"));
|
||||
}
|
||||
_login(user.Username, user.Password);
|
||||
return Json(new JsonResponse() { Success = true });
|
||||
}
|
||||
return Json(new JsonResponse() { Success = false, Message = "Modello non valido" });
|
||||
});
|
||||
}
|
||||
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());
|
||||
});
|
||||
}
|
||||
|
||||
[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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user