refactor: rename Api project to Web
- HrynCo.NotificationService.Api -> HrynCo.NotificationService.Web - HrynCo.NotificationService.Api.IntegrationTests -> HrynCo.NotificationService.Web.IntegrationTests - Updated slnx, docker-compose, project references, and namespaces - Project serves both REST API and admin UI Ref: IT-628 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
namespace HrynCo.NotificationService.Web;
|
||||
|
||||
public sealed class AppSettings
|
||||
{
|
||||
public const string SectionName = "App";
|
||||
|
||||
public string ConnectionString { get; init; } = string.Empty;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using HrynCo.NotificationService.Web.Infrastructure;
|
||||
using HrynCo.NotificationService.Services.Core;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers;
|
||||
|
||||
[Route("api/v1/[controller]")]
|
||||
[ApiController]
|
||||
public abstract class ApiControllerBase : ControllerBase
|
||||
{
|
||||
protected ApiControllerBase(IMediator mediator)
|
||||
{
|
||||
Mediator = mediator;
|
||||
}
|
||||
|
||||
protected IMediator Mediator { get; }
|
||||
|
||||
protected IActionResult FromServiceResult<T>(ServiceResult<T> result) =>
|
||||
result.IsSuccess
|
||||
? Ok(new ApiResponse<T> { Success = true, Data = result.Result })
|
||||
: MapServiceError(result.Error!);
|
||||
|
||||
protected IActionResult CreatedFromServiceResult<T>(ServiceResult<Guid> result, string actionName, Func<Guid, T> routeValues) =>
|
||||
result.IsSuccess
|
||||
? CreatedAtAction(actionName, routeValues(result.Result), new ApiResponse<Guid> { Success = true, Data = result.Result })
|
||||
: MapServiceError(result.Error!);
|
||||
|
||||
protected IActionResult MapServiceError(ServiceError error)
|
||||
{
|
||||
string code = error.Code?.ToString() ?? "Unknown";
|
||||
|
||||
return error.Code switch
|
||||
{
|
||||
ServiceErrorCode.NotFound => NotFound(ErrorResponse(code, error.Message)),
|
||||
ServiceErrorCode.Conflict => Conflict(ErrorResponse(code, error.Message)),
|
||||
ServiceErrorCode.InvalidRequest => BadRequest(ErrorResponse(code, error.Message)),
|
||||
null => throw new InvalidOperationException("Error code was null for failed result."),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(error), error, "Unexpected error code.")
|
||||
};
|
||||
}
|
||||
|
||||
private static ApiResponse<object> ErrorResponse(string code, string message) =>
|
||||
new() { Success = false, Error = new ApiError { Code = code, Message = message } };
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.EmailChannels;
|
||||
|
||||
public sealed record CreateEmailChannelRequest(
|
||||
string ServiceName,
|
||||
int Priority,
|
||||
EmailChannelType ChannelType,
|
||||
EmailChannelSettings Settings,
|
||||
int? DailyLimit,
|
||||
int? MonthlyLimit,
|
||||
int WarnThresholdPercent,
|
||||
bool IsActive
|
||||
);
|
||||
@@ -0,0 +1,77 @@
|
||||
using HrynCo.NotificationService.Web.Infrastructure;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Create;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Delete;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Get;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.GetByService;
|
||||
using HrynCo.NotificationService.Services.EmailChannels.Update;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.EmailChannels;
|
||||
|
||||
[Route("api/v1/email-channels")]
|
||||
public sealed class EmailChannelsController : ApiControllerBase
|
||||
{
|
||||
public EmailChannelsController(IMediator mediator) : base(mediator) { }
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetAll([FromQuery] string serviceName, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await Mediator.Send(new GetEmailChannelsQuery(serviceName), cancellationToken);
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
|
||||
[HttpGet("{id:guid}")]
|
||||
public async Task<IActionResult> Get(Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await Mediator.Send(new GetEmailChannelQuery(id), cancellationToken);
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create([FromBody] CreateEmailChannelRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var command = new CreateEmailChannelCommand(
|
||||
request.ServiceName,
|
||||
request.Priority,
|
||||
request.ChannelType,
|
||||
request.Settings,
|
||||
request.DailyLimit,
|
||||
request.MonthlyLimit,
|
||||
request.WarnThresholdPercent,
|
||||
request.IsActive
|
||||
);
|
||||
|
||||
var result = await Mediator.Send(command, cancellationToken);
|
||||
|
||||
if (!result.IsSuccess)
|
||||
return MapServiceError(result.Error!);
|
||||
|
||||
return CreatedAtAction(nameof(Get), new { id = result.Result },
|
||||
new ApiResponse<Guid> { Success = true, Data = result.Result });
|
||||
}
|
||||
|
||||
[HttpPut("{id:guid}")]
|
||||
public async Task<IActionResult> Update(Guid id, [FromBody] UpdateEmailChannelRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var command = new UpdateEmailChannelCommand(
|
||||
id,
|
||||
request.Priority,
|
||||
request.Settings,
|
||||
request.DailyLimit,
|
||||
request.MonthlyLimit,
|
||||
request.WarnThresholdPercent,
|
||||
request.IsActive
|
||||
);
|
||||
|
||||
var result = await Mediator.Send(command, cancellationToken);
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
|
||||
[HttpDelete("{id:guid}")]
|
||||
public async Task<IActionResult> Delete(Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await Mediator.Send(new DeleteEmailChannelCommand(id), cancellationToken);
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Providers;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.EmailChannels;
|
||||
|
||||
public sealed record UpdateEmailChannelRequest(
|
||||
int Priority,
|
||||
EmailChannelSettings Settings,
|
||||
int? DailyLimit,
|
||||
int? MonthlyLimit,
|
||||
int WarnThresholdPercent,
|
||||
bool IsActive
|
||||
);
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.EmailTemplates;
|
||||
|
||||
public sealed record CreateEmailTemplateRequest(
|
||||
string ServiceName,
|
||||
string Key,
|
||||
string LanguageCode,
|
||||
string Subject,
|
||||
string HtmlBody,
|
||||
string TextBody,
|
||||
IReadOnlyList<EmailTemplateVariable> Variables
|
||||
);
|
||||
@@ -0,0 +1,84 @@
|
||||
using HrynCo.NotificationService.Web.Infrastructure;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.Create;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.Delete;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.Get;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.GetByService;
|
||||
using HrynCo.NotificationService.Services.EmailTemplates.Update;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.EmailTemplates;
|
||||
|
||||
[Route("api/v1/email-templates")]
|
||||
public sealed class EmailTemplatesController : ApiControllerBase
|
||||
{
|
||||
public EmailTemplatesController(IMediator mediator) : base(mediator) { }
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetAll([FromQuery] string serviceName, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await Mediator.Send(new GetEmailTemplatesQuery(serviceName), cancellationToken);
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
|
||||
[HttpGet("{serviceName}/{key}/{languageCode}")]
|
||||
public async Task<IActionResult> Get(string serviceName, string key, string languageCode, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await Mediator.Send(new GetEmailTemplateQuery(serviceName, key, languageCode), cancellationToken);
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create([FromBody] CreateEmailTemplateRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var command = new CreateEmailTemplateCommand(
|
||||
request.ServiceName,
|
||||
request.Key,
|
||||
request.LanguageCode,
|
||||
request.Subject,
|
||||
request.HtmlBody,
|
||||
request.TextBody,
|
||||
request.Variables
|
||||
);
|
||||
|
||||
var result = await Mediator.Send(command, cancellationToken);
|
||||
|
||||
if (!result.IsSuccess)
|
||||
return MapServiceError(result.Error!);
|
||||
|
||||
return CreatedAtAction(
|
||||
nameof(Get),
|
||||
new { serviceName = request.ServiceName, key = request.Key, languageCode = request.LanguageCode },
|
||||
new ApiResponse<Guid> { Success = true, Data = result.Result }
|
||||
);
|
||||
}
|
||||
|
||||
[HttpPut("{serviceName}/{key}/{languageCode}")]
|
||||
public async Task<IActionResult> Update(
|
||||
string serviceName,
|
||||
string key,
|
||||
string languageCode,
|
||||
[FromBody] UpdateEmailTemplateRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var command = new UpdateEmailTemplateCommand(
|
||||
serviceName,
|
||||
key,
|
||||
languageCode,
|
||||
request.Subject,
|
||||
request.HtmlBody,
|
||||
request.TextBody,
|
||||
request.Variables
|
||||
);
|
||||
|
||||
var result = await Mediator.Send(command, cancellationToken);
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
|
||||
[HttpDelete("{serviceName}/{key}/{languageCode}")]
|
||||
public async Task<IActionResult> Delete(string serviceName, string key, string languageCode, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = await Mediator.Send(new DeleteEmailTemplateCommand(serviceName, key, languageCode), cancellationToken);
|
||||
return FromServiceResult(result);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
using HrynCo.NotificationService.DAL.Abstract.Templates;
|
||||
|
||||
namespace HrynCo.NotificationService.Web.Controllers.EmailTemplates;
|
||||
|
||||
public sealed record UpdateEmailTemplateRequest(
|
||||
string Subject,
|
||||
string HtmlBody,
|
||||
string TextBody,
|
||||
IReadOnlyList<EmailTemplateVariable> Variables
|
||||
);
|
||||
@@ -0,0 +1,32 @@
|
||||
# syntax=docker/dockerfile:1.4
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS base
|
||||
WORKDIR /app
|
||||
EXPOSE 8080
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
|
||||
COPY ["Directory.Build.props", "."]
|
||||
COPY ["Directory.Packages.props", "."]
|
||||
COPY ["HrynCo.NotificationService.DAL.Abstract/HrynCo.NotificationService.DAL.Abstract.csproj", "HrynCo.NotificationService.DAL.Abstract/"]
|
||||
COPY ["HrynCo.NotificationService.DAL.EF/HrynCo.NotificationService.DAL.EF.csproj", "HrynCo.NotificationService.DAL.EF/"]
|
||||
COPY ["HrynCo.NotificationService.Services/HrynCo.NotificationService.Services.csproj", "HrynCo.NotificationService.Services/"]
|
||||
COPY ["HrynCo.NotificationService.Api/HrynCo.NotificationService.Api.csproj", "HrynCo.NotificationService.Api/"]
|
||||
|
||||
RUN dotnet restore "HrynCo.NotificationService.Api/HrynCo.NotificationService.Api.csproj"
|
||||
|
||||
COPY . .
|
||||
|
||||
WORKDIR "/src/HrynCo.NotificationService.Api"
|
||||
RUN dotnet build "./HrynCo.NotificationService.Api.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "./HrynCo.NotificationService.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "HrynCo.NotificationService.Api.dll"]
|
||||
@@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
|
||||
<PackageReference Include="Scalar.AspNetCore" />
|
||||
<PackageReference Include="Serilog.AspNetCore" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" />
|
||||
<PackageReference Include="Serilog.Sinks.Seq" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\HrynCo.NotificationService.Services\HrynCo.NotificationService.Services.csproj" />
|
||||
<ProjectReference Include="..\HrynCo.NotificationService.DAL.EF\HrynCo.NotificationService.DAL.EF.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,6 @@
|
||||
@HrynCo.NotificationService.Api_HostAddress = http://localhost:5188
|
||||
|
||||
GET {{HrynCo.NotificationService.Api_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace HrynCo.NotificationService.Web.Infrastructure;
|
||||
|
||||
public sealed class ApiResponse<T>
|
||||
{
|
||||
public T? Data { get; init; } = default;
|
||||
public ApiError? Error { get; init; }
|
||||
public bool Success { get; init; }
|
||||
}
|
||||
|
||||
public sealed class ApiError
|
||||
{
|
||||
public required string Code { get; init; }
|
||||
public required string Message { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using HrynCo.NotificationService.Web;
|
||||
using HrynCo.NotificationService.DAL.EF;
|
||||
using HrynCo.NotificationService.Services;
|
||||
using Scalar.AspNetCore;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.AddSerilog();
|
||||
|
||||
var appSettings = builder.Configuration
|
||||
.GetSection(AppSettings.SectionName)
|
||||
.Get<AppSettings>() ?? throw new InvalidOperationException("App settings are not configured.");
|
||||
|
||||
builder.Services.AddSingleton(appSettings);
|
||||
builder.Services.AddOpenApi();
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddNotificationDataAccess(appSettings.ConnectionString);
|
||||
builder.Services.AddNotificationServices();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
app.MapScalarApiReference(options =>
|
||||
{
|
||||
options.Title = "HrynCo Notification Service";
|
||||
options.Theme = ScalarTheme.DeepSpace;
|
||||
});
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.MapControllers();
|
||||
app.Run();
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:5188",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "https://localhost:7210;http://localhost:5188",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using Serilog;
|
||||
|
||||
namespace HrynCo.NotificationService.Web;
|
||||
|
||||
public static class SerilogRegistrar
|
||||
{
|
||||
public static void AddSerilog(this WebApplicationBuilder builder)
|
||||
{
|
||||
var loggerConfiguration = new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(builder.Configuration)
|
||||
.Enrich.FromLogContext();
|
||||
|
||||
Log.Logger = loggerConfiguration.CreateLogger();
|
||||
|
||||
builder.Logging.AddSerilog(Log.Logger);
|
||||
builder.Host.UseSerilog();
|
||||
|
||||
builder.Services.AddSingleton(Log.Logger);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"MinimumLevel": {
|
||||
"Default": "Debug",
|
||||
"Override": {
|
||||
"Microsoft": "Information",
|
||||
"Microsoft.EntityFrameworkCore": "Information",
|
||||
"Microsoft.AspNetCore": "Information"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"App": {
|
||||
"ConnectionString": "Host=localhost;Port=5432;Database=notification_service;Username=postgres;Password=postgres"
|
||||
},
|
||||
"Serilog": {
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Override": {
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.EntityFrameworkCore": "Warning",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"WriteTo": [
|
||||
{ "Name": "Console" },
|
||||
{
|
||||
"Name": "Seq",
|
||||
"Args": {
|
||||
"serverUrl": "http://localhost:5341"
|
||||
}
|
||||
}
|
||||
],
|
||||
"Enrich": [ "FromLogContext" ]
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user