Skip to content

Commit

Permalink
Merge pull request #23 from CS3203-SEP-21-Group-22/dev1
Browse files Browse the repository at this point in the history
Validation not working debug attempt
  • Loading branch information
SharadaShehan authored Sep 20, 2024
2 parents e1ec192 + 76cad29 commit 23a2ba9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 7 additions & 2 deletions IMS.Presentation/Controllers/AdminControllers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using FluentValidation;
using IMS.Application.DTO;
using IMS.Application.Services;
using IMS.Infrastructure.Services;
Expand All @@ -15,19 +16,22 @@ public class AdminController : ControllerBase
private readonly DataBaseContext _dbContext;
private readonly ITokenParser _tokenParser;
private readonly ILogger<AdminController> _logger;
private readonly IValidator<UpdateLabDTO> _updateLabDTOValidator;
private readonly UserService _userService;
private readonly LabService _labService;

public AdminController(
DataBaseContext dbContext,
ILogger<AdminController> logger,
IValidator<UpdateLabDTO> updateLabDTOValidator,
ITokenParser tokenParser,
UserService userService,
LabService labService
)
{
_dbContext = dbContext;
_logger = logger;
_updateLabDTOValidator = updateLabDTOValidator;
_tokenParser = tokenParser;
_userService = userService;
_labService = labService;
Expand Down Expand Up @@ -95,8 +99,9 @@ public async Task<ActionResult<LabDTO>> UpdateLab(int id, UpdateLabDTO updateLab
try
{
// Validate the DTO
if (!ModelState.IsValid)
BadRequest(ModelState);
var result = await _updateLabDTOValidator.ValidateAsync(updateLabDTO);
if (!result.IsValid)
return BadRequest(result.Errors);
// Update the Lab
ResponseDTO<LabDTO> responseDTO = _labService.UpdateLab(id, updateLabDTO);
if (!responseDTO.success)
Expand Down
11 changes: 6 additions & 5 deletions IMS.Presentation/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FluentValidation;
using FluentValidation.AspNetCore;
using IMS.Application.DTO;
using IMS.Application.Interfaces;
using IMS.Application.Services;
using IMS.Infrastructure.Extensions;
Expand Down Expand Up @@ -31,11 +32,11 @@

builder.Services.AddControllers();

//builder.Services.AddFluentValidationAutoValidation();
builder
.Services.AddFluentValidationAutoValidation()
.AddFluentValidationClientsideAdapters()
.AddValidatorsFromAssemblyContaining<UpdateLabValidator>();
builder.Services.AddFluentValidationAutoValidation();
builder.Services.AddFluentValidationClientsideAdapters();
builder.Services.AddValidatorsFromAssemblyContaining<UpdateLabValidator>();
builder.Services.AddScoped<IValidator<UpdateLabDTO>, UpdateLabValidator>();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(opt =>
{
Expand Down

0 comments on commit 23a2ba9

Please sign in to comment.