using System; using System.Collections.Generic; using Amazon.Lambda.Core; using Amazon.Lambda.APIGatewayEvents; using MySqlConnector; using System.Data; using classes; using Services; namespace PMF { public class GetGraph: Pmfbase { public APIGatewayProxyResponse GetGraphs(APIGatewayProxyRequest request, ILambdaContext context) { APIGatewayProxyResponse response = null; string statement = "GET Graphs"; string userId = ""; var result = new List { }; try { string validationMessage = string.Empty; if (!Helper.IsRequestEmpty(request)) { var validRequest = Helper.ValidateRequestParameters(ref validationMessage, request.QueryStringParameters, "userId"); if (validRequest) { if (_con.State == ConnectionState.Closed) _con.Open(); var graphs = GraphService.LoadGraphSettings(_con); if (request.QueryStringParameters.ContainsKey("endDate") && request.QueryStringParameters.ContainsKey("startDate")) { var startDate = Helper.CleanInput(request.QueryStringParameters["startDate"].ToString()); var endDate = Helper.CleanInput(request.QueryStringParameters["endDate"].ToString()); userId = Helper.CleanInput(request.QueryStringParameters["userId"].ToString()); foreach (var graph in graphs) { switch (graph.GraphType) { case "PieGraph": result.Add(new { GraphTitle = graph.GraphTitle, GraphType = graph.GraphType, GraphX = graph.GraphX, GraphY = graph.GraphY, Data = GraphService.GetPieChart(_con, title: graph.GraphTitle, startDate: startDate, endDate: endDate) }); break; case "Table": if ((graph.GraphTitle.Contains("NedBank"))) { result.Add(new { GraphTitle = graph.GraphTitle, GraphType = graph.GraphType, GraphX = graph.GraphX, GraphY = graph.GraphY, Data = GraphService.GetNedBankTable(_con) }); break; } else { result.Add(new { GraphTitle = graph.GraphTitle, GraphType = graph.GraphType, GraphX = graph.GraphX, GraphY = graph.GraphY, Data = GraphService.GetTable(_con, graphTitle: graph.GraphTitle, startDate: startDate, endDate: endDate) }); } break; case "CounterTab": result.Add(new { GraphTitle = graph.GraphTitle, GraphType = graph.GraphType, Data = GraphService.TotalCounter(_con, graph.GraphTitle, startDate: startDate, endDate: endDate) }); break; case "StackGraph": result.Add(new { GraphTitle = graph.GraphTitle, GraphType = graph.GraphType, GraphX = graph.GraphX, GraphY = graph.GraphY, Data = GraphService.BarGraphFamily(_con, graph, startDate: startDate, endDate: endDate) }); break; case "VerticalBarGraph": result.Add(new { GraphTitle = graph.GraphTitle, GraphType = graph.GraphType, GraphX = graph.GraphX, GraphY = graph.GraphY, Data = GraphService.BarGraphFamily(_con, graph, startDate: startDate, endDate: endDate) }); break; case "LineMarkGraph": result.Add(new { GraphTitle = graph.GraphTitle, GraphType = graph.GraphType, GraphX = graph.GraphX, GraphY = graph.GraphY, Data = GraphService.BarGraphFamily(_con, graph, startDate: startDate, endDate: endDate) }); break; case "AreaGraph": result.Add(new { GraphTitle = graph.GraphTitle, GraphType = graph.GraphType, GraphX = graph.GraphX, GraphY = graph.GraphY, Data = GraphService.BarGraphFamily(_con, graph, startDate: startDate, endDate: endDate) }); break; case "RadarGraph": result.Add(new { GraphTitle = graph.GraphTitle, GraphType = graph.GraphType, GraphX = graph.GraphX, GraphY = graph.GraphY, Data = GraphService.BarGraphFamily(_con, graph, startDate: startDate, endDate: endDate) }); break; } } response = HttpService.CreateResponse(200, payload: result); } else { response = HttpService.CreateResponse(400, payload: "Please supply the start Date and the end Date"); } } else { response = HttpService.CreateResponse(400, payload: $"{validationMessage}"); } } else { response = HttpService.CreateResponse(400, payload: "Body can not be empty"); } _logger.WriteLog(userId, statement, response.Body, "DB"); } catch (Exception ex) { _logger.WriteLog(userId, statement, ex.Message, "DB"); return HttpService.CreateResponse(500, payload: "Graphs can not be generated. Please check the active sql queries"); } finally { if (_con != null || _con.State == ConnectionState.Open) _con.Close(); } return response; } } }