Refactoring
* refactoring * fixes * messages * new interface
This commit is contained in:
@@ -6,6 +6,8 @@ using LinqToDB;
|
||||
using System.Linq;
|
||||
using System;
|
||||
using SeniorAssistant.Models.Users;
|
||||
using SeniorAssistant.Data;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IdentityDemo.Controllers
|
||||
{
|
||||
@@ -14,111 +16,112 @@ namespace IdentityDemo.Controllers
|
||||
public class AccountController : BaseController
|
||||
{
|
||||
private static readonly string NoteModified = "Il tuo dottore ha modificato la nota per te";
|
||||
private static readonly string InvalidLogIn = "Username o Password sbagliati";
|
||||
private static readonly string AlreadyLogIn = "L'utente e' gia' loggato";
|
||||
private static readonly string UsernameDupl = "Lo username selezionato e' gia' in uso";
|
||||
private static readonly string ModNotExists = "L'oggetto da modificare non esiste";
|
||||
private static readonly string AlreadyPatie = "Sei gia' un paziente";
|
||||
private static readonly string DocNotExists = "Il dottore selezionato non esiste";
|
||||
private static readonly string InsertAsDoct = "Ti ha inserito come il suo dottore: ";
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult _login(string username, string password)
|
||||
public async Task<ActionResult> _login(string username, string password)
|
||||
{
|
||||
JsonResponse response = new JsonResponse
|
||||
{
|
||||
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;
|
||||
var result = await (from u in Db.Users
|
||||
where u.Username.Equals(username)
|
||||
&& u.Password.Equals(password)
|
||||
select u).ToListAsync();
|
||||
|
||||
if (result.Count == 1)
|
||||
{
|
||||
var loggedUser = HttpContext.Session.GetString(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("lastname", user.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");
|
||||
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"];
|
||||
}
|
||||
else
|
||||
{
|
||||
response.Message = "User already logged";
|
||||
}
|
||||
return Json(OkJson);
|
||||
}
|
||||
return Json(response);
|
||||
return Json(new JsonResponse()
|
||||
{
|
||||
Success = false,
|
||||
Message = InvalidLogIn
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult _logout()
|
||||
{
|
||||
HttpContext.Session.Clear();
|
||||
return Json(new JsonResponse());
|
||||
return Json(OkJson);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult _register(User user)
|
||||
public async Task<ActionResult> _register(User user)
|
||||
{
|
||||
return Action(() =>
|
||||
try
|
||||
{
|
||||
try
|
||||
Db.Insert(user);
|
||||
return await _login(user.Username, user.Password);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Json(new JsonResponse()
|
||||
{
|
||||
Db.Insert(user);
|
||||
return _login(user.Username, user.Password);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Json(new JsonResponse(false, "Username already exists"));
|
||||
}
|
||||
});
|
||||
Success = false,
|
||||
Message = UsernameDupl
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult _notification(string username, string message)
|
||||
public async Task<ActionResult> _notification(string username, string message, string redirectUrl = "#")
|
||||
{
|
||||
return LoggedAction(() =>
|
||||
return await LoggedAction(() =>
|
||||
{
|
||||
Db.Insert(new Notification()
|
||||
{
|
||||
Message = message,
|
||||
Username = username,
|
||||
Time = DateTime.Now,
|
||||
Seen = false
|
||||
Body = message,
|
||||
Username = HttpContext.Session.GetString(Username),
|
||||
Receiver = username,
|
||||
Url = redirectUrl,
|
||||
Time = DateTime.Now
|
||||
});
|
||||
return Json(OkJson);
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public ActionResult _notification(int id)
|
||||
public async Task<ActionResult> _notification(int id)
|
||||
{
|
||||
return LoggedAction(() =>
|
||||
return await LoggedAction(() =>
|
||||
{
|
||||
JsonResponse response = OkJson;
|
||||
|
||||
Notification note = Db.Notifications.Where(n => n.Id == id).ToArray().FirstOrDefault();
|
||||
if(note != null)
|
||||
{
|
||||
note.Seen = true;
|
||||
note.Seen = DateTime.Now;
|
||||
Db.Update(note);
|
||||
}
|
||||
else
|
||||
{
|
||||
response.Success = false;
|
||||
response.Message = "La notifica da modificare non esiste";
|
||||
response.Message = ModNotExists;
|
||||
}
|
||||
return Json(response);
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult _addDoc(string doctor)
|
||||
public async Task<ActionResult> _addDoc(string doctor)
|
||||
{
|
||||
return LoggedAction(() =>
|
||||
return await LoggedAction(() =>
|
||||
{
|
||||
string username = HttpContext.Session.GetString(Username);
|
||||
var isAlreadyPatient = Db.Patients.Where(p => p.Username.Equals(username)).ToArray().FirstOrDefault() != null;
|
||||
@@ -126,7 +129,7 @@ namespace IdentityDemo.Controllers
|
||||
return Json(new JsonResponse()
|
||||
{
|
||||
Success = false,
|
||||
Message = "You are already a patient"
|
||||
Message = AlreadyPatie
|
||||
});
|
||||
|
||||
var docExist = Db.Doctors.Where(d => d.Username.Equals(doctor)).ToArray().FirstOrDefault() != null;
|
||||
@@ -134,7 +137,7 @@ namespace IdentityDemo.Controllers
|
||||
return Json(new JsonResponse()
|
||||
{
|
||||
Success = false,
|
||||
Message = "Doctor doesn't exist"
|
||||
Message = DocNotExists
|
||||
});
|
||||
|
||||
Db.Insert(new Patient()
|
||||
@@ -143,40 +146,65 @@ namespace IdentityDemo.Controllers
|
||||
Username = username
|
||||
});
|
||||
|
||||
_notification(doctor, "L'utente "+username+" ti ha inserito come il suo dottore.");
|
||||
return Json(new JsonResponse());
|
||||
var a = _notification(doctor, InsertAsDoct + username);
|
||||
return Json(OkJson);
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult _sendMessage(string reciver, string body)
|
||||
public async Task<ActionResult> _sendMessage(string receiver, string body)
|
||||
{
|
||||
return LoggedAction(() => {
|
||||
return await LoggedAction(() => {
|
||||
string username = HttpContext.Session.GetString(Username);
|
||||
Message message = new Message()
|
||||
{
|
||||
Reciver = reciver,
|
||||
Receiver = receiver,
|
||||
Body = body,
|
||||
Time = DateTime.Now,
|
||||
Username = username,
|
||||
Seen = false
|
||||
Username = username
|
||||
};
|
||||
|
||||
Db.Insert(message);
|
||||
|
||||
return Json(new JsonResponse());
|
||||
return Json(OkJson);
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public ActionResult _addNote(string patient, string text)
|
||||
public async Task<ActionResult> _addNote(string patient, string text)
|
||||
{
|
||||
return LoggedAccessDataOf(patient, () =>
|
||||
return await LoggedAccessDataOf(patient, true, () =>
|
||||
{
|
||||
var pat = Db.Patients.Where((p) => p.Username.Equals(patient)).FirstOrDefault();
|
||||
pat.Notes = text;
|
||||
Db.Update(pat);
|
||||
_notification(patient, NoteModified);
|
||||
var a = _notification(patient, NoteModified);
|
||||
|
||||
return Json(OkJson);
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public async Task<ActionResult> _minHeartToPatient(string patient, int value)
|
||||
{
|
||||
return await LoggedAccessDataOf(patient, true, () =>
|
||||
{
|
||||
var pat = Db.Patients.Where((p) => p.Username.Equals(patient)).FirstOrDefault();
|
||||
pat.MinHeart = value;
|
||||
Db.Update(pat);
|
||||
|
||||
return Json(OkJson);
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public async Task<ActionResult> _maxHeartToPatient(string patient, int value)
|
||||
{
|
||||
return await LoggedAccessDataOf(patient, true, () =>
|
||||
{
|
||||
var pat = Db.Patients.Where((p) => p.Username.Equals(patient)).FirstOrDefault();
|
||||
pat.MaxHeart = value;
|
||||
Db.Update(pat);
|
||||
|
||||
return Json(OkJson);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user