Skip to content

Instantly share code, notes, and snippets.

@Ershad95

Ershad95/api.cs Secret

Created January 28, 2023 04:58
Show Gist options
  • Save Ershad95/7dd2920e5312263b375356734078b473 to your computer and use it in GitHub Desktop.
Save Ershad95/7dd2920e5312263b375356734078b473 to your computer and use it in GitHub Desktop.
[ApiController]
[Route("[controller]")]
public class CustomerController : ControllerBase
{
private readonly IDictionary<int, Customer> _customers;
private void FillCustomerFromMemory(int countOfCustomer)
{
for (int CustomerId = 1; CustomerId <= countOfCustomer; CustomerId++)
{
_customers.Add(key: CustomerId, new Customer($"name_{CustomerId}", CustomerId));
}
}
public CustomerController()
{
_customers = new Dictionary<int, Customer>();
FillCustomerFromMemory(countOfCustomer : 100);
}
[HttpGet]
public async Task<IEnumerable<Customer>> Get()
{
var output = new List<Customer>();
while (_customers.Any(_ => _.Key % 10 == 0))
{
var customer = _customers.First(_ => _.Key % 10 == 0);
output.Add(new Customer(customer.Value.Name, customer.Key));
await Task.Delay(500);
_customers.Remove(customer);
}
return output;
}
public class Customer
{
public int Id { get; private set; }
public string Name { get; private set; }
public Customer(string name, int id)
{
Name = name;
Id = id;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment