diff --git a/Dotnet.AspNetCore.Samples.WebApi.Tests/PlayerServiceTests.cs b/Dotnet.AspNetCore.Samples.WebApi.Tests/PlayerServiceTests.cs index 83eb444..f7933b8 100644 --- a/Dotnet.AspNetCore.Samples.WebApi.Tests/PlayerServiceTests.cs +++ b/Dotnet.AspNetCore.Samples.WebApi.Tests/PlayerServiceTests.cs @@ -33,23 +33,23 @@ public PlayerServiceTests() using var dbCommand = context.Database.GetDbConnection().CreateCommand(); dbCommand.CommandText = """ - CREATE TABLE IF NOT EXISTS "players" - ( - "id" INTEGER, - "firstName" TEXT NOT NULL, - "middleName" TEXT, - "lastName" TEXT NOT NULL, - "dateOfBirth" TEXT, - "squadNumber" INTEGER NOT NULL, - "position" TEXT NOT NULL, - "abbrPosition" TEXT, - "team" TEXT, - "league" TEXT, - "starting11" BOOLEAN, - PRIMARY KEY("id") - ); - """; - + CREATE TABLE IF NOT EXISTS "players" + ( + "id" INTEGER, + "firstName" TEXT NOT NULL, + "middleName" TEXT, + "lastName" TEXT NOT NULL, + "dateOfBirth" TEXT, + "squadNumber" INTEGER NOT NULL, + "position" TEXT NOT NULL, + "abbrPosition" TEXT, + "team" TEXT, + "league" TEXT, + "starting11" BOOLEAN, + PRIMARY KEY("id") + ); + """; + dbCommand.ExecuteNonQuery(); } @@ -58,6 +58,7 @@ PRIMARY KEY("id") } PlayerContext CreatePlayerContext() => new PlayerContext(dbContextOptions); + public void Dispose() => dbConnection.Dispose(); [Fact] @@ -140,14 +141,10 @@ private static ILogger CreateLoggerMock() private static IMemoryCache CreateMemoryCacheMock(object? value) { var mock = new Mock(); - - mock - .Setup(x => x.TryGetValue(It.IsAny(), out value)) - .Returns(true); - - mock - .Setup(x => x.CreateEntry(It.IsAny())) - .Returns(Mock.Of); + + mock.Setup(x => x.TryGetValue(It.IsAny(), out value)).Returns(true); + + mock.Setup(x => x.CreateEntry(It.IsAny())).Returns(Mock.Of); return mock.Object; } diff --git a/Dotnet.AspNetCore.Samples.WebApi/Controllers/PlayersController.cs b/Dotnet.AspNetCore.Samples.WebApi/Controllers/PlayersController.cs index 7d80a19..0d37820 100644 --- a/Dotnet.AspNetCore.Samples.WebApi/Controllers/PlayersController.cs +++ b/Dotnet.AspNetCore.Samples.WebApi/Controllers/PlayersController.cs @@ -1,6 +1,6 @@ -using Microsoft.AspNetCore.Mvc; using Dotnet.AspNetCore.Samples.WebApi.Models; using Dotnet.AspNetCore.Samples.WebApi.Services; +using Microsoft.AspNetCore.Mvc; namespace Dotnet.AspNetCore.Samples.WebApi.Controllers; @@ -10,7 +10,7 @@ Scaffolded with dotnet-aspnet-codegenerator https://learn.microsoft.com/en-us/aspnet/core/fundamentals/tools/dotnet-aspnet-codegenerator dotnet aspnet-codegenerator controller -name PlayersController \ --async -api --model Player --dataContext PlayerContext \ +-async -api --model Player --dataContext PlayerContext \ -outDir Controllers --------------------------------------------------------------------------------------------------- */ @@ -60,7 +60,7 @@ HTTP GET public async Task>> GetPlayers() { var players = await _playerService.RetrieveAsync(); - + if (players.Any()) { return players; diff --git a/Dotnet.AspNetCore.Samples.WebApi/Data/PlayerDataBuilder.cs b/Dotnet.AspNetCore.Samples.WebApi/Data/PlayerDataBuilder.cs index e698167..08e12af 100644 --- a/Dotnet.AspNetCore.Samples.WebApi/Data/PlayerDataBuilder.cs +++ b/Dotnet.AspNetCore.Samples.WebApi/Data/PlayerDataBuilder.cs @@ -1,5 +1,5 @@ -using Dotnet.AspNetCore.Samples.WebApi.Models; -using System.Text.Json; +using System.Text.Json; +using Dotnet.AspNetCore.Samples.WebApi.Models; namespace Dotnet.AspNetCore.Samples.WebApi.Data; @@ -7,8 +7,7 @@ public static class PlayerDataBuilder { public static Player? SeedOneById(int id) { - return SeedWithStarting11() - .SingleOrDefault(player => player.Id == id); + return SeedWithStarting11().SingleOrDefault(player => player.Id == id); } public static List SeedWithStarting11() @@ -17,7 +16,7 @@ public static List SeedWithStarting11() players.Add( new Player - { + { Id = 1, FirstName = "Damián", MiddleName = "Emiliano", @@ -31,10 +30,10 @@ public static List SeedWithStarting11() Starting11 = true, } ); - + players.Add( new Player - { + { Id = 2, FirstName = "Nahuel", LastName = "Molina", @@ -47,10 +46,10 @@ public static List SeedWithStarting11() Starting11 = true, } ); - + players.Add( new Player - { + { Id = 3, FirstName = "Cristian", MiddleName = "Gabriel", @@ -64,10 +63,10 @@ public static List SeedWithStarting11() Starting11 = true, } ); - + players.Add( new Player - { + { Id = 4, FirstName = "Nicolás", MiddleName = "Hernán Gonzalo", @@ -81,10 +80,10 @@ public static List SeedWithStarting11() Starting11 = true, } ); - + players.Add( new Player - { + { Id = 5, FirstName = "Nicolás", MiddleName = "Alejandro", @@ -98,10 +97,10 @@ public static List SeedWithStarting11() Starting11 = true, } ); - + players.Add( new Player - { + { Id = 6, FirstName = "Ángel", MiddleName = "Fabián", @@ -115,10 +114,10 @@ public static List SeedWithStarting11() Starting11 = true, } ); - + players.Add( new Player - { + { Id = 7, FirstName = "Rodrigo", MiddleName = "Javier", @@ -132,10 +131,10 @@ public static List SeedWithStarting11() Starting11 = true, } ); - + players.Add( new Player - { + { Id = 8, FirstName = "Enzo", MiddleName = "Jeremías", @@ -149,10 +148,10 @@ public static List SeedWithStarting11() Starting11 = true, } ); - + players.Add( new Player - { + { Id = 9, FirstName = "Alexis", LastName = "Mac Allister", @@ -165,10 +164,10 @@ public static List SeedWithStarting11() Starting11 = true, } ); - + players.Add( new Player - { + { Id = 10, FirstName = "Lionel", MiddleName = "Andrés", @@ -182,10 +181,10 @@ public static List SeedWithStarting11() Starting11 = true, } ); - + players.Add( new Player - { + { Id = 11, FirstName = "Julián", LastName = "Álvarez", @@ -198,158 +197,159 @@ public static List SeedWithStarting11() Starting11 = true, } ); - + return players; } + public static List SeedWithDeserializedJson() { var players = new List(); var json = """ - [{ - "id": 1, - "firstName": "Damián", - "middleName": "Emiliano", - "lastName": "Martínez", - "dateOfBirth": "1992-09-01T00:00:00.000Z", - "squadNumber": 23, - "position": "Goalkeeper", - "abbrPosition": "GK", - "team": "Aston Villa FC", - "league": "Premier League", - "starting11": true - }, - { - "id": 2, - "firstName": "Nahuel", - "middleName": null, - "lastName": "Molina", - "dateOfBirth": "1998-04-05T00:00:00.000Z", - "squadNumber": 26, - "position": "Right-Back", - "abbrPosition": "RB", - "team": "Atlético Madrid", - "league": "La Liga", - "starting11": true - }, - { - "id": 3, - "firstName": "Cristian", - "middleName": "Gabriel", - "lastName": "Romero", - "dateOfBirth": "1998-04-26T00:00:00.000Z", - "squadNumber": 13, - "position": "Centre-Back", - "abbrPosition": "CB", - "team": "Tottenham Hotspur", - "league": "Premier League", - "starting11": true - }, - { - "id": 4, - "firstName": "Nicolás", - "middleName": "Hernán Gonzalo", - "lastName": "Otamendi", - "dateOfBirth": "1988-02-11T00:00:00.000Z", - "squadNumber": 19, - "position": "Centre-Back", - "abbrPosition": "CB", - "team": "SL Benfica", - "league": "Liga Portugal", - "starting11": true - }, - { - "id": 5, - "firstName": "Nicolás", - "middleName": "Alejandro", - "lastName": "Tagliafico", - "dateOfBirth": "1992-08-30T00:00:00.000Z", - "squadNumber": 3, - "position": "Left-Back", - "abbrPosition": "LB", - "team": "Olympique Lyon", - "league": "Ligue 1", - "starting11": true - }, - { - "id": 6, - "firstName": "Ángel", - "middleName": "Fabián", - "lastName": "Di María", - "dateOfBirth": "1988-02-13T00:00:00.000Z", - "squadNumber": 11, - "position": "Right Winger", - "abbrPosition": "RW", - "team": "SL Benfica", - "league": "Liga Portugal", - "starting11": true - }, - { - "id": 7, - "firstName": "Rodrigo", - "middleName": "Javier", - "lastName": "de Paul", - "dateOfBirth": "1994-05-23T00:00:00.000Z", - "squadNumber": 7, - "position": "Central Midfield", - "abbrPosition": "CM", - "team": "Atlético Madrid", - "league": "La Liga", - "starting11": true - }, - { - "id": 8, - "firstName": "Enzo", - "middleName": "Jeremías", - "lastName": "Fernández", - "dateOfBirth": "2001-01-16T00:00:00.000Z", - "squadNumber": 24, - "position": "Central Midfield", - "abbrPosition": "CM", - "team": "Chelsea FC", - "league": "Premier League", - "starting11": true - }, - { - "id": 9, - "firstName": "Alexis", - "middleName": null, - "lastName": "Mac Allister", - "dateOfBirth": "1998-12-23T00:00:00.000Z", - "squadNumber": 20, - "position": "Central Midfield", - "abbrPosition": "CM", - "team": "Liverpool FC", - "league": "Premier League", - "starting11": true - }, - { - "id": 10, - "firstName": "Lionel", - "middleName": "Andrés", - "lastName": "Messi", - "dateOfBirth": "1987-06-23T00:00:00.000Z", - "squadNumber": 10, - "position": "Right Winger", - "abbrPosition": "RW", - "team": "Inter Miami CF", - "league": "Major League Soccer", - "starting11": true - }, - { - "id": 11, - "firstName": "Julián", - "middleName": null, - "lastName": "Álvarez", - "dateOfBirth": "2000-01-30T00:00:00.000Z", - "squadNumber": 9, - "position": "Centre-Forward", - "abbrPosition": "CF", - "team": "Manchester City", - "league": "Premier League", - "starting11": true - }] - """; + [{ + "id": 1, + "firstName": "Damián", + "middleName": "Emiliano", + "lastName": "Martínez", + "dateOfBirth": "1992-09-01T00:00:00.000Z", + "squadNumber": 23, + "position": "Goalkeeper", + "abbrPosition": "GK", + "team": "Aston Villa FC", + "league": "Premier League", + "starting11": true + }, + { + "id": 2, + "firstName": "Nahuel", + "middleName": null, + "lastName": "Molina", + "dateOfBirth": "1998-04-05T00:00:00.000Z", + "squadNumber": 26, + "position": "Right-Back", + "abbrPosition": "RB", + "team": "Atlético Madrid", + "league": "La Liga", + "starting11": true + }, + { + "id": 3, + "firstName": "Cristian", + "middleName": "Gabriel", + "lastName": "Romero", + "dateOfBirth": "1998-04-26T00:00:00.000Z", + "squadNumber": 13, + "position": "Centre-Back", + "abbrPosition": "CB", + "team": "Tottenham Hotspur", + "league": "Premier League", + "starting11": true + }, + { + "id": 4, + "firstName": "Nicolás", + "middleName": "Hernán Gonzalo", + "lastName": "Otamendi", + "dateOfBirth": "1988-02-11T00:00:00.000Z", + "squadNumber": 19, + "position": "Centre-Back", + "abbrPosition": "CB", + "team": "SL Benfica", + "league": "Liga Portugal", + "starting11": true + }, + { + "id": 5, + "firstName": "Nicolás", + "middleName": "Alejandro", + "lastName": "Tagliafico", + "dateOfBirth": "1992-08-30T00:00:00.000Z", + "squadNumber": 3, + "position": "Left-Back", + "abbrPosition": "LB", + "team": "Olympique Lyon", + "league": "Ligue 1", + "starting11": true + }, + { + "id": 6, + "firstName": "Ángel", + "middleName": "Fabián", + "lastName": "Di María", + "dateOfBirth": "1988-02-13T00:00:00.000Z", + "squadNumber": 11, + "position": "Right Winger", + "abbrPosition": "RW", + "team": "SL Benfica", + "league": "Liga Portugal", + "starting11": true + }, + { + "id": 7, + "firstName": "Rodrigo", + "middleName": "Javier", + "lastName": "de Paul", + "dateOfBirth": "1994-05-23T00:00:00.000Z", + "squadNumber": 7, + "position": "Central Midfield", + "abbrPosition": "CM", + "team": "Atlético Madrid", + "league": "La Liga", + "starting11": true + }, + { + "id": 8, + "firstName": "Enzo", + "middleName": "Jeremías", + "lastName": "Fernández", + "dateOfBirth": "2001-01-16T00:00:00.000Z", + "squadNumber": 24, + "position": "Central Midfield", + "abbrPosition": "CM", + "team": "Chelsea FC", + "league": "Premier League", + "starting11": true + }, + { + "id": 9, + "firstName": "Alexis", + "middleName": null, + "lastName": "Mac Allister", + "dateOfBirth": "1998-12-23T00:00:00.000Z", + "squadNumber": 20, + "position": "Central Midfield", + "abbrPosition": "CM", + "team": "Liverpool FC", + "league": "Premier League", + "starting11": true + }, + { + "id": 10, + "firstName": "Lionel", + "middleName": "Andrés", + "lastName": "Messi", + "dateOfBirth": "1987-06-23T00:00:00.000Z", + "squadNumber": 10, + "position": "Right Winger", + "abbrPosition": "RW", + "team": "Inter Miami CF", + "league": "Major League Soccer", + "starting11": true + }, + { + "id": 11, + "firstName": "Julián", + "middleName": null, + "lastName": "Álvarez", + "dateOfBirth": "2000-01-30T00:00:00.000Z", + "squadNumber": 9, + "position": "Centre-Forward", + "abbrPosition": "CF", + "team": "Manchester City", + "league": "Premier League", + "starting11": true + }] + """; var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; var starting11 = JsonSerializer.Deserialize>(json, options); diff --git a/Dotnet.AspNetCore.Samples.WebApi/Models/Player.cs b/Dotnet.AspNetCore.Samples.WebApi/Models/Player.cs index 6417c57..e9c4b2a 100644 --- a/Dotnet.AspNetCore.Samples.WebApi/Models/Player.cs +++ b/Dotnet.AspNetCore.Samples.WebApi/Models/Player.cs @@ -3,13 +3,13 @@ public class Player { public long Id { get; set; } - + public string? FirstName { get; set; } - + public string? MiddleName { get; set; } - + public string? LastName { get; set; } - + public DateTime? DateOfBirth { get; set; } public int SquadNumber { get; set; } diff --git a/Dotnet.AspNetCore.Samples.WebApi/Models/PlayerContext.cs b/Dotnet.AspNetCore.Samples.WebApi/Models/PlayerContext.cs index 2934e8d..80636cf 100644 --- a/Dotnet.AspNetCore.Samples.WebApi/Models/PlayerContext.cs +++ b/Dotnet.AspNetCore.Samples.WebApi/Models/PlayerContext.cs @@ -1,15 +1,13 @@ - -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; namespace Dotnet.AspNetCore.Samples.WebApi.Models; public class PlayerContext : DbContext { - public PlayerContext() {} + public PlayerContext() { } + public PlayerContext(DbContextOptions options) - : base(options) - { - } + : base(options) { } public virtual DbSet Players { get; set; } = null!; } diff --git a/Dotnet.AspNetCore.Samples.WebApi/Program.cs b/Dotnet.AspNetCore.Samples.WebApi/Program.cs index e4cd65c..badc8ae 100644 --- a/Dotnet.AspNetCore.Samples.WebApi/Program.cs +++ b/Dotnet.AspNetCore.Samples.WebApi/Program.cs @@ -16,7 +16,7 @@ builder.Services.AddDbContext(options => options.UseSqlite(@"Data Source=Data/players-sqlite3.db") - ); +); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); @@ -40,6 +40,7 @@ } app.UseHttpsRedirection(); + // https://learn.microsoft.com/en-us/aspnet/core/security/cors app.UseCors(); app.UseAuthorization(); diff --git a/Dotnet.AspNetCore.Samples.WebApi/Services/IPlayerService.cs b/Dotnet.AspNetCore.Samples.WebApi/Services/IPlayerService.cs index 3774c55..ba9a582 100644 --- a/Dotnet.AspNetCore.Samples.WebApi/Services/IPlayerService.cs +++ b/Dotnet.AspNetCore.Samples.WebApi/Services/IPlayerService.cs @@ -1,5 +1,4 @@ - -using Dotnet.AspNetCore.Samples.WebApi.Models; +using Dotnet.AspNetCore.Samples.WebApi.Models; namespace Dotnet.AspNetCore.Samples.WebApi.Services; diff --git a/Dotnet.AspNetCore.Samples.WebApi/Services/PlayerService.cs b/Dotnet.AspNetCore.Samples.WebApi/Services/PlayerService.cs index deaf7b1..71b6b0d 100644 --- a/Dotnet.AspNetCore.Samples.WebApi/Services/PlayerService.cs +++ b/Dotnet.AspNetCore.Samples.WebApi/Services/PlayerService.cs @@ -11,7 +11,11 @@ public class PlayerService : IPlayerService private readonly ILogger _logger; private readonly IMemoryCache _memoryCache; - public PlayerService(PlayerContext playerContext, ILogger logger, IMemoryCache memoryCache) + public PlayerService( + PlayerContext playerContext, + ILogger logger, + IMemoryCache memoryCache + ) { _playerContext = playerContext; _logger = logger; @@ -37,8 +41,10 @@ public async Task CreateAsync(Player player) */ public async Task> RetrieveAsync() { - if (_memoryCache.TryGetValue(MemoryCacheKey_Retrieve, out List? players) - && players != null) + if ( + _memoryCache.TryGetValue(MemoryCacheKey_Retrieve, out List? players) + && players != null + ) { _logger.Log(LogLevel.Information, "Players retrieved from MemoryCache."); return players; @@ -84,8 +90,8 @@ public async Task UpdateAsync(Player player) if (entry.Entity is Player) { throw new NotImplementedException( - "Concurrency conflicts handling not implemented for " - + entry.Metadata.Name); + "Concurrency conflicts handling not implemented for " + entry.Metadata.Name + ); } } }