TelegramDbContext - async

This commit is contained in:
Anton Suhorukov 2023-03-17 11:47:17 +03:00
parent 7d6feae7cd
commit dde9cac96e
5 changed files with 29 additions and 29 deletions

View File

@ -37,7 +37,7 @@ public class TelegramDao
_dbContextFactory = dbContextFactory; _dbContextFactory = dbContextFactory;
} }
public void RegisterUser(Guid userId, int tenantId, long telegramId) public async Task RegisterUserAsync(Guid userId, int tenantId, long telegramId)
{ {
var user = new TelegramUser var user = new TelegramUser
{ {
@ -46,43 +46,43 @@ public class TelegramDao
TelegramUserId = telegramId TelegramUserId = telegramId
}; };
using var dbContext = _dbContextFactory.CreateDbContext(); using var dbContext = await _dbContextFactory.CreateDbContextAsync();
dbContext.AddOrUpdate(dbContext.Users, user); await dbContext.AddOrUpdateAsync(q => q.Users, user);
dbContext.SaveChanges(); await dbContext.SaveChangesAsync();
} }
public TelegramUser GetUser(Guid userId, int tenantId) public async Task<TelegramUser> GetUserAsync(Guid userId, int tenantId)
{ {
using var dbContext = _dbContextFactory.CreateDbContext(); using var dbContext = await _dbContextFactory.CreateDbContextAsync();
return dbContext.Users.Find(tenantId, userId); return await dbContext.Users.FindAsync(tenantId, userId);
} }
public List<TelegramUser> GetUser(long telegramId) public async List<TelegramUser> GetUsersAsync(long telegramId)
{ {
using var dbContext = _dbContextFactory.CreateDbContext(); using var dbContext = await _dbContextFactory.CreateDbContextAsync();
return dbContext.Users return await dbContext.Users
.AsNoTracking() .AsNoTracking()
.Where(r => r.TelegramUserId == telegramId) .Where(r => r.TelegramUserId == telegramId)
.ToList(); .ToListAsync();
} }
public void Delete(Guid userId, int tenantId) public async Task DeleteAsync(Guid userId, int tenantId)
{ {
using var dbContext = _dbContextFactory.CreateDbContext(); using var dbContext = await _dbContextFactory.CreateDbContextAsync();
dbContext.Users await dbContext.Users
.Where(r => r.PortalUserId == userId) .Where(r => r.PortalUserId == userId)
.Where(r => r.TenantId == tenantId) .Where(r => r.TenantId == tenantId)
.ExecuteDelete(); .ExecuteDeleteAsync();
} }
public void Delete(long telegramId) public async void DeleteAsync(long telegramId)
{ {
using var dbContext = _dbContextFactory.CreateDbContext(); using var dbContext = await _dbContextFactory.CreateDbContextAsync();
dbContext.Users await dbContext.Users
.Where(r => r.TelegramUserId == telegramId) .Where(r => r.TelegramUserId == telegramId)
.ExecuteDelete(); .ExecuteDeleteAsync();
} }
} }

View File

@ -85,9 +85,9 @@ public class TelegramHelper
} }
} }
public RegStatus UserIsConnected(Guid userId, int tenantId) public async Task<RegStatus> UserIsConnectedAsync(Guid userId, int tenantId)
{ {
if (_telegramDao.GetUser(userId, tenantId) != null) if (await _telegramDao.GetUserAsync(userId, tenantId) != null)
{ {
return RegStatus.Registered; return RegStatus.Registered;
} }
@ -111,9 +111,9 @@ public class TelegramHelper
_telegramServiceClient.DisableClient(tenantId); _telegramServiceClient.DisableClient(tenantId);
} }
public void Disconnect(Guid userId, int tenantId) public async Task DisconnectAsync(Guid userId, int tenantId)
{ {
_telegramDao.Delete(userId, tenantId); await _telegramDao.DeleteAsync(userId, tenantId);
} }
private bool IsAwaitingRegistration(Guid userId, int tenantId) private bool IsAwaitingRegistration(Guid userId, int tenantId)

View File

@ -68,7 +68,7 @@ public class UserCommands : CommandContext
if (tenantId == TenantId) if (tenantId == TenantId)
{ {
_telegramDao.RegisterUser(portalUserId, tenantId, telegramUserId); await _telegramDao.RegisterUserAsync(portalUserId, tenantId, telegramUserId);
await ReplyAsync("Ok!"); await ReplyAsync("Ok!");

View File

@ -69,7 +69,7 @@ public class TelegramHandler
try try
{ {
var tgUser = telegramDao.GetUser(Guid.Parse(msg.Reciever), msg.TenantId); var tgUser = await telegramDao.GetUserAsync(Guid.Parse(msg.Reciever), msg.TenantId);
if (tgUser == null) if (tgUser == null)
{ {

View File

@ -790,17 +790,17 @@ public class SettingsController : BaseSettingsController
/// </summary> /// </summary>
/// <returns>0 - not connected, 1 - connected, 2 - awaiting confirmation</returns> /// <returns>0 - not connected, 1 - connected, 2 - awaiting confirmation</returns>
[HttpGet("telegramisconnected")] [HttpGet("telegramisconnected")]
public object TelegramIsConnected() public async Task<object> TelegramIsConnectedAsync()
{ {
return (int)_telegramHelper.UserIsConnected(_authContext.CurrentAccount.ID, Tenant.Id); return (int) await _telegramHelper.UserIsConnectedAsync(_authContext.CurrentAccount.ID, Tenant.Id);
} }
/// <summary> /// <summary>
/// Unlinks TelegramBot from your account /// Unlinks TelegramBot from your account
/// </summary> /// </summary>
[HttpDelete("telegramdisconnect")] [HttpDelete("telegramdisconnect")]
public void TelegramDisconnect() public async Task TelegramDisconnectAsync()
{ {
_telegramHelper.Disconnect(_authContext.CurrentAccount.ID, Tenant.Id); await _telegramHelper.DisconnectAsync(_authContext.CurrentAccount.ID, Tenant.Id);
} }
} }