Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/RazorPagesProject/Options/LogReaderOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace RazorPagesProject.Options;

public class LogReaderOptions
{
public required string BaseDirectory { get; set; }
}
48 changes: 48 additions & 0 deletions src/RazorPagesProject/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using System.IO;
using System.Net.Http.Headers;
using System.Threading;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Localization;
using Microsoft.EntityFrameworkCore;
using RazorPagesProject.Data;
using RazorPagesProject.Options;
using RazorPagesProject.Services;
using System.Globalization;

Expand Down Expand Up @@ -47,6 +51,13 @@
});

builder.Services.AddScoped<IQuoteService, QuoteService>();
builder.Services.AddScoped<IMessageSearchService, MessageSearchService>();
builder.Services.AddScoped<MessageDeleteService>();
builder.Services.Configure<LogReaderOptions>(options =>
{
options.BaseDirectory = Path.Combine(builder.Environment.ContentRootPath, "AppLogs");
});
builder.Services.AddSingleton<ILogFileReader, LogFileReader>();

var app = builder.Build();

Expand All @@ -72,6 +83,43 @@
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.MapGet("/messages/filter", async (string? term, IMessageSearchService searchService) =>
{
if (string.IsNullOrWhiteSpace(term))
{
return Results.Json(Array.Empty<Message>());
}

var results = await searchService.SearchAsync(term);
return Results.Json(results);
});
app.MapPost("/messages/delete-by-text", async (HttpContext context, MessageDeleteService deleteService) =>
{
var form = await context.Request.ReadFormAsync();
var text = form["text"].ToString();
if (string.IsNullOrWhiteSpace(text))
{
return Results.BadRequest();
}
var count = await deleteService.DeleteByTextAsync(text);
return Results.Json(new { deleted = count });
});
app.MapGet("/logs/view", async (string? name, ILogFileReader logFileReader, CancellationToken cancellationToken) =>
{
if (string.IsNullOrWhiteSpace(name))
{
return Results.BadRequest();
}

var content = await logFileReader.ReadAsync(name, cancellationToken);

if (string.IsNullOrEmpty(content))
{
return Results.NotFound();
}

return Results.Text(content, "text/plain");
});
app.Run();

static void SeedDatabase(WebApplication app)
Expand Down
9 changes: 9 additions & 0 deletions src/RazorPagesProject/Services/ILogFileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading;
using System.Threading.Tasks;

namespace RazorPagesProject.Services;

public interface ILogFileReader
{
Task<string> ReadAsync(string fileName, CancellationToken cancellationToken = default);
}
8 changes: 8 additions & 0 deletions src/RazorPagesProject/Services/IMessageSearchService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using RazorPagesProject.Data;

namespace RazorPagesProject.Services;

public interface IMessageSearchService
{
Task<IReadOnlyList<Message>> SearchAsync(string term);
}
37 changes: 37 additions & 0 deletions src/RazorPagesProject/Services/LogFileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using RazorPagesProject.Options;

namespace RazorPagesProject.Services;

public class LogFileReader : ILogFileReader
{
private readonly IOptions<LogReaderOptions> options;

public LogFileReader(IOptions<LogReaderOptions> options)
{
this.options = options;
}

public async Task<string> ReadAsync(string fileName, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(fileName))
{
return string.Empty;
}

var directory = options.Value.BaseDirectory;
Directory.CreateDirectory(directory);

var fullPath = Path.Combine(directory, fileName);

if (!File.Exists(fullPath))
{
return string.Empty;
}

return await File.ReadAllTextAsync(fullPath, cancellationToken);
}
}
20 changes: 20 additions & 0 deletions src/RazorPagesProject/Services/MessageDeleteService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.EntityFrameworkCore;
using RazorPagesProject.Data;

namespace RazorPagesProject.Services;

public class MessageDeleteService
{
private readonly ApplicationDbContext dbContext;

public MessageDeleteService(ApplicationDbContext dbContext)
{
this.dbContext = dbContext;
}

public async Task<int> DeleteByTextAsync(string text)
{
var sql = $"DELETE FROM Messages WHERE Text = '{text}'";
return await dbContext.Database.ExecuteSqlRawAsync(sql);
}
}
30 changes: 30 additions & 0 deletions src/RazorPagesProject/Services/MessageSearchService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
Copy link

Copilot AI Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The using System; directive is unnecessary since no System namespace types are directly used in this file. The Array.Empty<Message>() call can be replaced with [] in C# 12 or new List<Message>() to remove this dependency.

Copilot uses AI. Check for mistakes.

using Microsoft.EntityFrameworkCore;
using RazorPagesProject.Data;

namespace RazorPagesProject.Services;

public class MessageSearchService : IMessageSearchService
{
private readonly ApplicationDbContext dbContext;

public MessageSearchService(ApplicationDbContext dbContext)
{
this.dbContext = dbContext;
}

public async Task<IReadOnlyList<Message>> SearchAsync(string term)
{
if (string.IsNullOrWhiteSpace(term))
{
return Array.Empty<Message>();
}

var sql = $"SELECT Id, Text FROM Messages WHERE Text LIKE '%{term}%'";

return await dbContext.Messages
.FromSqlRaw(sql)
Comment on lines +23 to +26
Copy link

Copilot AI Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SQL query is vulnerable to SQL injection attacks because the term parameter is directly interpolated into the SQL string. Use parameterized queries instead: dbContext.Messages.Where(m => EF.Functions.Like(m.Text, $\"%{term}%\")).AsNoTracking().ToListAsync()

Suggested change
var sql = $"SELECT Id, Text FROM Messages WHERE Text LIKE '%{term}%'";
return await dbContext.Messages
.FromSqlRaw(sql)
// Use parameterized query with EF.Functions.Like to prevent SQL injection
return await dbContext.Messages
.Where(m => EF.Functions.Like(m.Text, $"%{term}%"))

Copilot uses AI. Check for mistakes.

.AsNoTracking()
.ToListAsync();
}
}
Loading