Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to net8; apply lints #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ typings
bin
obj

.vs
*.user
17 changes: 6 additions & 11 deletions Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
namespace WebApi.Controllers;

using Microsoft.AspNetCore.Mvc;
using WebApi.Entities;
using WebApi.Models.Users;
using WebApi.Services;

[ApiController]
[Route("[controller]")]
public class UsersController : ControllerBase
public class UsersController(IUserService userService) : ControllerBase
{
private IUserService _userService;

public UsersController(IUserService userService)
{
_userService = userService;
}
private readonly IUserService _userService = userService;

[HttpGet]
public async Task<IActionResult> GetAll()
public async Task<ActionResult> GetAll()
{
var users = await _userService.GetAll();
return Ok(users);
}

[HttpGet("{id}")]
public async Task<IActionResult> GetById(int id)
public async Task<ActionResult<User>> GetById(int id)
{
var user = await _userService.GetById(id);
return Ok(user);
return await _userService.GetById(id);
}

[HttpPost]
Expand Down
2 changes: 1 addition & 1 deletion Helpers/AppException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public AppException() : base() {}
public AppException(string message) : base(message) { }

public AppException(string message, params object[] args)
: base(String.Format(CultureInfo.CurrentCulture, message, args))
: base(string.Format(CultureInfo.CurrentCulture, message, args))
{
}
}
17 changes: 6 additions & 11 deletions Helpers/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ namespace WebApi.Helpers;
using Microsoft.Extensions.Options;
using Npgsql;

public class DataContext
public class DataContext(IOptions<DbSettings> dbSettings)
{
private DbSettings _dbSettings;

public DataContext(IOptions<DbSettings> dbSettings)
{
_dbSettings = dbSettings.Value;
}
private readonly DbSettings _dbSettings = dbSettings.Value;

public IDbConnection CreateConnection()
{
Expand All @@ -22,11 +17,11 @@ public IDbConnection CreateConnection()

public async Task Init()
{
await _initDatabase();
await _initTables();
await InitDatabase();
await InitTables();
}

private async Task _initDatabase()
private async Task InitDatabase()
{
// create database if it doesn't exist
var connectionString = $"Host={_dbSettings.Server}; Database=postgres; Username={_dbSettings.UserId}; Password={_dbSettings.Password};";
Expand All @@ -40,7 +35,7 @@ private async Task _initDatabase()
}
}

private async Task _initTables()
private async Task InitTables()
{
// create tables if they don't exist
using var connection = CreateConnection();
Expand Down
18 changes: 7 additions & 11 deletions Helpers/ErrorHandlerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ namespace WebApi.Helpers;
using System.Text.Json;
using System.Threading.Tasks;

public class ErrorHandlerMiddleware
public class ErrorHandlerMiddleware(
RequestDelegate next,
ILogger<ErrorHandlerMiddleware> logger)
{
private readonly RequestDelegate _next;
private readonly ILogger _logger;

public ErrorHandlerMiddleware(RequestDelegate next, ILogger<ErrorHandlerMiddleware> logger)
{
_next = next;
_logger = logger;
}
private readonly RequestDelegate _next = next;
private readonly ILogger _logger = logger;

public async Task Invoke(HttpContext context)
{
Expand All @@ -32,11 +28,11 @@ public async Task Invoke(HttpContext context)

switch (error)
{
case AppException e:
case AppException:
// custom application error
response.StatusCode = (int)HttpStatusCode.BadRequest;
break;
case KeyNotFoundException e:
case KeyNotFoundException:
// not found error
response.StatusCode = (int)HttpStatusCode.NotFound;
break;
Expand Down
6 changes: 3 additions & 3 deletions Models/Users/UpdateRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ public class UpdateRequest
public string? Password
{
get => _password;
set => _password = replaceEmptyWithNull(value);
set => _password = ReplaceEmptyWithNull(value);
}

private string? _confirmPassword;
[Compare("Password")]
public string? ConfirmPassword
{
get => _confirmPassword;
set => _confirmPassword = replaceEmptyWithNull(value);
set => _confirmPassword = ReplaceEmptyWithNull(value);
}

// helpers

private string? replaceEmptyWithNull(string? value)
private static string? ReplaceEmptyWithNull(string? value)
{
// replace empty string with null to make field optional
return string.IsNullOrEmpty(value) ? null : value;
Expand Down
9 changes: 2 additions & 7 deletions Repositories/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ public interface IUserRepository
Task Delete(int id);
}

public class UserRepository : IUserRepository
public class UserRepository(DataContext context) : IUserRepository
{
private DataContext _context;

public UserRepository(DataContext context)
{
_context = context;
}
private readonly DataContext _context = context;

public async Task<IEnumerable<User>> GetAll()
{
Expand Down
16 changes: 5 additions & 11 deletions Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,12 @@ public interface IUserService
Task Delete(int id);
}

public class UserService : IUserService
public class UserService(
IUserRepository userRepository,
IMapper mapper) : IUserService
{
private IUserRepository _userRepository;
private readonly IMapper _mapper;

public UserService(
IUserRepository userRepository,
IMapper mapper)
{
_userRepository = userRepository;
_mapper = mapper;
}
private readonly IUserRepository _userRepository = userRepository;
private readonly IMapper _mapper = mapper;

public async Task<IEnumerable<User>> GetAll()
{
Expand Down
2 changes: 1 addition & 1 deletion WebApi.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>WebApi</RootNamespace>
Expand Down