public IHttpActionResult GetDriver([FromUri] string senderID, [FromUri] string userIdToGet) { Driver result = null; if (!LoggedUsers.Contains(senderID)) { return(Content(HttpStatusCode.Unauthorized, "Not logged in.")); } try { if (DbDriver.Exists(userIdToGet)) { if (!DbAdmin.Exists(senderID) || senderID != userIdToGet) { return(Content(HttpStatusCode.Unauthorized, "Not a dispatcher nor the user whose information are requested.")); } result = DbDriver.GetSingleEntityByKey(userIdToGet); } } catch (Exception e) { Trace.Write($"Error on 'GetDriver()'. Error message: {e.Message}"); Trace.Write($"[STACK_TRACE] {e.StackTrace}"); return(InternalServerError(e)); } if (result == null) { return(NotFound()); } return(Ok(result)); }
public IHttpActionResult Login([FromBody] LoginModel loginModel) { if (LoggedUsers.Contains(loginModel.Username)) { return(Content(HttpStatusCode.Conflict, $"User '{loginModel.Username}' already logged in.")); } IUser result = null; try { if (DbAdmin.Exists(loginModel.Username)) { result = DbAdmin.GetSingleEntityByKey(loginModel.Username); } else if (DbDriver.Exists(loginModel.Username)) { result = DbDriver.GetSingleEntityByKey(loginModel.Username); } else if (DbCustomer.Exists(loginModel.Username)) { result = DbCustomer.GetSingleEntityByKey(loginModel.Username); } } catch (Exception e) { Trace.Write($"Error on 'Login()'. Error message: {e.Message}"); Trace.Write($"[STACK_TRACE] {e.StackTrace}"); return(InternalServerError(e)); } if (result == null) { return(NotFound()); } if (result.Password.Equals(loginModel.Password)) { LoggedUsers.Add(loginModel.Username); return(Ok(result)); } else { return(BadRequest($"Entered password did not match the required one for user '{loginModel.Username}'.")); } }