Skip to content

Commit

Permalink
Merge pull request #107 from TomaszKandula/dev
Browse files Browse the repository at this point in the history
merge: dev to stage
  • Loading branch information
TomaszKandula authored Jan 16, 2022
2 parents 8154575 + c6d44cd commit b0c6250
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public override async Task<GetEmailsHistoryQueryResult> Handle(GetEmailsHistoryQ
.Where(user => user.Id == userId)
.FirstOrDefaultAsync(cancellationToken);

var wording = history.Count == 1 ? "entry" : "entries";
_loggerService.LogInformation($"Found {history.Count} history {wording} for requested user");
return new GetEmailsHistoryQueryResult
{
AssociatedUser = associatedUser.UserAlias,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace EmailSender.Backend.Cqrs.Handlers.Queries.Logger;

using Microsoft.AspNetCore.Mvc;
using MediatR;

public class GetLogFileContentQuery : IRequest<FileContentResult>
{
public string LogFileName { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace EmailSender.Backend.Cqrs.Handlers.Queries.Logger;

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Core.Exceptions;
using Shared.Resources;

public class GetLogFileContentQueryHandler : RequestHandler<GetLogFileContentQuery, FileContentResult>
{
public override async Task<FileContentResult> Handle(GetLogFileContentQuery request, CancellationToken cancellationToken)
{
var pathToFolder = $"{AppDomain.CurrentDomain.BaseDirectory}logs";
if (!Directory.Exists(pathToFolder))
throw new BusinessException(nameof(ErrorCodes.ERROR_UNEXPECTED), ErrorCodes.ERROR_UNEXPECTED);

var fullFilePath = $"{pathToFolder}{Path.DirectorySeparatorChar}{request.LogFileName}";
if (!File.Exists(fullFilePath))
throw new BusinessException(nameof(ErrorCodes.FILE_NOT_FOUND), ErrorCodes.FILE_NOT_FOUND);

var fileContent = await File.ReadAllBytesAsync(fullFilePath, cancellationToken);
return new FileContentResult(fileContent, "text/plain");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace EmailSender.Backend.Cqrs.Handlers.Queries.Logger;

using FluentValidation;
using Shared.Resources;

public class GetLogFileContentQueryValidator : AbstractValidator<GetLogFileContentQuery>
{
public GetLogFileContentQueryValidator()
{
RuleFor(query => query.LogFileName)
.NotEmpty()
.WithErrorCode(nameof(ValidationCodes.REQUIRED))
.WithMessage(ValidationCodes.REQUIRED)
.MaximumLength(255)
.WithErrorCode(nameof(ValidationCodes.NAME_TOO_LONG))
.WithMessage(ValidationCodes.NAME_TOO_LONG);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace EmailSender.Backend.Cqrs.Handlers.Queries.Logger;

using MediatR;

public class GetLogFilesListQuery : IRequest<GetLogFilesListQueryResult> { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace EmailSender.Backend.Cqrs.Handlers.Queries.Logger;

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;

public class GetLogFilesListQueryHandler : RequestHandler<GetLogFilesListQuery, GetLogFilesListQueryResult>
{
public override async Task<GetLogFilesListQueryResult> Handle(GetLogFilesListQuery request, CancellationToken cancellationToken)
{
var pathToFolder = $"{AppDomain.CurrentDomain.BaseDirectory}logs";
if (!Directory.Exists(pathToFolder))
return new GetLogFilesListQueryResult();

var fullPathFileList = Directory.EnumerateFiles(pathToFolder, "*.txt", SearchOption.TopDirectoryOnly);
var logFiles = new List<string>();

foreach (var item in fullPathFileList)
{
logFiles.Add(Path.GetFileName(item));
}

return await Task.FromResult(new GetLogFilesListQueryResult
{
LogFiles = logFiles
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace EmailSender.Backend.Cqrs.Handlers.Queries.Logger;

using System.Collections.Generic;

public class GetLogFilesListQueryResult
{
public List<string> LogFiles { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public override async Task<GetUserDomainsQueryResult> Handle(GetUserDomainsQuery
.Select(allowDomain => allowDomain.Host)
.ToListAsync(cancellationToken);

_loggerService.LogInformation($"Found {hosts.Count} host(s) for requested user");
return new GetUserDomainsQueryResult
{
Hosts = hosts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public override async Task<GetUserEmailsQueryResult> Handle(GetUserEmailsQuery r
.Where(user => user.Id == userId)
.FirstOrDefaultAsync(cancellationToken);

_loggerService.LogInformation($"Found {emails.Count} email(s) for requested user");
return new GetUserEmailsQueryResult
{
AssociatedUser = associatedUser.UserAlias,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@
<data name="INVALID_API_VERSION" xml:space="preserve">
<value>Provided API version seems to be invalid</value>
</data>
<data name="FILE_NOT_FOUND" xml:space="preserve">
<value>Cannot find file</value>
</data>
</root>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@
<data name="INVALID_EMAIL_ADDRESS" xml:space="preserve">
<value>Invalid email address</value>
</data>
<data name="NAME_TOO_LONG" xml:space="preserve">
<value>The name is too long</value>
</data>
</root>
23 changes: 23 additions & 0 deletions EmailSender.WebApi/Controllers/LoggerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace EmailSender.WebApi.Controllers;

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Backend.Cqrs.Handlers.Queries.Logger;
using MediatR;

[ApiVersion("1.0")]
public class LoggerController : BaseController
{
public LoggerController(IMediator mediator) : base(mediator) { }

[HttpGet]
[ProducesResponseType(typeof(GetLogFilesListQueryResult), StatusCodes.Status200OK)]
public async Task<GetLogFilesListQueryResult> GetLogFilesList([FromHeader(Name = HeaderName)] string privateKey)
=> await Mediator.Send(new GetLogFilesListQuery());

[HttpGet("{fileName}")]
[ProducesResponseType(typeof(IActionResult), StatusCodes.Status200OK)]
public async Task<IActionResult> GetLogFileContent([FromRoute] string fileName, [FromHeader(Name = HeaderName)] string privateKey)
=> await Mediator.Send(new GetLogFileContentQuery { LogFileName = fileName });
}
9 changes: 8 additions & 1 deletion EmailSender.WebApi/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace EmailSender.WebApi;

using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -56,7 +57,13 @@ public void Configure(IApplicationBuilder builder)

builder.UseResponseCompression();
builder.UseRouting();
builder.UseEndpoints(endpoints => endpoints.MapControllers());
builder.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapGet("/", context
=> context.Response.WriteAsync("Email Sender API"));
});

builder.SetupSwaggerUi(_environment);
}
}

0 comments on commit b0c6250

Please sign in to comment.