Compare commits
2 Commits
ad730832db
...
0df4864888
Author | SHA1 | Date | |
---|---|---|---|
0df4864888 | |||
29b0ad184e |
89
DysonNetwork.Sphere/Connection/AutoCompletionController.cs
Normal file
89
DysonNetwork.Sphere/Connection/AutoCompletionController.cs
Normal file
@ -0,0 +1,89 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DysonNetwork.Sphere.Connection;
|
||||
|
||||
[ApiController]
|
||||
[Route("completion")]
|
||||
public class AutoCompletionController(AppDatabase db)
|
||||
: ControllerBase
|
||||
{
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<AutoCompletionResponse>> GetCompletions([FromBody] AutoCompletionRequest request)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(request?.Content))
|
||||
{
|
||||
return BadRequest("Content is required");
|
||||
}
|
||||
|
||||
var result = new AutoCompletionResponse();
|
||||
var lastWord = request.Content.Trim().Split(' ').LastOrDefault() ?? string.Empty;
|
||||
|
||||
if (lastWord.StartsWith("@"))
|
||||
{
|
||||
var searchTerm = lastWord[1..]; // Remove the @
|
||||
result.Items = await GetAccountCompletions(searchTerm);
|
||||
result.Type = "account";
|
||||
}
|
||||
else if (lastWord.StartsWith(":"))
|
||||
{
|
||||
var searchTerm = lastWord[1..]; // Remove the :
|
||||
result.Items = await GetStickerCompletions(searchTerm);
|
||||
result.Type = "sticker";
|
||||
}
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
private async Task<List<CompletionItem>> GetAccountCompletions(string searchTerm)
|
||||
{
|
||||
return await db.Accounts
|
||||
.Where(a => EF.Functions.ILike(a.Name, $"%{searchTerm}%"))
|
||||
.OrderBy(a => a.Name)
|
||||
.Take(10)
|
||||
.Select(a => new CompletionItem
|
||||
{
|
||||
Id = a.Id.ToString(),
|
||||
DisplayName = a.Name,
|
||||
SecondaryText = a.Nick,
|
||||
Type = "account"
|
||||
})
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
private async Task<List<CompletionItem>> GetStickerCompletions(string searchTerm)
|
||||
{
|
||||
return await db.Stickers
|
||||
.Include(s => s.Pack)
|
||||
.Where(s => EF.Functions.ILike(s.Pack.Prefix + s.Slug, $"%{searchTerm}%"))
|
||||
.OrderBy(s => s.Slug)
|
||||
.Take(10)
|
||||
.Select(s => new CompletionItem
|
||||
{
|
||||
Id = s.Id.ToString(),
|
||||
DisplayName = s.Slug,
|
||||
Type = "sticker"
|
||||
})
|
||||
.ToListAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public class AutoCompletionRequest
|
||||
{
|
||||
[Required] public string Content { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class AutoCompletionResponse
|
||||
{
|
||||
public string Type { get; set; } = string.Empty;
|
||||
public List<CompletionItem> Items { get; set; } = new();
|
||||
}
|
||||
|
||||
public class CompletionItem
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
public string DisplayName { get; set; } = string.Empty;
|
||||
public string? SecondaryText { get; set; }
|
||||
public string Type { get; set; } = string.Empty;
|
||||
}
|
@ -109,7 +109,9 @@ public class PostController(
|
||||
queryable = queryable.Where(p => p.SearchVector.Matches(EF.Functions.ToTsQuery(query)));
|
||||
else
|
||||
queryable = queryable.Where(p => EF.Functions.ILike(p.Title, $"%{query}%") ||
|
||||
EF.Functions.ILike(p.Content, $"%{query}%"));
|
||||
EF.Functions.ILike(p.Description, $"%{query}%") ||
|
||||
EF.Functions.ILike(p.Content, $"%{query}%")
|
||||
);
|
||||
|
||||
var totalCount = await queryable.CountAsync();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user