/// <summary> /// Convert database query result to DBResult type /// </summary> /// <param name="outputParams"></param> /// <param name="data"></param> /// <returns></returns> public static Models.Database.DBResult GetResult(DynamicParameters outputParams, object data) { if (outputParams == null) { throw new ArgumentNullException("Invalid outputParams"); } var rst = new Models.Database.DBResult() { ErrorCode = outputParams.Get <int>("ErrorCode"), ErrorMessage = outputParams.Get <string>("ErrorMessage"), Data = data }; try { //if database query for searching, try to catch ActualSize output param for pagination rst.ActualSize = outputParams.Get <int>("ActualSize"); } catch (Exception) { rst.ActualSize = data == null ? 0 : 1; } return(rst); }
/// <summary> /// Generate JWT based on the Login results /// </summary> /// <param name="data"></param> /// <param name="key"></param> /// <param name="issuer"></param> /// <param name="mapper"></param> /// <returns></returns> public static UserJWT Get(Models.Database.DBResult data, string key, string issuer, AutoMapper.IMapper mapper) { if (data == null || data.Data == null) { throw new ArgumentNullException("In UserJWT, user could not be null."); } if (data.ErrorCode != 200 || !string.IsNullOrEmpty(data.ErrorMessage)) { throw new ArgumentException("User data is not valid."); } if (mapper == null) { throw new ArgumentNullException("Mapper could not be null."); } if (!(data.Data is Models.Database.StoredProcedures.SP_Login.Outputs)) { throw new ArgumentException("Data is not valid."); } var loginResult = data.Data as Models.Database.StoredProcedures.SP_Login.Outputs; var userJWT = mapper.Map <UserJWT>(loginResult); userJWT.Token = loginResult.Token; userJWT.Generate(key, issuer); return(userJWT); }
public void Setup() { _key = "A simple key for generating JWT and test the common class."; _issuer = "http://FTSS.com"; _dbToken = "DatabaseToken"; _roleTitle = "DBRoleTitle"; _email = "Username"; _expireDate = System.DateTime.Now.AddDays(1); _loginResult = new Models.Database.StoredProcedures.SP_Login.Outputs() { UserId = 1, Token = _dbToken, ExpireDate = _expireDate, RoleTitle = _roleTitle, Email = _email }; _data = new Models.Database.DBResult(200, "", _loginResult); _userJWT = new Logic.Security.UserJWT() { UserId = 1, Token = _dbToken, ExpireDate = _expireDate, RoleTitle = _roleTitle, Email = _email }; _mapper = new Mock <AutoMapper.IMapper>(); _mapper.Setup(c => c.Map <Logic.Security.UserJWT>(_loginResult)).Returns(_userJWT); }
/// <summary> /// Convert database result to rest standard result /// </summary> /// <param name="dbResult"></param> /// <returns></returns> public IActionResult FromDatabase(Models.Database.DBResult dbResult) { if (dbResult == null) { return(StatusCode(500, "Unhandled internal server error")); } if (dbResult.StatusCode >= 200 && dbResult.StatusCode < 300) { return(Ok(dbResult)); } return(StatusCode(dbResult.StatusCode, dbResult.ErrorMessage ?? "Unhandled error")); }
public IActionResult Login([FromBody] Models.Database.StoredProcedures.SP_Login.Inputs inputs) { try { var rst = _ctx.SP_Login(inputs); //Generate JWT if (rst.ErrorCode == 200) { var jwt = Logic.Security.UserJWT.Get(rst, JWTKey, JWTIssuer, _mapper); rst = new Models.Database.DBResult(200, "", jwt); } return(FromDatabase(rst)); } catch (Exception e) { _logger.Add(e, "Error in UsersController.Login(filterParams)"); return(Problem(e.Message, e.StackTrace, 500, "Error in Login")); } }