Skip to content

Commit d5aa869

Browse files
committed
Refactor Log Controller / Services
- Removed AllowAnonymous attribute in Update under Employee feature - Added model validation in LogEditViewModel
1 parent 9f2763d commit d5aa869

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

src/Api/Features/Employees/EmployeeController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public async Task<EmployeeViewModel> Details(Guid id)
3434

3535
// PUT api/employee
3636
[HttpPut]
37-
[AllowAnonymous]
3837
public async Task<IActionResult> Update(EmployeeViewModel model)
3938
{
4039
// Check if Card No already exists

src/Api/Features/Logs/LogController.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ await _mediator.Send(new List.Query(parameters))
3333
}
3434

3535
// GET: api/log/{id}
36+
[Authorize(Roles = "Admin")]
3637
[HttpGet("{id:guid}")]
3738
public async Task<LogViewModel> Details(Guid id)
3839
{
@@ -46,8 +47,8 @@ public async Task<IActionResult> Log(LogInOutViewModel viewModel)
4647
{
4748
// Validate card no. & password
4849
var user = await _mediator.Send(new ValidateTimeInOut.Query(viewModel));
49-
if (user.Id == Guid.Empty)
50-
return BadRequest("Invalid username or password!");
50+
if (user == null)
51+
return BadRequest("Invalid card no. or password!");
5152

5253
// Broadcast to web client
5354
await _hubContext.Clients.All.SendAsync("employee-logged");
@@ -61,9 +62,14 @@ await _mediator.Send(new RecordLog.Command(user))
6162
// PUT api/log
6263
[Authorize(Roles = "Admin")]
6364
[HttpPut]
64-
public async Task<LogViewModel> Update(LogEditViewModel model)
65+
public async Task<ActionResult<LogViewModel>> Update(LogEditViewModel model)
6566
{
66-
return await _mediator.Send(new Update.Command(model));
67+
var result = await _mediator.Send(new Update.Command(model));
68+
69+
if (result == null)
70+
return BadRequest();
71+
72+
return result;
6773
}
6874
}
6975
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
2+
using System.ComponentModel.DataAnnotations;
23
using WebApi.Entities;
34

45
namespace WebApi.Features.Logs
56
{
6-
public class LogEditViewModel : BaseEntity
7+
public class LogEditViewModel
78
{
8-
public Guid EmployeeId { get; set; }
9+
public Guid Id { get; set; }
10+
[Required]
911
public DateTime TimeIn { get; set; }
1012
public DateTime? TimeOut { get; set; }
11-
public string FullName { get; set; }
1213
}
1314
}

src/Api/Features/Logs/Update.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,21 @@ public async Task<LogViewModel> Handle(Command request, CancellationToken cancel
4242
{
4343
try
4444
{
45-
var model = await _context.Logs.FindAsync(request.ViewModel.Id);
45+
var model = await _context.Logs.FirstOrDefaultAsync(m => m.Id == request.ViewModel.Id);
46+
47+
// Validate model
48+
if (model == null)
49+
return null;
4650

4751
// Convert datetime to UTC before updating
4852
request.ViewModel.TimeIn = LogExtensions.ToUtc(request.ViewModel.TimeIn.ToString());
4953
request.ViewModel.TimeOut = (request.ViewModel.TimeOut != null)
5054
? LogExtensions.ToUtc(request.ViewModel.TimeOut.ToString())
5155
: request.ViewModel.TimeOut;
5256

53-
_mapper.Map(request.ViewModel, model);
57+
// Update specifc fields only
58+
model.TimeIn = request.ViewModel.TimeIn;
59+
model.TimeOut = request.ViewModel.TimeOut;
5460
model.Updated = DateTime.UtcNow;
5561

5662
_context.Entry(model).State = EntityState.Modified;

src/Api/Features/Logs/ValidateTimeInOut.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public async Task<EmployeeViewModel> Handle(Query request, CancellationToken can
5858
.Where(m => m.Deleted == null)
5959
.SingleOrDefaultAsync(cancellationToken);
6060

61-
// Check if employee exist
61+
// Check if employee exists
6262
if (emp == null)
63-
return new EmployeeViewModel{ Id = Guid.Empty };
63+
return null;
6464

6565
// Check if password is correct
6666
var user = await _manager.FindByIdAsync(emp.IdentityId);
6767
if (!await _manager.CheckPasswordAsync(user, request.ViewModel.Password))
68-
return new EmployeeViewModel{ Id = Guid.Empty };
68+
return null;
6969

7070
return _mapper.Map<EmployeeViewModel>(emp);
7171
}

0 commit comments

Comments
 (0)