-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22e52e6
commit 19b01d4
Showing
5 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 12 additions & 1 deletion
13
HR.LeaveManagement.Application/Exceptions/BadRequestException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,20 @@ | ||
namespace HR.LeaveManagement.Application.Exceptions; | ||
using FluentValidation.Results; | ||
|
||
namespace HR.LeaveManagement.Application.Exceptions; | ||
|
||
public class BadRequestException : Exception | ||
{ | ||
public BadRequestException(string message) : base(message) | ||
{ | ||
|
||
} | ||
|
||
public BadRequestException(string message, ValidationResult validationResult) : base(message) | ||
{ | ||
Errors = new(); | ||
foreach (var error in validationResult.Errors) | ||
Errors.Add(error.ErrorMessage); | ||
} | ||
|
||
public List<string> Errors { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...pplication/Features/LeaveType/Commands/CreateLeaveType/CreateLeaveTypeCommandValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using FluentValidation; | ||
|
||
using HR.LeaveManagement.Application.Contracts.Persistence; | ||
|
||
namespace HR.LeaveManagement.Application.Features.LeaveType.Commands.CreateLeaveType; | ||
public class CreateLeaveTypeCommandValidator : AbstractValidator<CreateLeaveTypeCommand> | ||
{ | ||
private readonly ILeaveTypeRepository _leaveTypeRepository; | ||
|
||
public CreateLeaveTypeCommandValidator(ILeaveTypeRepository leaveTypeRepository) | ||
{ | ||
RuleFor(p => p.Name) | ||
.NotEmpty().WithMessage("{PropertyName} is required") | ||
.NotNull() | ||
.MaximumLength(70).WithMessage("{PropertyName} must be fewer than 70 characters"); | ||
|
||
RuleFor(p => p.DefaultDays) | ||
.LessThan(100).WithMessage("{PropertyName} cannot exceed 100") | ||
.GreaterThan(1).WithMessage("{PropertyName} cannot be less than 1"); | ||
|
||
RuleFor(q => q) | ||
.MustAsync(LeaveTypeNameUnique) | ||
.WithMessage("Leave type already exists!"); | ||
|
||
_leaveTypeRepository = leaveTypeRepository; | ||
} | ||
|
||
private Task<bool> LeaveTypeNameUnique(CreateLeaveTypeCommand command, CancellationToken ctoken) | ||
{ | ||
return _leaveTypeRepository.IsLeaveTypeUnique(command.Name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters