diff --git a/API_SQLuedo/API/API.csproj b/API_SQLuedo/API/API.csproj index effc567..6cce789 100644 --- a/API_SQLuedo/API/API.csproj +++ b/API_SQLuedo/API/API.csproj @@ -21,6 +21,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/API_SQLuedo/API/Controllers/QueryController.cs b/API_SQLuedo/API/Controllers/QueryController.cs new file mode 100644 index 0000000..9ab92d7 --- /dev/null +++ b/API_SQLuedo/API/Controllers/QueryController.cs @@ -0,0 +1,32 @@ +using Dto; +using Asp.Versioning; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Shared; +using Model.OrderCriteria; + +namespace API.Controllers +{ + [Route("api/v{version:apiVersion}/[controller]")] + [Authorize] + [ApiVersion("1.0")] + [ApiController] + public class QueryController(ILogger logger, IQueryService queryService) : ControllerBase + { + [HttpPost("{database}/execute")] + [ProducesResponseType(typeof(QueryDto), 200)] + [ProducesResponseType(typeof(string), 204)] + public IActionResult ExecuteQuery([FromBody]string query, string database) + { + var queryResult = queryService.ExecuteQuery(query, database); + if (queryResult == null) + { + logger.LogError("[ERREUR] La requête n'a rien renvoyé."); + return StatusCode(204); + } + + logger.LogInformation("[INFORMATION] La requête a renvoyé : {result} ", queryResult); + return Ok(queryResult); + } + } +} diff --git a/API_SQLuedo/API/Program.cs b/API_SQLuedo/API/Program.cs index 0b0944b..559793d 100644 --- a/API_SQLuedo/API/Program.cs +++ b/API_SQLuedo/API/Program.cs @@ -19,6 +19,8 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddScoped, QueryDataServiceApi>(); + builder.Services.AddScoped, UserDataService>(); builder.Services.AddScoped, UserDataServiceApi>(); diff --git a/API_SQLuedo/API/Service/QueryDataServiceApi.cs b/API_SQLuedo/API/Service/QueryDataServiceApi.cs new file mode 100644 index 0000000..e2e8f3c --- /dev/null +++ b/API_SQLuedo/API/Service/QueryDataServiceApi.cs @@ -0,0 +1,69 @@ +using Dto; +using Entities; +using Model.OrderCriteria; +using Npgsql; +using Shared; +using Shared.Mapper; +using System.Text; +using Newtonsoft.Json; + +namespace API.Service; + +public class QueryDataServiceApi : IQueryService +{ + public QueryDto ExecuteQuery(string query, string database) + { + string connectionString = + $"Host=localhost;Username=admin;Password=motdepasse;Database={database}"; + + if (string.IsNullOrEmpty(database)) + { + return new QueryDto { Result = "Le nom de la base de données est requis." }; + } + + using (NpgsqlConnection connection = new NpgsqlConnection(connectionString)) + { + connection.Open(); + using (NpgsqlCommand command = new NpgsqlCommand(query, connection)) + { + using (NpgsqlDataReader reader = command.ExecuteReader()) + { + List> resultList = new List>(); + + + List columnNames = new List(); + for (int i = 0; i < reader.FieldCount; i++) + { + columnNames.Add(reader.GetName(i)); + } + + while (reader.Read()) + { + Dictionary row = new Dictionary(); + for (int i = 0; i < reader.FieldCount; i++) + { + row[columnNames[i]] = reader[i].ToString(); + } + + resultList.Add(row); + } + + string resultJson = JsonConvert.SerializeObject(resultList); + + QueryDto queryDto = new QueryDto { Result = resultJson }; + return queryDto; + } + } + } + } + + public IEnumerable GetTables(string database) + { + throw new NotImplementedException(); + } + + public IEnumerable GetColumns(string database, string table) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/API_SQLuedo/Dto/QueryDTO.cs b/API_SQLuedo/Dto/QueryDTO.cs new file mode 100644 index 0000000..ec3c0b9 --- /dev/null +++ b/API_SQLuedo/Dto/QueryDTO.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Dto +{ + public class QueryDto + { + public string Result { get; set; } + } +} diff --git a/API_SQLuedo/Shared/IQueryService.cs b/API_SQLuedo/Shared/IQueryService.cs new file mode 100644 index 0000000..85ecdf2 --- /dev/null +++ b/API_SQLuedo/Shared/IQueryService.cs @@ -0,0 +1,17 @@ +using Microsoft.AspNetCore.Mvc; +using Model.OrderCriteria; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Shared +{ + public interface IQueryService + { + public TQuery ExecuteQuery(string query, string database); + public IEnumerable GetTables(string database); + public IEnumerable GetColumns(string database,string table); + } +}