49 lines
1.3 KiB
C#
49 lines
1.3 KiB
C#
using Chatbot.Api.Domain.Data.DbContexts;
|
|
using Chatbot.Api.Domain.Entities;
|
|
using Chatbot.Api.Domain.Repositories;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Chatbot.Api.Domain.Data.Repositories
|
|
{
|
|
class BotRepository : IBotRepository
|
|
{
|
|
private readonly BotDbContext _dbContext;
|
|
|
|
public BotRepository(BotDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public async Task<Bot[]> GetBots()
|
|
{
|
|
return await _dbContext.Bots.ToArrayAsync();
|
|
}
|
|
|
|
public async Task<Guid> GetBotId(string botName)
|
|
{
|
|
var bot = await _dbContext.Bots.FirstOrDefaultAsync(z => z.BotName == botName);
|
|
if (bot != null)
|
|
return bot.BotId;
|
|
else
|
|
return await CreateBot(botName);
|
|
}
|
|
|
|
private async Task<Guid> CreateBot(string botName)
|
|
{
|
|
var id = Guid.NewGuid();
|
|
_dbContext.Add(new Bot()
|
|
{
|
|
BotId = id,
|
|
BotCode = botName.ToUpper(),
|
|
BotName = botName,
|
|
CreationDate = DateTime.Now
|
|
});
|
|
await _dbContext.SaveChangesAsync();
|
|
|
|
return id;
|
|
}
|
|
}
|
|
}
|