chatbot/Chatbot.Api.Domain.Data/Repositories/BotRepository.cs

49 lines
1.3 KiB
C#
Raw Permalink Normal View History

2020-06-06 18:29:20 +03:00
using Chatbot.Api.Domain.Data.DbContexts;
using Chatbot.Api.Domain.Entities;
using Chatbot.Api.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
2020-06-06 19:21:40 +03:00
using System;
2020-06-06 18:29:20 +03:00
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();
}
2020-06-06 19:21:40 +03:00
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;
}
2020-06-06 18:29:20 +03:00
}
}