-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add IMessageSearchService and MessageSearchService for message … #518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,30 @@ | ||||||||||||||||
using System; | ||||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The SQL query is vulnerable to SQL injection attacks because the
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||
.AsNoTracking() | ||||||||||||||||
.ToListAsync(); | ||||||||||||||||
} | ||||||||||||||||
} |
There was a problem hiding this comment.
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. TheArray.Empty<Message>()
call can be replaced with[]
in C# 12 ornew List<Message>()
to remove this dependency.Copilot uses AI. Check for mistakes.