diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 864f9f7..1434c3a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,7 +13,7 @@ jobs: - name: Setup .NET SDK uses: actions/setup-dotnet@v1 with: - dotnet-version: '7.0.101' + dotnet-version: '8.0.x' - name: Build run: dotnet build src/VSPoll.API.sln - name: Test diff --git a/.editorconfig b/src/.editorconfig similarity index 98% rename from .editorconfig rename to src/.editorconfig index 6667560..b5f9208 100644 --- a/.editorconfig +++ b/src/.editorconfig @@ -1,4 +1,5 @@ # To learn more about .editorconfig see https://aka.ms/editorconfigdocs +dotnet_remove_unnecessary_suppression_exclusions = category: ReSharper ############################### # Core EditorConfig Options # ############################### diff --git a/src/VSPoll.API.UnitTest/Utils/MathUtilsTests.cs b/src/VSPoll.API.UnitTest/Utils/MathUtilsTests.cs index 2698825..66582d0 100644 --- a/src/VSPoll.API.UnitTest/Utils/MathUtilsTests.cs +++ b/src/VSPoll.API.UnitTest/Utils/MathUtilsTests.cs @@ -50,14 +50,7 @@ public void NormalizePercentages_Changes() 19.161676646706585m, 22.75449101796407m, }.ToList(); - var expected = new[] - { - 19, - 21, - 19, - 18, - 23, - }; + int[] expected = [19, 21, 19, 18, 23]; var options = percentages.Select(percentage => new PollOption { Percentage = percentage }).ToList(); MathUtils.NormalizePercentages(options); diff --git a/src/VSPoll.API.UnitTest/VSPoll.API.UnitTest.csproj b/src/VSPoll.API.UnitTest/VSPoll.API.UnitTest.csproj index a2d072c..9f7c7e9 100644 --- a/src/VSPoll.API.UnitTest/VSPoll.API.UnitTest.csproj +++ b/src/VSPoll.API.UnitTest/VSPoll.API.UnitTest.csproj @@ -1,7 +1,7 @@  - net7 + net8 false diff --git a/src/VSPoll.API/Controllers/OptionController.cs b/src/VSPoll.API/Controllers/OptionController.cs index e2d33fd..f70a093 100644 --- a/src/VSPoll.API/Controllers/OptionController.cs +++ b/src/VSPoll.API/Controllers/OptionController.cs @@ -15,18 +15,8 @@ namespace VSPoll.API.Controllers; #pragma warning disable IDE0079 [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract")] #pragma warning restore IDE0079 -public class OptionController : Controller +public class OptionController(IPollService pollService, IOptionService optionService, IUserService userService) : ControllerBase { - private readonly IPollService pollService; - private readonly IOptionService optionService; - private readonly IUserService userService; - public OptionController(IPollService pollService, IOptionService optionService, IUserService userService) - { - this.pollService = pollService; - this.optionService = optionService; - this.userService = userService; - } - /// /// Gets the voters of an option /// diff --git a/src/VSPoll.API/Controllers/PollController.cs b/src/VSPoll.API/Controllers/PollController.cs index b2896ba..f4be279 100644 --- a/src/VSPoll.API/Controllers/PollController.cs +++ b/src/VSPoll.API/Controllers/PollController.cs @@ -18,18 +18,8 @@ namespace VSPoll.API.Controllers; #pragma warning disable IDE0079 [SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract")] #pragma warning restore IDE0079 -public class PollController : Controller +public class PollController(IPollService pollService, IOptionService optionService, IUserService userService) : ControllerBase { - private readonly IPollService pollService; - private readonly IOptionService optionService; - private readonly IUserService userService; - public PollController(IPollService pollService, IOptionService optionService, IUserService userService) - { - this.pollService = pollService; - this.optionService = optionService; - this.userService = userService; - } - /// /// Gets data from a poll /// diff --git a/src/VSPoll.API/Dockerfile b/src/VSPoll.API/Dockerfile index 40bbdee..4375940 100644 --- a/src/VSPoll.API/Dockerfile +++ b/src/VSPoll.API/Dockerfile @@ -1,11 +1,11 @@ #See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. -FROM mcr.microsoft.com/dotnet/aspnet:7.0-alpine AS base +FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine AS base WORKDIR /app EXPOSE 80 EXPOSE 443 -FROM mcr.microsoft.com/dotnet/sdk:7.0-alpine AS build +FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build WORKDIR /src COPY ["VSPoll.API.csproj", "VSPoll.API/"] RUN dotnet restore "VSPoll.API/VSPoll.API.csproj" diff --git a/src/VSPoll.API/Persistence/Contexts/PollContext.cs b/src/VSPoll.API/Persistence/Contexts/PollContext.cs index d19f8d8..bce2295 100644 --- a/src/VSPoll.API/Persistence/Contexts/PollContext.cs +++ b/src/VSPoll.API/Persistence/Contexts/PollContext.cs @@ -3,7 +3,7 @@ namespace VSPoll.API.Persistence.Contexts; -public class PollContext : DbContext +public class PollContext(DbContextOptions contextOptions) : DbContext(contextOptions) { public DbSet Polls { get; set; } = null!; public DbSet PollOptions { get; set; } = null!; @@ -12,6 +12,4 @@ public class PollContext : DbContext protected override void OnModelCreating(ModelBuilder modelBuilder) => modelBuilder.Entity().HasKey(vote => new { vote.OptionId, vote.UserId }); - - public PollContext(DbContextOptions contextOptions) : base(contextOptions) { } } diff --git a/src/VSPoll.API/Persistence/Repositories/OptionRepository.cs b/src/VSPoll.API/Persistence/Repositories/OptionRepository.cs index a03b07e..55194b6 100644 --- a/src/VSPoll.API/Persistence/Repositories/OptionRepository.cs +++ b/src/VSPoll.API/Persistence/Repositories/OptionRepository.cs @@ -8,11 +8,8 @@ namespace VSPoll.API.Persistence.Repositories; -public class OptionRepository : IOptionRepository +public class OptionRepository(PollContext context) : IOptionRepository { - private readonly PollContext context; - public OptionRepository(PollContext context) => this.context = context; - public Task CheckIfOptionExists(Guid id) => context.PollOptions.AnyAsync(option => option.Id == id); diff --git a/src/VSPoll.API/Persistence/Repositories/PollRepository.cs b/src/VSPoll.API/Persistence/Repositories/PollRepository.cs index 4db5a3f..ee137de 100644 --- a/src/VSPoll.API/Persistence/Repositories/PollRepository.cs +++ b/src/VSPoll.API/Persistence/Repositories/PollRepository.cs @@ -8,11 +8,8 @@ namespace VSPoll.API.Persistence.Repositories; -public class PollRepository : IPollRepository +public class PollRepository(PollContext context) : IPollRepository { - private readonly PollContext context; - public PollRepository(PollContext context) => this.context = context; - public Task CheckIfPollExists(Guid id) => context.Polls.AnyAsync(poll => poll.Id == id); diff --git a/src/VSPoll.API/Persistence/Repositories/UserRepository.cs b/src/VSPoll.API/Persistence/Repositories/UserRepository.cs index 68a7390..ccc2f6b 100644 --- a/src/VSPoll.API/Persistence/Repositories/UserRepository.cs +++ b/src/VSPoll.API/Persistence/Repositories/UserRepository.cs @@ -5,11 +5,8 @@ namespace VSPoll.API.Persistence.Repositories; -public class UserRepository : IUserRepository +public class UserRepository(PollContext context) : IUserRepository { - private readonly PollContext context; - public UserRepository(PollContext context) => this.context = context; - public async Task AddOrUpdateUserAsync(User user) { if (await context.Users.AnyAsync(u => u.Id == user.Id)) diff --git a/src/VSPoll.API/Services/OptionService.cs b/src/VSPoll.API/Services/OptionService.cs index 3ccb5ca..9856b34 100644 --- a/src/VSPoll.API/Services/OptionService.cs +++ b/src/VSPoll.API/Services/OptionService.cs @@ -7,12 +7,8 @@ namespace VSPoll.API.Services; -public class OptionService : IOptionService +public class OptionService(IOptionRepository optionRepository) : IOptionService { - private readonly IOptionRepository optionRepository; - public OptionService(IOptionRepository optionRepository) - => this.optionRepository = optionRepository; - public Task CheckIfOptionExistsAsync(Guid id) => optionRepository.CheckIfOptionExists(id); diff --git a/src/VSPoll.API/Services/PollService.cs b/src/VSPoll.API/Services/PollService.cs index be39210..c129b5c 100644 --- a/src/VSPoll.API/Services/PollService.cs +++ b/src/VSPoll.API/Services/PollService.cs @@ -11,16 +11,8 @@ namespace VSPoll.API.Services; -public class PollService : IPollService +public class PollService(IPollRepository pollRepository, IOptionRepository optionRepository) : IPollService { - private readonly IPollRepository pollRepository; - private readonly IOptionRepository optionRepository; - public PollService(IPollRepository pollRepository, IOptionRepository optionRepository) - { - this.pollRepository = pollRepository; - this.optionRepository = optionRepository; - } - public Task CheckIfPollExistsAsync(Guid id) => pollRepository.CheckIfPollExists(id); diff --git a/src/VSPoll.API/Services/UserService.cs b/src/VSPoll.API/Services/UserService.cs index 6cc16a4..4024a2f 100644 --- a/src/VSPoll.API/Services/UserService.cs +++ b/src/VSPoll.API/Services/UserService.cs @@ -10,16 +10,8 @@ namespace VSPoll.API.Services; -public class UserService : IUserService +public class UserService(IUserRepository userRepository, IConfiguration configuration) : IUserService { - private readonly IUserRepository userRepository; - private readonly IConfiguration configuration; - public UserService(IUserRepository userRepository, IConfiguration configuration) - { - this.userRepository = userRepository; - this.configuration = configuration; - } - public bool Authenticate(Authentication authentication, [NotNullWhen(false)] out string? error) { var dataCheck = $"auth_date={authentication.AuthDate}\nfirst_name={authentication.FirstName}\nid={authentication.Id}"; @@ -32,7 +24,7 @@ public bool Authenticate(Authentication authentication, [NotNullWhen(false)] out if (authentication.Username is not null) dataCheck += $"\nusername={authentication.Username}"; - var token = configuration.GetSection("Secrets").GetValue("BotToken"); + var token = configuration.GetSection("Secrets").GetValue("BotToken")!; var secretKey = SHA256.HashData(Encoding.UTF8.GetBytes(token)); using HMACSHA256 hmac = new(secretKey); var testHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(dataCheck)); diff --git a/src/VSPoll.API/VSPoll.API.csproj b/src/VSPoll.API/VSPoll.API.csproj index c84e9b7..db9b9f9 100644 --- a/src/VSPoll.API/VSPoll.API.csproj +++ b/src/VSPoll.API/VSPoll.API.csproj @@ -1,7 +1,7 @@  - net7 + net8 Linux enable