using System; using System.Collections.Generic; using Amazon.Lambda.Core; using Amazon.Lambda.APIGatewayEvents; using MySqlConnector; using System.Data; using classes; namespace PMF { public class ApiLogs: Pmfbase { MySqlDataReader _reader; public APIGatewayProxyResponse GetAPILogs(APIGatewayProxyRequest request, ILambdaContext context) { //For testing only APIGatewayProxyResponse response; string statement = "DB::GetAPILogs"; string userId = ""; try { if (!Helper.IsRequestEmpty(request)) { string validationMessage = string.Empty; var validRequest = Helper.ValidateRequestParameters(ref validationMessage, request.QueryStringParameters, "userId"); if (validRequest) { if (_con.State == ConnectionState.Closed) _con.Open(); userId = Helper.CleanInput(request.QueryStringParameters["userId"]); statement = "SELECT * FROM `log` WHERE strCognitoUuid = '" + userId + "'"; var cmd = new MySqlCommand(statement, _con); _reader = cmd.ExecuteReader(); List logs = new List { }; if (_reader.HasRows) { while (_reader.Read()) { logs.Add(new { userId = _reader.GetValue(0).ToString(), txtQuery = _reader.GetValue(2).ToString(), tctResponse = _reader.GetValue(3).ToString(), strTye = _reader.GetValue(4).ToString() }); } _reader.Close(); response = HttpService.CreateResponse(200, payload: logs, serializeToJson: true); //_logger.WriteLog(userId, statement, response.Body.ToString(), "DB"); return response; } else { response = HttpService.CreateResponse(400, payload: "User not found", serializeToJson: true); _logger.WriteLog(userId, statement, response.Body.ToString(), "DB"); return response; } } else { _logger.LogMessage(context, $"Processing request failed - Please add parameter(s): {validationMessage}"); response = HttpService.CreateResponse(400, payload: $"Please add parameter(s) {validationMessage}"); _logger.WriteLog(userId, statement, response.Body.ToString(), "DB"); return response; } } else { _logger.LogMessage(context, $"Processing request failed invalid request"); response = HttpService.CreateResponse(400, payload: "No parameters provided"); return response; } } catch (Exception ex) { _logger.LogMessage(context, "Processing request failed: " + ex.Message); _logger.WriteLog(userId, statement, ex.Message, "DB"); return HttpService.CreateResponse(500, payload: ex.Message); } finally { if (_reader != null) _reader.Close(); if (_con != null || _con.State == ConnectionState.Open) _con.Close(); } } } }