diff --git a/Dotnet.AspNetCore.Samples.WebApi.Tests/PlayerServiceTests.cs b/Dotnet.AspNetCore.Samples.WebApi.Tests/PlayerServiceTests.cs index 99ab93d..f71afd8 100644 --- a/Dotnet.AspNetCore.Samples.WebApi.Tests/PlayerServiceTests.cs +++ b/Dotnet.AspNetCore.Samples.WebApi.Tests/PlayerServiceTests.cs @@ -25,14 +25,14 @@ public async Task GivenRetrieve_WhenInvokedTwice_ThenSecondExecutionTimeShouldBe var service = new PlayerService(context.Object, logger.Object, memoryCache); // Act - var first = await ExecutionTime(() => service.Retrieve()); - var second = await ExecutionTime(() => service.Retrieve()); + var first = await ExecutionTimeAsync(() => service.RetrieveAsync()); + var second = await ExecutionTimeAsync(() => service.RetrieveAsync()); // Assert second.Should().BeLessThan(first); } - public async Task ExecutionTime(Func awaitable) + private async Task ExecutionTimeAsync(Func awaitable) { var stopwatch = new Stopwatch(); stopwatch.Start(); @@ -41,4 +41,4 @@ public async Task ExecutionTime(Func awaitable) return stopwatch.ElapsedMilliseconds; } -} \ No newline at end of file +} diff --git a/Dotnet.AspNetCore.Samples.WebApi/Controllers/PlayersController.cs b/Dotnet.AspNetCore.Samples.WebApi/Controllers/PlayersController.cs index f92c85b..ec738a9 100644 --- a/Dotnet.AspNetCore.Samples.WebApi/Controllers/PlayersController.cs +++ b/Dotnet.AspNetCore.Samples.WebApi/Controllers/PlayersController.cs @@ -44,7 +44,7 @@ public async Task> PostPlayer(Player player) } else { - await _playerService.Create(player); + await _playerService.CreateAsync(player); return CreatedAtAction(nameof(GetPlayer), new { id = player.Id }, player); } @@ -59,7 +59,7 @@ HTTP GET [HttpGet] public async Task>> GetPlayers() { - var players = await _playerService.Retrieve(); + var players = await _playerService.RetrieveAsync(); if (players.Any()) { @@ -106,7 +106,7 @@ public async Task PutPlayer(long id, Player player) } else { - await _playerService.Update(player); + await _playerService.UpdateAsync(player); return NoContent(); } @@ -127,7 +127,7 @@ public async Task DeletePlayer(long id) } else { - await _playerService.Delete(id); + await _playerService.DeleteAsync(id); return NoContent(); } diff --git a/Dotnet.AspNetCore.Samples.WebApi/Services/IPlayerService.cs b/Dotnet.AspNetCore.Samples.WebApi/Services/IPlayerService.cs index 50d0d6a..96d32dd 100644 --- a/Dotnet.AspNetCore.Samples.WebApi/Services/IPlayerService.cs +++ b/Dotnet.AspNetCore.Samples.WebApi/Services/IPlayerService.cs @@ -5,13 +5,13 @@ namespace Dotnet.AspNetCore.Samples.WebApi.Services; public interface IPlayerService { - public Task Create(Player player); + public Task CreateAsync(Player player); - public Task> Retrieve(); + public Task> RetrieveAsync(); public ValueTask RetrieveById(long id); - public Task Update(Player player); + public Task UpdateAsync(Player player); - public Task Delete(long id); + public Task DeleteAsync(long id); } diff --git a/Dotnet.AspNetCore.Samples.WebApi/Services/PlayerService.cs b/Dotnet.AspNetCore.Samples.WebApi/Services/PlayerService.cs index ee238e5..12e82b2 100644 --- a/Dotnet.AspNetCore.Samples.WebApi/Services/PlayerService.cs +++ b/Dotnet.AspNetCore.Samples.WebApi/Services/PlayerService.cs @@ -23,9 +23,9 @@ public PlayerService(PlayerContext playerContext, ILogger logger, Create ----------------------------------------------------------------------------------------------- */ - public async Task Create(Player player) + public async Task CreateAsync(Player player) { - _playerContext.Players.Add(player); + _playerContext.Entry(player).State = EntityState.Added; await _playerContext.SaveChangesAsync(); _memoryCache.Remove(MemoryCacheKey_Retrieve); } @@ -35,9 +35,9 @@ public async Task Create(Player player) Retrieve ----------------------------------------------------------------------------------------------- */ - public Task> Retrieve() + public async Task> RetrieveAsync() { - if (_memoryCache.TryGetValue(MemoryCacheKey_Retrieve, out Task>? players) + if (_memoryCache.TryGetValue(MemoryCacheKey_Retrieve, out List? players) && players != null) { _logger.Log(LogLevel.Information, "Players retrieved from MemoryCache."); @@ -47,9 +47,9 @@ public Task> Retrieve() { // Introduced on purpose to simulate a real database query with a // delay of beteen 1 and 2 seconds. - Task.Delay(new Random().Next(1000, 2000)); + await Task.Delay(new Random().Next(1000, 2000)); - players = _playerContext.Players.ToListAsync(); + players = await _playerContext.Players.ToListAsync(); _memoryCache.Set(MemoryCacheKey_Retrieve, players, GetMemoryCacheEntryOptions()); _logger.Log(LogLevel.Information, "Players retrieved from DbContext."); @@ -67,7 +67,7 @@ public Task> Retrieve() Update ----------------------------------------------------------------------------------------------- */ - public async Task Update(Player player) + public async Task UpdateAsync(Player player) { _playerContext.Entry(player).State = EntityState.Modified; @@ -96,23 +96,18 @@ public async Task Update(Player player) Delete ----------------------------------------------------------------------------------------------- */ - public async Task Delete(long id) + public async Task DeleteAsync(long id) { var player = await _playerContext.Players.FindAsync(id); if (player != null) { - _playerContext.Players.Remove(player); + _playerContext.Entry(player).State = EntityState.Deleted; await _playerContext.SaveChangesAsync(); _memoryCache.Remove(MemoryCacheKey_Retrieve); } } - private bool PlayerExists(long id) - { - return (_playerContext.Players?.Any(e => e.Id == id)).GetValueOrDefault(); - } - private MemoryCacheEntryOptions GetMemoryCacheEntryOptions() { return new MemoryCacheEntryOptions()