Skip to content

Commit 88f3962

Browse files
committed
Refactor Configurations to Config Feature
- Adjusted model, dbcontext, seed - Updated migrations
1 parent aea493f commit 88f3962

File tree

13 files changed

+90
-76
lines changed

13 files changed

+90
-76
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace WebApi.Entities
44
{
5-
public class Configuration
5+
public class Config
66
{
77
public Guid Id { get; set; }
88
public string TimeIn { get; set; }
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Authorization;
4+
using MediatR;
5+
6+
namespace WebApi.Features.Config
7+
{
8+
[Authorize(Roles = "Admin")]
9+
[Route("api/[controller]"), ApiController]
10+
public class ConfigController : ControllerBase
11+
{
12+
private readonly IMediator _mediator;
13+
14+
public ConfigController(IMediator mediator)
15+
{
16+
_mediator = mediator;
17+
}
18+
19+
// GET api/config
20+
[HttpGet]
21+
public async Task<ConfigViewModel> Index()
22+
{
23+
return await _mediator.Send(new Details.Query());
24+
}
25+
26+
// PUT api/config
27+
[HttpPut]
28+
public async Task<ConfigViewModel> Update(ConfigViewModel viewModel)
29+
{
30+
return await _mediator.Send(new Update.Command(viewModel));
31+
}
32+
}
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using WebApi.Entities;
2+
using AutoMapper;
3+
4+
namespace WebApi.Features.Config
5+
{
6+
public class ConfigProfile : Profile
7+
{
8+
public ConfigProfile()
9+
{
10+
CreateMap<Entities.Config, ConfigViewModel>();
11+
CreateMap<ConfigViewModel, Entities.Config>();
12+
}
13+
}
14+
}

WebApi/Features/Configurations/ConfigurationViewModel.cs renamed to WebApi/Features/Config/ConfigViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
22

3-
namespace WebApi.Features.Configurations
3+
namespace WebApi.Features.Config
44
{
5-
public class ConfigurationViewModel
5+
public class ConfigViewModel
66
{
77
public Guid Id { get; set; }
88
public string TimeIn { get; set; }
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
using Microsoft.EntityFrameworkCore;
77
using WebApi.Infrastructure;
88

9-
namespace WebApi.Features.Configurations
9+
namespace WebApi.Features.Config
1010
{
11+
/// <summary>
12+
/// Get current attendance config
13+
/// </summary>
1114
public class Details
1215
{
13-
public class Query : IRequest<ConfigurationViewModel> { }
16+
public class Query : IRequest<ConfigViewModel> { }
1417

15-
public class QueryHandler : IRequestHandler<Query, ConfigurationViewModel>
18+
public class QueryHandler : IRequestHandler<Query, ConfigViewModel>
1619
{
1720
private readonly ApplicationDbContext _context;
1821
private readonly IMapper _mapper;
@@ -23,13 +26,16 @@ public QueryHandler(ApplicationDbContext context, IMapper mapper)
2326
_mapper = mapper;
2427
}
2528

26-
public async Task<ConfigurationViewModel> Handle(Query request, CancellationToken cancellationToken)
29+
public async Task<ConfigViewModel> Handle(Query request, CancellationToken cancellationToken)
2730
{
2831

2932
try
3033
{
31-
var model = await _context.Configurations.FirstOrDefaultAsync(cancellationToken);
32-
return _mapper.Map<ConfigurationViewModel>(model);
34+
// Get the current config model
35+
var model = await _context.Config.FirstOrDefaultAsync(cancellationToken);
36+
37+
// Map model to view model
38+
return _mapper.Map<ConfigViewModel>(model);
3339
}
3440
catch (Exception e)
3541
{
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@
66
using Microsoft.EntityFrameworkCore;
77
using WebApi.Infrastructure;
88

9-
namespace WebApi.Features.Configurations
9+
namespace WebApi.Features.Config
1010
{
11+
/// <summary>
12+
/// Update attendance config
13+
/// </summary>
1114
public class Update
1215
{
13-
public class Command : IRequest
16+
public class Command : IRequest<ConfigViewModel>
1417
{
15-
public Command(ConfigurationViewModel viewModel)
18+
public Command(ConfigViewModel viewModel)
1619
{
1720
ViewModel = viewModel;
1821
}
1922

20-
public ConfigurationViewModel ViewModel { get; }
23+
public ConfigViewModel ViewModel { get; }
2124
}
2225

23-
public class CommandHandler : AsyncRequestHandler<Command>
26+
public class CommandHandler : IRequestHandler<Command, ConfigViewModel>
2427
{
2528

2629
private readonly ApplicationDbContext _context;
@@ -32,15 +35,22 @@ public CommandHandler(ApplicationDbContext context, IMapper mapper)
3235
_mapper = mapper;
3336
}
3437

35-
protected override async Task Handle(Command request, CancellationToken cancellationToken)
38+
public async Task<ConfigViewModel> Handle(Command request, CancellationToken cancellationToken)
3639
{
3740
try
3841
{
39-
var model = await _context.Configurations.FindAsync(request.ViewModel.Id);
42+
// Get the old model
43+
var model = await _context.Config.FirstAsync();
44+
45+
// Map the view model into old model
4046
_mapper.Map(request.ViewModel, model);
4147

48+
// Change entity state to update
4249
_context.Entry(model).State = EntityState.Modified;
4350
await _context.SaveChangesAsync();
51+
52+
// Map the result to view model
53+
return _mapper.Map<ConfigViewModel>(model);
4454
}
4555
catch (Exception e)
4656
{

WebApi/Features/Configurations/ConfigurationController.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.

WebApi/Features/Configurations/ConfigurationProfile.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

WebApi/Infrastructure/ApplicationDbContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public ApplicationDbContext(DbContextOptions options) : base(options)
1313

1414
public DbSet<Employee> Employees { get; set; }
1515
public DbSet<Log> Logs { get; set; }
16-
17-
public DbSet<Configuration> Configurations { get; set; }
16+
public DbSet<Config> Config { get; set; }
1817
}
1918
}

WebApi/Migrations/20180930045058_init.Designer.cs renamed to WebApi/Migrations/20181014061742_init.Designer.cs

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)