BotSharp/Platform.Articulate/Controllers/DomainController.cs

70 lines
2.1 KiB
C#
Raw Normal View History

using BotSharp.Core;
2018-09-29 18:18:34 +00:00
using BotSharp.Platform.Models;
2018-09-28 00:49:43 +00:00
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Platform.Articulate.Models;
using Platform.Articulate.ViewModels;
2018-09-28 00:49:43 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Platform.Articulate.Controllers
2018-09-28 00:49:43 +00:00
{
#if ARTICULATE
[Route("[controller]")]
public class DomainController : ControllerBase
{
2018-09-29 18:18:34 +00:00
private ArticulateAi<AgentStorageInMemory<AgentModel>, AgentModel> builder;
public DomainController()
{
builder = new ArticulateAi<AgentStorageInMemory<AgentModel>, AgentModel>();
}
2018-09-28 00:49:43 +00:00
[HttpGet("{domainId}")]
public DomainModel GetDomain([FromRoute] int domainId)
{
string dataDir = Path.Combine(AppDomain.CurrentDomain.GetData("DataPath").ToString(), "Articulate");
var dataPath = Directory.GetFiles(dataDir).FirstOrDefault(x => Regex.IsMatch(x, $"-domain-{domainId}.json"));
string json = System.IO.File.ReadAllText(dataPath);
var domain = JsonConvert.DeserializeObject<DomainModel>(json);
return domain;
}
[HttpPost]
public DomainModel PostDomain()
2018-09-28 00:49:43 +00:00
{
DomainModel domain = null;
using (var reader = new StreamReader(Request.Body))
{
string body = reader.ReadToEnd();
domain = JsonConvert.DeserializeObject<DomainModel>(body);
}
var agent = builder.GetAgentByName(domain.Agent);
2018-09-29 18:18:34 +00:00
domain.Id = Guid.NewGuid().ToString();
(agent as AgentModel).Domains.Add(domain);
builder.SaveAgent(agent);
return domain;
2018-09-28 00:49:43 +00:00
}
[HttpGet("/agent/{agentId}/domain")]
2018-09-29 18:18:34 +00:00
public DomainPageViewModel GetAgentDomains([FromRoute] string agentId, [FromQuery] int start, [FromQuery] int limit)
2018-09-28 00:49:43 +00:00
{
2018-09-29 18:18:34 +00:00
var agent = builder.GetAgentById(agentId);
2018-09-28 00:49:43 +00:00
2018-09-30 15:11:36 +00:00
return new DomainPageViewModel { Domains = agent.Domains, Total = agent.Domains.Count };
2018-09-28 00:49:43 +00:00
}
}
#endif
}