-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathUpdateAccountInfoCommand.cs
More file actions
53 lines (47 loc) · 1.55 KB
/
Copy pathUpdateAccountInfoCommand.cs
File metadata and controls
53 lines (47 loc) · 1.55 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
namespace MainCore.Commands.Update
{
[Handler]
public static partial class UpdateAccountInfoCommand
{
public sealed record Command(AccountId AccountId) : IAccountCommand;
private static async ValueTask HandleAsync(
Command command,
IChromeBrowser browser,
AppDbContext context
)
{
await Task.CompletedTask;
var dto = Get(browser.Html);
context.UpdateToDatabase(command.AccountId, dto);
}
private static AccountInfoDto Get(HtmlDocument doc)
{
var gold = InfoParser.GetGold(doc);
var silver = InfoParser.GetSilver(doc);
var hasPlusAccount = InfoParser.HasPlusAccount(doc);
return new AccountInfoDto
{
Gold = gold,
Silver = silver,
HasPlusAccount = hasPlusAccount,
Tribe = TribeEnums.Any,
};
}
private static void UpdateToDatabase(this AppDbContext context, AccountId accountId, AccountInfoDto dto)
{
var dbAccountInfo = context.AccountsInfo
.FirstOrDefault(x => x.AccountId == accountId.Value);
if (dbAccountInfo is null)
{
var accountInfo = dto.ToEntity(accountId);
context.Add(accountInfo);
}
else
{
dto.To(dbAccountInfo);
context.Update(dbAccountInfo);
}
context.SaveChanges();
}
}
}