Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.3.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.2" />
<PackageReference Include="Scalar.AspNetCore" Version="2.0.18" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public abstract class GenericControllerBase<TModel>(ILogger logger,
internal readonly IService _service = service;

[HttpPost()]
[ProducesResponseType<Product>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public virtual async Task<IActionResult> AddAsync([FromBody] TModel model)
{
WriteToTraceLog(nameof(GenericControllerBase<TModel>), nameof(AddAsync));
Expand All @@ -31,8 +29,6 @@ public virtual async Task<IActionResult> AddAsync([FromBody] TModel model)
}

[HttpDelete("id")]
[ProducesResponseType<Product>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public virtual async Task<IActionResult> DeleteAsync(int id)
{
WriteToTraceLog(nameof(GenericControllerBase<TModel>), nameof(DeleteAsync));
Expand All @@ -43,8 +39,6 @@ public virtual async Task<IActionResult> DeleteAsync(int id)
}

[HttpGet()]
[ProducesResponseType<Product>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public virtual async Task<IActionResult> FindAllAsync([FromBody] PageingFilter pageingFilter)
{
WriteToTraceLog(nameof(GenericControllerBase<TModel>), nameof(FindAllAsync), "pageingFilter");
Expand All @@ -58,15 +52,13 @@ public virtual async Task<IActionResult> FindAllAsync([FromBody] PageingFilter p
}

[HttpGet("id")]
[ProducesResponseType<Product>(StatusCodes.Status200OK)]
public virtual async Task<IActionResult?> FindAsync(int id)
{
WriteToTraceLog(nameof(GenericControllerBase<TModel>), nameof(FindAllAsync));
return Ok(await ((IFindService<TModel, int>)_service).FindAsync(id));
}

[HttpPut()]
[ProducesResponseType<Product>(StatusCodes.Status200OK)]
public virtual async Task<IActionResult> UpdateAsync([FromBody] TModel model)
{
WriteToTraceLog(nameof(GenericControllerBase<TModel>), nameof(UpdateAsync), "model");
Expand Down
37 changes: 37 additions & 0 deletions src/API/AdventureWorksDemo.API/Controllers/ReportsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using AdventureWorksDemo.Data.Models.Reports;
using AdventureWorksDemo.Data.Services;

using Microsoft.AspNetCore.Mvc;

namespace AdventureWorksDemo.API.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ReportsController(ILogger<ReportsController> logger, IReportService service) : ControllerBase
{
internal readonly ILogger _logger = logger;
internal readonly IReportService _service = service;

[HttpGet("onlinevsoffline")]
[ProducesResponseType<SalesSummary>(StatusCodes.Status200OK)]
public virtual async Task<IActionResult> ReportOnlineVsOffLine()
{
WriteToTraceLog(nameof(AdventureWorksDemo.API.Controllers), nameof(ReportOnlineVsOffLine));
return Ok(await Task.Run(() => _service.ReportOnlineVsOffLine()));
}

[HttpGet("salesbyterritory")]
[ProducesResponseType<SaleByTerritory>(StatusCodes.Status200OK)]
public virtual async Task<IActionResult> ReportSalesByTerritory()
{
WriteToTraceLog(nameof(AdventureWorksDemo.API.Controllers), nameof(ReportSalesByTerritory));
return Ok(await Task.Run(() => _service.ReportSalesByTerritory()));
}

internal void WriteToTraceLog(string namespaceName, string methodName, string parmeterNames = "")
{
string Message = $"{namespaceName}.{methodName}({parmeterNames})";
_logger.LogTrace("{Message}", Message);
}
}
}
21 changes: 13 additions & 8 deletions src/API/AdventureWorksDemo.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using AdventureWorksDemo.API.Exceptions;
using AdventureWorksDemo.Data.StartUp;

using Microsoft.Extensions.DependencyInjection;

using Scalar.AspNetCore;

namespace AdventureWorksDemo.API
Expand All @@ -16,21 +14,30 @@ public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

//Exception handling
////Exception handling
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
builder.Services.AddProblemDetails();
// Add child DI objects
new IocData(builder.Configuration).ConfigureServices(builder.Services);
// Add controllers
builder.Services.AddControllers();
// Add child projects
new IocData(builder.Configuration).ConfigureServices(builder.Services);
// Add Cache Timeout
builder.Services.AddOutputCache(options =>
{
options.AddBasePolicy(policy => policy.Expire(TimeSpan.FromMinutes(10)));
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApi();

var app = builder.Build();

app.UseOutputCache();

app.UseExceptionHandler();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapOpenApi()
.CacheOutput();
app.MapScalarApiReference();
}

Expand All @@ -40,8 +47,6 @@ public static void Main(string[] args)

app.MapControllers();

app.UseExceptionHandler();

app.Run();
}
}
Expand Down
Loading