Skip to content

Commit

Permalink
Account controller;
Browse files Browse the repository at this point in the history
  • Loading branch information
CuteLeon committed Sep 11, 2023
1 parent 4f20405 commit 682f284
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public class AccountController : Controller
{
private readonly ILogger<AccountController> logger;
private readonly IAccountRepository accountRepository;
private readonly IOptionsSnapshot<TradingCompetitionConfiguration> tradingCompetitionOption;
private readonly IOptionsMonitor<TradingCompetitionConfiguration> tradingCompetitionOption;
private readonly ITradingCalendar tradingCalendar;

public AccountController(
ILogger<AccountController> logger,
IAccountRepository accountRepository,
IOptionsSnapshot<TradingCompetitionConfiguration> tradingCompetitionOption,
IOptionsMonitor<TradingCompetitionConfiguration> tradingCompetitionOption,
ITradingCalendar tradingCalendar)
{
this.logger = logger;
Expand All @@ -36,22 +36,27 @@ public AccountController(
public async Task<ActionResult<AccountResponse>> GetAccount()
{
var userName = this.HttpContext.User?.Identity?.Name;
if(string.IsNullOrEmpty(userName))
{
return this.BadRequest("User name is empty.");
}

this.logger.LogInformation($"Get account for user: {userName}");
var account = await this.accountRepository.FindAccountByUserAsync(userName);
if (account is null)
{
account = new Account()
{
Name = userName,
InitialCapital = this.tradingCompetitionOption.Value.InitialCapital,
InitialCapital = this.tradingCompetitionOption.CurrentValue.InitialCapital,
};
await this.accountRepository.CreateAccountAsync(account);
this.logger.LogInformation($"Account created for user: {userName}, {account.Id}");
}

var date = this.tradingCalendar.GetCurrentTradingDate();
var startTime = this.tradingCompetitionOption.Value.StartTime;
var endTime = this.tradingCompetitionOption.Value.EndTime;
var startTime = this.tradingCompetitionOption.CurrentValue.StartTime;
var endTime = this.tradingCompetitionOption.CurrentValue.EndTime;
var currentTime = TimeOnly.FromDateTime(DateTime.Now);
var isTradingDate = this.tradingCalendar.IsTradingDate(date);
var isTradingTime = startTime <= currentTime && currentTime <= endTime;
Expand Down

0 comments on commit 682f284

Please sign in to comment.