You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

74 lines
2.3 KiB

using BlazorFluentUI.Models;
namespace BlazorFluentUI.Services
{
public class CountryService
{
private readonly List<Country> _countries = new List<Country>();
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
private readonly Random _random = new Random();
private readonly string[] _countryNames = { "Korea", "USA", "Canada", "Mexico", "Brazil", "UK", "Germany", "France", "Italy", "China", "Japan", "India", "Australia", "Russia", "Spain", "South Africa", "New Zealand", "Argentina", "Egypt", "Thailand", "Sweden", "Norway", "Denmark", "Finland", "Netherlands", "Belgium" };
public CountryService()
{
var randomCountryNames = _countryNames.OrderBy(x => Guid.NewGuid()).ToArray();
for (int i = 0; i < randomCountryNames.Length; i++)
{
var country = new Country
{
Id = i + 1,
Name = randomCountryNames[i],
};
_countries.Add(country);
}
}
public async Task<Country> GetCountryAsync(int id)
{
await _semaphore.WaitAsync();
try
{
return _countries.FirstOrDefault(c => c.Id == id);
}
finally
{
_semaphore.Release();
}
}
public async Task<IEnumerable<Country>> GetAllCountriesAsync()
{
await _semaphore.WaitAsync();
try
{
return _countries.ToList();
}
finally
{
_semaphore.Release();
}
}
//public async Task GenerateRandomCountriesAsync(int count)
//{
// await _semaphore.WaitAsync();
// try
// {
// for (int i = 0; i < count; i++)
// {
// var country = new Country
// {
// Id = _countries.Any() ? _countries.Max(c => c.Id) + 1 : 1,
// Name = _countryNames[_random.Next(_countryNames.Length)],
// };
// _countries.Add(country);
// }
// }
// finally
// {
// _semaphore.Release();
// }
//}
}
}