105 lines
3.4 KiB
C#
105 lines
3.4 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using ChatApi.Data;
|
||
using ChatRAG.Services.ResponseService;
|
||
using ChatRAG.Services;
|
||
|
||
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
|
||
|
||
namespace ChatApi.Controllers
|
||
{
|
||
[ApiController]
|
||
[Route("[controller]")]
|
||
public class ChatController : ControllerBase
|
||
{
|
||
private readonly ILogger<ChatController> _logger;
|
||
private readonly IResponseService _responseService;
|
||
private readonly TextFilter _textFilter;
|
||
private readonly UserDataRepository _userDataRepository;
|
||
private readonly TextData _textData;
|
||
private readonly IHttpClientFactory _httpClientFactory;
|
||
|
||
public ChatController(
|
||
ILogger<ChatController> logger,
|
||
IResponseService responseService,
|
||
UserDataRepository userDataRepository,
|
||
TextData textData,
|
||
IHttpClientFactory httpClientFactory)
|
||
{
|
||
_logger = logger;
|
||
_responseService = responseService;
|
||
_userDataRepository = userDataRepository;
|
||
_textData = textData;
|
||
this._httpClientFactory = httpClientFactory;
|
||
}
|
||
|
||
[HttpPost]
|
||
[Route("response")]
|
||
[Authorize]
|
||
public async Task<string?> GetResponse([FromForm] ChatRequest chatRequest)
|
||
{
|
||
var userData = await _userDataRepository.GeByTokentAsync(AppDomain.CurrentDomain.GetData("Token") as string);
|
||
var response = await _responseService.GetResponse(userData, chatRequest.SessionId, chatRequest.Message);
|
||
return response;
|
||
}
|
||
|
||
[HttpPost]
|
||
[Route("savetext")]
|
||
public async Task SaveSingleText([FromBody] TextRequest request)
|
||
{
|
||
await _textData.SalvarNoMongoDB(request.Id, request.Title, request.Content);
|
||
}
|
||
|
||
[HttpGet]
|
||
[Route("texts")]
|
||
public async Task<IEnumerable<TextResponse>> GetTexts()
|
||
{
|
||
var texts = await _textData.GetAll();
|
||
return texts.Select(t => {
|
||
return new TextResponse
|
||
{
|
||
Id = t.Id,
|
||
Title = t.Titulo,
|
||
Content = t.Conteudo
|
||
};
|
||
});
|
||
}
|
||
|
||
[HttpGet]
|
||
[Route("texts/id/{id}")]
|
||
public async Task<TextResponse> GetText([FromRoute] string id)
|
||
{
|
||
var textItem = await _textData.GetById(id);
|
||
|
||
return new TextResponse {
|
||
Id = textItem.Id,
|
||
Title = textItem.Titulo,
|
||
Content = textItem.Conteudo
|
||
};
|
||
}
|
||
|
||
[HttpGet]
|
||
[Route("health")]
|
||
[AllowAnonymous]
|
||
public IActionResult Health()
|
||
{
|
||
return Ok("It<49>s online.");
|
||
}
|
||
|
||
}
|
||
|
||
public class TextRequest
|
||
{
|
||
public string? Id { get; set; }
|
||
public string Title { get; set; }
|
||
public string Content { get; set; }
|
||
}
|
||
public class TextResponse
|
||
{
|
||
public string? Id { get; set; }
|
||
public string Title { get; set; }
|
||
public string Content { get; set; }
|
||
}
|
||
}
|
||
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
|