diff --git a/RestAPI/Controllers/RoleController.cs b/RestAPI/Controllers/RoleController.cs new file mode 100644 index 0000000..0121b8b --- /dev/null +++ b/RestAPI/Controllers/RoleController.cs @@ -0,0 +1,120 @@ +using BusinessLogic.BusinessLogic; +using Contracts.BindingModels; +using Contracts.BusinessLogicContracts; +using Contracts.Exceptions; +using Contracts.SearchModels; +using Contracts.ViewModels; +using Microsoft.AspNetCore.Http.HttpResults; +using Microsoft.AspNetCore.Mvc; + +namespace RestAPI.Controllers +{ + [Route("[controller]/[action]")] + [ApiController] + public class RoleController + { + private readonly ILogger _logger; + private readonly IRoleLogic _roleLogic; + + public RoleController(ILogger logger, IRoleLogic roleLogic) + { + _logger = logger; + _roleLogic = roleLogic; + } + + [HttpGet] + public IResult Get([FromQuery] RoleSearchModel model) + { + try + { + var res = _roleLogic.ReadElement(model); + return Results.Ok(res); + } + catch (ElementNotFoundException ex) + { + _logger.LogInformation(ex, "Element not found"); + return Results.NoContent(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error get role"); + return Results.Problem(ex.Message); + } + } + + [HttpGet] + public IResult GeFilteredtList([FromQuery] RoleSearchModel model) + { + try + { + var res = _roleLogic.ReadElements(model).ToList(); + return Results.Ok(res); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error get list role"); + return Results.Problem(ex.Message); + } + } + + [HttpGet] + public IResult GetList() + { + try + { + var res = _roleLogic.ReadElements(null).ToList(); + return Results.Ok(res); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error get list role"); + return Results.Problem(ex.Message); + } + } + + [HttpPost] + public IResult Create([FromBody] RoleBindingModel model) + { + try + { + var res = _roleLogic.Create(model); + return Results.Created(string.Empty, res); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error create role"); + return Results.Problem(ex.Message); + } + } + + [HttpPut] + public IResult Update([FromBody] RoleBindingModel model) + { + try + { + var res = _roleLogic.Update(model); + return Results.Ok(res); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error update role"); + return Results.Problem(ex.Message); + } + } + + [HttpDelete] + public IResult Delete([FromQuery] RoleSearchModel model) + { + try + { + var res = _roleLogic.Delete(model); ; + return Results.Ok(res); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error delete role"); + return Results.Problem(ex.Message); + } + } + } +} \ No newline at end of file diff --git a/RestAPI/Controllers/WeatherForecastController.cs b/RestAPI/Controllers/WeatherForecastController.cs deleted file mode 100644 index 40741f2..0000000 --- a/RestAPI/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace RestAPI.Controllers -{ - [ApiController] - [Route("[controller]")] - public class WeatherForecastController : ControllerBase - { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } - } -} diff --git a/RestAPI/Program.cs b/RestAPI/Program.cs index 48863a6..6fbbd7c 100644 --- a/RestAPI/Program.cs +++ b/RestAPI/Program.cs @@ -1,19 +1,43 @@ +using BusinessLogic.BusinessLogic; +using Contracts.BusinessLogicContracts; +using Contracts.StorageContracts; +using DatabaseImplement.Implements; +using Microsoft.OpenApi.Models; +using System; + +const string VERSION = "v1"; +const string TITLE = "21GunsRestAPI"; + var builder = WebApplication.CreateBuilder(args); -// Add services to the container. +builder.Logging.SetMinimumLevel(LogLevel.Trace); +builder.Logging.AddLog4Net("log4net.config"); + +#region DI + +builder.Services.AddTransient(); +builder.Services.AddTransient(); + +builder.Services.AddTransient(); +builder.Services.AddTransient(); + +#endregion DI builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +builder.Services.AddSwaggerGen(c => +{ + c.SwaggerDoc(VERSION, new OpenApiInfo { Title = TITLE, Version = VERSION }); +}); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { - app.UseSwagger(); - app.UseSwaggerUI(); + app.UseSwagger(); + app.UseSwaggerUI(c => c.SwaggerEndpoint($"/swagger/{VERSION}/swagger.json", $"{TITLE} {VERSION}")); } app.UseHttpsRedirection(); @@ -22,4 +46,4 @@ app.UseAuthorization(); app.MapControllers(); -app.Run(); +app.Run(); \ No newline at end of file diff --git a/RestAPI/RestAPI.csproj b/RestAPI/RestAPI.csproj index 2cf74c4..ee6f069 100644 --- a/RestAPI/RestAPI.csproj +++ b/RestAPI/RestAPI.csproj @@ -11,7 +11,20 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + Always + + + diff --git a/RestAPI/RestAPI.http b/RestAPI/RestAPI.http index ae5b1fd..cb9c1f5 100644 --- a/RestAPI/RestAPI.http +++ b/RestAPI/RestAPI.http @@ -1,6 +1,6 @@ @RestAPI_HostAddress = http://localhost:5168 -GET {{RestAPI_HostAddress}}/weatherforecast/ +GET {{RestAPI_HostAddress}}/api/ Accept: application/json ### diff --git a/RestAPI/WeatherForecast.cs b/RestAPI/WeatherForecast.cs deleted file mode 100644 index cf5ac94..0000000 --- a/RestAPI/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace RestAPI -{ - public class WeatherForecast - { - public DateOnly Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } - } -} diff --git a/RestAPI/log4net.config b/RestAPI/log4net.config new file mode 100644 index 0000000..0da4e4c --- /dev/null +++ b/RestAPI/log4net.config @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file