|
@@ -1,17 +1,20 @@
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
|
|
+using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
namespace MafiaTelegramBot.DataBase
|
|
namespace MafiaTelegramBot.DataBase
|
|
{
|
|
{
|
|
public static class UserDao
|
|
public static class UserDao
|
|
{
|
|
{
|
|
- private static readonly List<UserEntity> DataBase = new();
|
|
|
|
|
|
+ //private static readonly List<UserEntity> DataBase = new();
|
|
|
|
+ private static Mafia DataBase = new Mafia();
|
|
|
|
+
|
|
private static readonly Dictionary<long,UserEntity> ActiveUsers = new();
|
|
private static readonly Dictionary<long,UserEntity> ActiveUsers = new();
|
|
public static async Task<UserEntity> GetUserById(long id)
|
|
public static async Task<UserEntity> GetUserById(long id)
|
|
{
|
|
{
|
|
if (ActiveUsers.ContainsKey(id)) return ActiveUsers[id];
|
|
if (ActiveUsers.ContainsKey(id)) return ActiveUsers[id];
|
|
- var user = await Task.Run(()=> DataBase.First(user1 => user1.Id == id));
|
|
|
|
|
|
+ var user = await Task.Run(()=> DataBase.Users.First(user1 => user1.Id == id));
|
|
ActiveUsers.Add(user.Id, user);
|
|
ActiveUsers.Add(user.Id, user);
|
|
return user;
|
|
return user;
|
|
}
|
|
}
|
|
@@ -23,17 +26,28 @@ namespace MafiaTelegramBot.DataBase
|
|
|
|
|
|
public static async Task Update(UserEntity userEntity)
|
|
public static async Task Update(UserEntity userEntity)
|
|
{
|
|
{
|
|
- var updateIndex = await Task.Run(() => DataBase.FindIndex(user1 => user1.Id == userEntity.Id));
|
|
|
|
- if (updateIndex != -1) DataBase[updateIndex] = userEntity;
|
|
|
|
- else DataBase.Add(userEntity);
|
|
|
|
|
|
+ if (await DataBase.Users.AnyAsync(user => user.Id == userEntity.Id))
|
|
|
|
+ {
|
|
|
|
+ DataBase.Update(userEntity);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ DataBase.Add(userEntity);
|
|
|
|
+ }
|
|
|
|
+ await DataBase.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
|
|
public static async Task<bool> UserExists(long id)
|
|
public static async Task<bool> UserExists(long id)
|
|
{
|
|
{
|
|
- return await Task.Run(() =>
|
|
|
|
- {
|
|
|
|
- return ActiveUsers.ContainsKey(id) || DataBase.Exists(user => user.Id == id);
|
|
|
|
- });
|
|
|
|
|
|
+ if (ActiveUsers.ContainsKey(id))
|
|
|
|
+ return true;
|
|
|
|
+ return await DataBase.Users.AnyAsync(user => user.Id == id);
|
|
|
|
+ /*
|
|
|
|
+ return await Task.Run(() =>
|
|
|
|
+ {
|
|
|
|
+ return ActiveUsers.ContainsKey(id) || DataBase.Users.AnyAsync(user => user.Id == id);
|
|
|
|
+ });
|
|
|
|
+ */
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|