Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions src/RazorPagesProject/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Localization;
Expand Down Expand Up @@ -47,6 +48,7 @@
});

builder.Services.AddScoped<IQuoteService, QuoteService>();
builder.Services.AddScoped<IMessageSearchService, MessageSearchService>();

var app = builder.Build();

Expand All @@ -72,6 +74,16 @@
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.Run();

static void SeedDatabase(WebApplication app)
Expand Down
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);
}
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();
}
}