diff --git a/examples/HttpApiSimulator/Contracts/User.cs b/examples/HttpApiSimulator/Contracts/User.cs index 0f2b519f..10c290a7 100644 --- a/examples/HttpApiSimulator/Contracts/User.cs +++ b/examples/HttpApiSimulator/Contracts/User.cs @@ -1,28 +1,27 @@ -namespace HttpApiSimulator.Contracts +namespace HttpApiSimulator.Contracts; + +public class User { - public class User - { - public int Id { get; set; } - public string FirstName { get; set; } - public string LastName { get; set; } - public int Age { get; set; } - } + public int Id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public int Age { get; set; } +} - public class UpdateUserReq - { - public string FirstName { get; set; } - public string LastName { get; set; } - public int Age { get; set; } +public class UpdateUserReq +{ + public string FirstName { get; set; } + public string LastName { get; set; } + public int Age { get; set; } - public User ToUser(int id = 0) + public User ToUser(int id = 0) + { + return new User { - return new User - { - Id = id, - FirstName = this.FirstName, - LastName = this.LastName, - Age = this.Age - }; - } + Id = id, + FirstName = this.FirstName, + LastName = this.LastName, + Age = this.Age + }; } } diff --git a/examples/HttpApiSimulator/Controllers/CookiesAuthenticationController.cs b/examples/HttpApiSimulator/Controllers/CookiesAuthenticationController.cs index d142bbad..1f6605e9 100644 --- a/examples/HttpApiSimulator/Controllers/CookiesAuthenticationController.cs +++ b/examples/HttpApiSimulator/Controllers/CookiesAuthenticationController.cs @@ -1,50 +1,49 @@ using System.Collections.Concurrent; using Microsoft.AspNetCore.Mvc; -namespace HttpApiSimulator.Controllers +namespace HttpApiSimulator.Controllers; + +[Route("api/[controller]")] +[ApiController] +public class CookiesAuthenticationController : ControllerBase { - [Route("api/[controller]")] - [ApiController] - public class CookiesAuthenticationController : ControllerBase - { - private static readonly ConcurrentBag GeneratedCookies = new(); + private static readonly ConcurrentBag GeneratedCookies = new(); - public CookiesAuthenticationController() { } + public CookiesAuthenticationController() { } - [HttpPost] - public IActionResult Login([FromBody] LoginDto loginDto) - { - var authCookie = Guid.NewGuid(); + [HttpPost] + public IActionResult Login([FromBody] LoginDto loginDto) + { + var authCookie = Guid.NewGuid(); - GeneratedCookies.Add(authCookie); - Response.Cookies.Append("auth_hash_cookie", authCookie.ToString()); + GeneratedCookies.Add(authCookie); + Response.Cookies.Append("auth_hash_cookie", authCookie.ToString()); - return Ok("Cookies were added."); - } + return Ok("Cookies were added."); + } + + [HttpGet] + public IActionResult GetData() + { + var authCookie = Request.Cookies["auth_hash_cookie"]; + var convertResult = Guid.TryParse(authCookie, out Guid guidAuthCookie); - [HttpGet] - public IActionResult GetData() + if (convertResult) { - var authCookie = Request.Cookies["auth_hash_cookie"]; - var convertResult = Guid.TryParse(authCookie, out Guid guidAuthCookie); - - if (convertResult) - { - if (GeneratedCookies.TryPeek(out guidAuthCookie)) - return Ok("Authentication successful."); - else - return Unauthorized("Authentication failed. The auth cookie is wrong."); - } + if (GeneratedCookies.TryPeek(out guidAuthCookie)) + return Ok("Authentication successful."); else - { - return Unauthorized("The auth cookie is not valid."); - } + return Unauthorized("Authentication failed. The auth cookie is wrong."); + } + else + { + return Unauthorized("The auth cookie is not valid."); } } +} - public class LoginDto - { - public string Login { get; set; } - public string Password { get; set; } - } +public class LoginDto +{ + public string Login { get; set; } + public string Password { get; set; } } diff --git a/examples/HttpApiSimulator/Controllers/DataBasesController.cs b/examples/HttpApiSimulator/Controllers/DataBasesController.cs index d96ee812..1c968c02 100644 --- a/examples/HttpApiSimulator/Controllers/DataBasesController.cs +++ b/examples/HttpApiSimulator/Controllers/DataBasesController.cs @@ -1,24 +1,23 @@ using HttpApiSimulator.Infra.DAL; using Microsoft.AspNetCore.Mvc; -namespace HttpApiSimulator.Controllers +namespace HttpApiSimulator.Controllers; + +[Route("api/[controller]")] +[ApiController] +public class DataBasesController : ControllerBase { - [Route("api/[controller]")] - [ApiController] - public class DataBasesController : ControllerBase - { - private readonly IUserRepository _repository; + private readonly IUserRepository _repository; - public DataBasesController(IUserRepository repository) - { - _repository = repository; - } + public DataBasesController(IUserRepository repository) + { + _repository = repository; + } - [HttpPut] - public void PrepareDB() - { - _repository.DeleTable(); - _repository.CreateDB(); - } + [HttpPut] + public void PrepareDB() + { + _repository.DeleTable(); + _repository.CreateDB(); } } diff --git a/examples/HttpApiSimulator/Controllers/UsersController.cs b/examples/HttpApiSimulator/Controllers/UsersController.cs index 4b5c5f55..d064389c 100644 --- a/examples/HttpApiSimulator/Controllers/UsersController.cs +++ b/examples/HttpApiSimulator/Controllers/UsersController.cs @@ -2,38 +2,37 @@ using HttpApiSimulator.Infra.DAL; using Microsoft.AspNetCore.Mvc; -namespace HttpApiSimulator.Controllers +namespace HttpApiSimulator.Controllers; + +[Route("api/[controller]")] +[ApiController] +public class UsersController : ControllerBase { - [Route("api/[controller]")] - [ApiController] - public class UsersController : ControllerBase - { - private readonly IUserRepository _repository; + private readonly IUserRepository _repository; - public UsersController(IUserRepository repository) - { - _repository = repository; - } + public UsersController(IUserRepository repository) + { + _repository = repository; + } - [HttpGet("{id}")] - public async Task Get(int id) - { - var user = await _repository.GetById(id); - return user == null - ? Results.NotFound() - : Results.Ok(user); - } + [HttpGet("{id}")] + public async Task Get(int id) + { + var user = await _repository.GetById(id); + return user == null + ? Results.NotFound() + : Results.Ok(user); + } - [HttpPost] - public async Task Post([FromBody] User user) - { - return await _repository.Update(user); - } + [HttpPost] + public async Task Post([FromBody] User user) + { + return await _repository.Update(user); + } - [HttpPut("{id}")] - public async Task Put(int id, [FromBody] UpdateUserReq request) - { - return await _repository.Update(request.ToUser(id)); - } + [HttpPut("{id}")] + public async Task Put(int id, [FromBody] UpdateUserReq request) + { + return await _repository.Update(request.ToUser(id)); } } diff --git a/examples/HttpApiSimulator/Infra/DAL/IUserRepository.cs b/examples/HttpApiSimulator/Infra/DAL/IUserRepository.cs index 3d5f256a..312a9366 100644 --- a/examples/HttpApiSimulator/Infra/DAL/IUserRepository.cs +++ b/examples/HttpApiSimulator/Infra/DAL/IUserRepository.cs @@ -1,12 +1,11 @@ using HttpApiSimulator.Contracts; -namespace HttpApiSimulator.Infra.DAL +namespace HttpApiSimulator.Infra.DAL; + +public interface IUserRepository { - public interface IUserRepository - { - ValueTask GetById(int id); - ValueTask Update(User user); - void CreateDB(); - void DeleTable(); - } + ValueTask GetById(int id); + ValueTask Update(User user); + void CreateDB(); + void DeleTable(); } diff --git a/examples/HttpApiSimulator/Infra/DAL/RedisRepository.cs b/examples/HttpApiSimulator/Infra/DAL/RedisRepository.cs index 4b89c7ac..df398371 100644 --- a/examples/HttpApiSimulator/Infra/DAL/RedisRepository.cs +++ b/examples/HttpApiSimulator/Infra/DAL/RedisRepository.cs @@ -2,42 +2,41 @@ using HttpApiSimulator.Contracts; using StackExchange.Redis; -namespace HttpApiSimulator.Infra.DAL +namespace HttpApiSimulator.Infra.DAL; + +public class RedisRepository: IUserRepository { - public class RedisRepository: IUserRepository - { - private ConnectionMultiplexer _redis; - private IDatabase _database; - private RedisSettings _settings; + private ConnectionMultiplexer _redis; + private IDatabase _database; + private RedisSettings _settings; - public RedisRepository(RedisSettings settings) - { - _settings = settings; - _redis = ConnectionMultiplexer.Connect(_settings.ConnectionString); - } + public RedisRepository(RedisSettings settings) + { + _settings = settings; + _redis = ConnectionMultiplexer.Connect(_settings.ConnectionString); + } - public void CreateDB() - { - _database = _redis.GetDatabase(); - } + public void CreateDB() + { + _database = _redis.GetDatabase(); + } - public void DeleTable() - { - var server = _redis.GetServer(_settings.ServerName); - server.FlushDatabase(); - } + public void DeleTable() + { + var server = _redis.GetServer(_settings.ServerName); + server.FlushDatabase(); + } - public async ValueTask GetById(int id) - { - byte[] data = await _database.StringGetAsync(id.ToString()); - var user = data != null ? JsonSerializer.Deserialize(data) : null; - return user; - } + public async ValueTask GetById(int id) + { + byte[] data = await _database.StringGetAsync(id.ToString()); + var user = data != null ? JsonSerializer.Deserialize(data) : null; + return user; + } - public ValueTask Update(User user) - { - var data = JsonSerializer.SerializeToUtf8Bytes(user); - return new ValueTask(_database.StringSetAsync(user.Id.ToString(), data)); - } + public ValueTask Update(User user) + { + var data = JsonSerializer.SerializeToUtf8Bytes(user); + return new ValueTask(_database.StringSetAsync(user.Id.ToString(), data)); } } diff --git a/examples/HttpApiSimulator/Infra/DAL/SQLiteRepository.cs b/examples/HttpApiSimulator/Infra/DAL/SQLiteRepository.cs index 11e5f987..d8439961 100644 --- a/examples/HttpApiSimulator/Infra/DAL/SQLiteRepository.cs +++ b/examples/HttpApiSimulator/Infra/DAL/SQLiteRepository.cs @@ -2,65 +2,64 @@ using Dapper.Contrib.Extensions; using HttpApiSimulator.Contracts; -namespace HttpApiSimulator.Infra.DAL +namespace HttpApiSimulator.Infra.DAL; + +public class SQLiteDBRepository : IUserRepository { - public class SQLiteDBRepository : IUserRepository - { - private SQLiteConnection _connection = null; + private SQLiteConnection _connection = null; - public SQLiteDBRepository(SQLiteSettings settings) - { - _connection = new SQLiteConnection(settings.ConnectionString); - _connection.Open(); - } + public SQLiteDBRepository(SQLiteSettings settings) + { + _connection = new SQLiteConnection(settings.ConnectionString); + _connection.Open(); + } - public void CreateDB() - { - using var command = _connection.CreateCommand(); + public void CreateDB() + { + using var command = _connection.CreateCommand(); - command.CommandText = "PRAGMA journal_mode=WAL"; - command.ExecuteNonQuery(); + command.CommandText = "PRAGMA journal_mode=WAL"; + command.ExecuteNonQuery(); - command.CommandText = "pragma synchronous = normar"; - command.ExecuteNonQuery(); - command.CommandText = @"CREATE TABLE IF NOT EXISTS users + command.CommandText = "pragma synchronous = normar"; + command.ExecuteNonQuery(); + command.CommandText = @"CREATE TABLE IF NOT EXISTS users (Id INTEGER PRIMARY KEY, FirstName TEXT, LastName TEXT, Age INTEGER)"; - command.ExecuteNonQuery(); - } + command.ExecuteNonQuery(); + } - public ValueTask GetById(int id) - { - return new ValueTask(_connection.GetAsync(id)); - } + public ValueTask GetById(int id) + { + return new ValueTask(_connection.GetAsync(id)); + } - public ValueTask Update(User user) - { - using var command = _connection.CreateCommand(); + public ValueTask Update(User user) + { + using var command = _connection.CreateCommand(); - command.CommandText = @"INSERT INTO users (Id, FirstName, LastName, Age) + command.CommandText = @"INSERT INTO users (Id, FirstName, LastName, Age) VALUES (@Id, @FirstName, @LastName, @Age) ON CONFLICT(Id) DO UPDATE SET FirstName = excluded.FirstName, LastName = excluded.LastName, Age = excluded.Age;"; - command.Parameters.AddWithValue("@Id", user.Id); - command.Parameters.AddWithValue("@FirstName", user.FirstName); - command.Parameters.AddWithValue("@LastName", user.LastName); - command.Parameters.AddWithValue("@Age", user.Age); + command.Parameters.AddWithValue("@Id", user.Id); + command.Parameters.AddWithValue("@FirstName", user.FirstName); + command.Parameters.AddWithValue("@LastName", user.LastName); + command.Parameters.AddWithValue("@Age", user.Age); - var affectedRows = command.ExecuteNonQuery(); + var affectedRows = command.ExecuteNonQuery(); - return ValueTask.FromResult(affectedRows > 0); - } + return ValueTask.FromResult(affectedRows > 0); + } - public void DeleTable() - { - using var command = _connection.CreateCommand(); + public void DeleTable() + { + using var command = _connection.CreateCommand(); - command.CommandText = "DROP TABLE IF EXISTS users"; - command.ExecuteNonQuery(); - } + command.CommandText = "DROP TABLE IF EXISTS users"; + command.ExecuteNonQuery(); } } diff --git a/examples/HttpApiSimulator/Program.cs b/examples/HttpApiSimulator/Program.cs index 16ae8844..333ffeda 100644 --- a/examples/HttpApiSimulator/Program.cs +++ b/examples/HttpApiSimulator/Program.cs @@ -1,71 +1,70 @@ using HttpApiSimulator.Infra.DAL; -namespace HttpApiSimulator +namespace HttpApiSimulator; + +public class SQLiteSettings { - public class SQLiteSettings - { - public string ConnectionString { get; set; } - } + public string ConnectionString { get; set; } +} - public class RedisSettings - { - public string ConnectionString { get; set; } - public string ServerName { get; set; } - } +public class RedisSettings +{ + public string ConnectionString { get; set; } + public string ServerName { get; set; } +} - public class Program +public class Program +{ + public static void Main(string[] args) { - public static void Main(string[] args) - { - var builder = WebApplication.CreateBuilder(args); + var builder = WebApplication.CreateBuilder(args); - builder.Services.AddControllers(); + builder.Services.AddControllers(); - builder.Services.AddSwaggerGen(); - builder.Services.AddAuthentication(); + builder.Services.AddSwaggerGen(); + builder.Services.AddAuthentication(); - var dbUse = builder.Configuration.GetValue("DbUse", ""); + var dbUse = builder.Configuration.GetValue("DbUse", ""); - if (dbUse == "SQLite") - { - var settings = builder.Configuration.GetSection("SQLiteSettings").Get(); - var rep = new SQLiteDBRepository(settings); - builder.Services.AddSingleton(rep); - } - else if (dbUse == "Redis") - { - var settings = builder.Configuration.GetSection("RedisSetings").Get(); - var rep = new RedisRepository(settings); - builder.Services.AddSingleton(rep); - } - else // InMemory - { - var inMemory = new InMemoryRepository(); - builder.Services.AddSingleton(inMemory); - } + if (dbUse == "SQLite") + { + var settings = builder.Configuration.GetSection("SQLiteSettings").Get(); + var rep = new SQLiteDBRepository(settings); + builder.Services.AddSingleton(rep); + } + else if (dbUse == "Redis") + { + var settings = builder.Configuration.GetSection("RedisSetings").Get(); + var rep = new RedisRepository(settings); + builder.Services.AddSingleton(rep); + } + else // InMemory + { + var inMemory = new InMemoryRepository(); + builder.Services.AddSingleton(inMemory); + } - var app = builder.Build(); + var app = builder.Build(); - if (!app.Environment.IsDevelopment()) - { - app.UseExceptionHandler("/Error"); - } + if (!app.Environment.IsDevelopment()) + { + app.UseExceptionHandler("/Error"); + } - if (app.Environment.IsDevelopment()) - { - app.UseSwagger(); - app.UseSwaggerUI(); - } + if (app.Environment.IsDevelopment()) + { + app.UseSwagger(); + app.UseSwaggerUI(); + } - app.UseHttpsRedirection(); - app.UseRouting(); + app.UseHttpsRedirection(); + app.UseRouting(); - app.UseAuthorization(); + app.UseAuthorization(); - app.MapControllers(); - app.UseWebSockets(); + app.MapControllers(); + app.UseWebSockets(); - app.Run(); - } + app.Run(); } }