-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathAddAccountsCommand.cs
More file actions
59 lines (50 loc) · 1.76 KB
/
AddAccountsCommand.cs
File metadata and controls
59 lines (50 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
namespace MainCore.Commands.UI.AddAccountsViewModel
{
[Handler]
public static partial class AddAccountsCommand
{
public sealed record Command(List<AccountDto> Dtos) : ICommand;
private static async ValueTask<Result> HandleAsync(
Command command,
AppDbContext context,
IUseragentManager useragentManager,
IRxQueue rxQueue
)
{
await Task.CompletedTask;
var existAccounts = context.Accounts
.ToDto()
.ToList();
var dtos = command.Dtos
.Where(dto => !existAccounts.Exists(x => x.Username == dto.Username && x.Server == dto.Server))
.ToList();
if (dtos.Count == 0)
{
return Result.Fail("All accounts are duplicated");
}
var accounts = dtos
.Select(x => x.ToEntity());
foreach (var access in accounts.SelectMany(x => x.Accesses).Where(access => string.IsNullOrEmpty(access.Useragent)))
{
access.Useragent = useragentManager.Get();
}
foreach (var account in accounts)
{
account.Info = new();
account.Settings = [];
foreach (var (setting, value) in AppDbContext.AccountDefaultSettings)
{
account.Settings.Add(new AccountSetting
{
Setting = setting,
Value = value,
});
}
context.Add(account);
}
context.SaveChanges();
rxQueue.Enqueue(new AccountsModified());
return Result.Ok();
}
}
}