176 lines
5.6 KiB
C#
176 lines
5.6 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using ChatApi.Data;
|
||
using ChatRAG.Services.ResponseService;
|
||
using ChatRAG.Services;
|
||
using ChatRAG.Data;
|
||
using ChatRAG.Models;
|
||
using ChatRAG.Requests;
|
||
using BlazMapper;
|
||
using ChatRAG.Services.Contracts;
|
||
|
||
#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 ProjectDataRepository _projectDataRepository;
|
||
private readonly ITextDataService _textDataService;
|
||
private readonly IHttpClientFactory _httpClientFactory;
|
||
|
||
public ChatController(
|
||
ILogger<ChatController> logger,
|
||
IResponseService responseService,
|
||
UserDataRepository userDataRepository,
|
||
ITextDataService textDataService,
|
||
ProjectDataRepository projectDataRepository,
|
||
IHttpClientFactory httpClientFactory)
|
||
{
|
||
_logger = logger;
|
||
_responseService = responseService;
|
||
_userDataRepository = userDataRepository;
|
||
_textDataService = textDataService;
|
||
_projectDataRepository = projectDataRepository;
|
||
_httpClientFactory = httpClientFactory;
|
||
}
|
||
|
||
[HttpPost]
|
||
[Route("response")]
|
||
public async Task<IActionResult> GetResponse([FromForm] ChatRequest chatRequest)
|
||
{
|
||
try
|
||
{
|
||
var userData = await _userDataRepository.GeByTokentAsync(chatRequest.SessionId);
|
||
var response = await _responseService.GetResponse(userData, chatRequest.ProjectId, chatRequest.SessionId, chatRequest.Message);
|
||
return Ok(response);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return StatusCode(500, ex.Message);
|
||
}
|
||
}
|
||
|
||
|
||
[HttpPost]
|
||
[Route("savegroup")]
|
||
public async Task SaveSingleProject([FromBody] ProjectRequest project)
|
||
{
|
||
var projectSave = project.MapTo<ProjectRequest, Project>();
|
||
await _projectDataRepository.SaveAsync(projectSave);
|
||
}
|
||
|
||
[HttpGet]
|
||
[Route("groups")]
|
||
public async Task<IEnumerable<ProjectRequest>> GetProjects()
|
||
{
|
||
var projects = await _projectDataRepository.GetAsync();
|
||
return projects.Select(s => s.MapTo<Project, ProjectRequest>());
|
||
}
|
||
|
||
|
||
[HttpPost]
|
||
[Route("savetext")]
|
||
public async Task<IActionResult> SaveSingleText([FromBody] TextRequest request)
|
||
{
|
||
try
|
||
{
|
||
await _textDataService.SaveDocumentAsync(new DocumentInput
|
||
{
|
||
Id = request.Id,
|
||
Title = request.Title,
|
||
Content = request.Content,
|
||
ProjectId = request.ProjectId
|
||
});
|
||
return Created();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return StatusCode(500, ex.Message);
|
||
}
|
||
}
|
||
|
||
[HttpPost]
|
||
[Route("savetexts")]
|
||
public async Task<IActionResult> SaveTexts([FromBody] List<TextRequest> requests)
|
||
{
|
||
try
|
||
{
|
||
foreach(var request in requests)
|
||
{
|
||
await _textDataService.SaveDocumentAsync(new DocumentInput
|
||
{
|
||
Id = request.Id,
|
||
Title = request.Title,
|
||
Content = request.Content,
|
||
ProjectId = request.ProjectId
|
||
});
|
||
}
|
||
return Created();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return StatusCode(500, ex.Message);
|
||
}
|
||
}
|
||
|
||
[HttpGet]
|
||
[Route("texts")]
|
||
public async Task<IEnumerable<TextResponse>> GetTexts(string groupId)
|
||
{
|
||
var texts = await _textDataService.GetByPorjectId(groupId);
|
||
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 _textDataService.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 ProjectId { 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.
|