Skip to content

Commit 5770677

Browse files
committed
add employee filtering by position & enum support for swagger
1 parent 943b75c commit 5770677

File tree

6 files changed

+24
-36
lines changed

6 files changed

+24
-36
lines changed

CheckDrive.Api/CheckDrive.Api/CheckDrive.Api.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
2525
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
2626
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.8.0" />
27+
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.9.0" />
2728
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="27.1.50" />
2829
</ItemGroup>
2930

CheckDrive.Api/CheckDrive.Api/Controllers/AccountsController.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using CheckDrive.Application.Constants;
22
using CheckDrive.Application.DTOs.Account;
33
using CheckDrive.Application.Interfaces;
4+
using CheckDrive.Domain.Enums;
45
using Microsoft.AspNetCore.Authorization;
56
using Microsoft.AspNetCore.Mvc;
67

@@ -18,9 +19,9 @@ public AccountsController(IAccountService service)
1819
}
1920

2021
[HttpGet]
21-
public async Task<ActionResult<List<AccountDto>>> GetAsync()
22+
public async Task<ActionResult<List<AccountDto>>> GetAsync(EmployeePosition? position)
2223
{
23-
var accounts = await _service.GetAsync();
24+
var accounts = await _service.GetAsync(position);
2425

2526
return Ok(accounts);
2627
}

CheckDrive.Api/CheckDrive.Api/Extensions/DependencyInjection.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using Newtonsoft.Json.Converters;
1010
using Newtonsoft.Json.Serialization;
1111
using System.Text;
12-
using CheckDrive.Api.Filters;
1312

1413
namespace CheckDrive.Api.Extensions;
1514

@@ -56,15 +55,19 @@ private static void AddSwagger(IServiceCollection services)
5655
.AddSwaggerGen(setup =>
5756
{
5857
setup.SwaggerDoc("v1", new OpenApiInfo { Title = "Check-Drive API", Version = "v1" });
59-
60-
setup.SchemaFilter<EnumSchemaFilter>();
61-
});
58+
})
59+
.AddSwaggerGenNewtonsoftSupport();
6260
}
6361

6462
private static void AddAuthentication(IServiceCollection services, IConfiguration configuration)
6563
{
6664
var jwtOptions = configuration.GetSection(nameof(JwtOptions)).Get<JwtOptions>();
6765

66+
if (jwtOptions is null)
67+
{
68+
throw new InvalidOperationException("Could not load JWT configurations.");
69+
}
70+
6871
services
6972
.AddAuthentication(options =>
7073
{
@@ -84,7 +87,7 @@ private static void AddAuthentication(IServiceCollection services, IConfiguratio
8487
ValidateLifetime = true,
8588
ValidateIssuerSigningKey = true,
8689
IssuerSigningKey = new SymmetricSecurityKey(
87-
Encoding.UTF8.GetBytes(jwtOptions!.SecretKey))
90+
Encoding.UTF8.GetBytes(jwtOptions.SecretKey))
8891
};
8992

9093
options.Events = new JwtBearerEvents

CheckDrive.Api/CheckDrive.Api/Filters/EnumSchemaFilter.cs

-23
This file was deleted.

CheckDrive.Api/CheckDrive.Application/Interfaces/IAccountService.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using CheckDrive.Application.DTOs.Account;
2+
using CheckDrive.Domain.Enums;
23

34
namespace CheckDrive.Application.Interfaces;
45

56
public interface IAccountService
67
{
7-
Task<List<AccountDto>> GetAsync();
8+
Task<List<AccountDto>> GetAsync(EmployeePosition? position);
89
Task<AccountDto> GetByIdAsync(string id);
910
Task<AccountDto> CreateAsync(CreateAccountDto account);
1011
Task<AccountDto> UpdateAsync(UpdateAccountDto account);

CheckDrive.Api/CheckDrive.Application/Services/AccountService.cs

+10-5
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ public AccountService(ICheckDriveDbContext context, IMapper mapper, UserManager<
2424
_userManager = userManager ?? throw new ArgumentNullException(nameof(userManager));
2525
}
2626

27-
public async Task<List<AccountDto>> GetAsync()
27+
public async Task<List<AccountDto>> GetAsync(EmployeePosition? position)
2828
{
29-
var accounts = await _context.Employees
29+
var query = _context.Employees
3030
.AsNoTracking()
31-
.Include(x => x.Account)
32-
.ToListAsync();
31+
.AsQueryable();
32+
33+
if (position.HasValue)
34+
{
35+
query = query.Where(x => x.Position == position.Value);
36+
}
3337

38+
var accounts = await query.ToListAsync();
3439
var dtos = _mapper.Map<List<AccountDto>>(accounts);
3540

3641
return dtos;
@@ -100,7 +105,7 @@ public async Task DeleteAsync(string id)
100105
await _context.SaveChangesAsync();
101106
}
102107

103-
private async Task AssignToRoleAsync(IdentityUser user,EmployeePosition position)
108+
private async Task AssignToRoleAsync(IdentityUser user, EmployeePosition position)
104109
{
105110
var role = position switch
106111
{

0 commit comments

Comments
 (0)