I learned how to use the fluent validator I would like to know if you could help me with a question. I have a personal system where in my controller the create post method has the following code:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create (TicketViewModel model)
{
ICollection <Piece> pieces;
try
{
if (ModelState.IsValid)
{
if (_ticketRepository.GetById (model.Id)! = null)
{
Console.WriteLine ("Ticket already exists!");
}
var _model = _mapper.Map <Ticket> (model);
var count = _ticketRepository.FindAllByPiece (_model);
if (count> 0)
{
ModelState.AddModelError ("", "This ticket already exists for this room. Check the piece, date and time.");
pieces = _pieceRepository.FindAll ();
model.Pieces = pieces;
return View (model);
}
_ticketRepository.Insert (_model);
model.Seats = RegistrationSeats (model.QuantityOfSeats);
return RedirectToAction ("Index");
}
pieces = _pieceRepository.FindAll ();
model.Pieces = pieces;
return View (model);
}
catch (Exception ex)
{
Console.WriteLine (ex.Message);
return View (model);
}
}
Note that in the code above I have a part like this:
var count = _ticketRepository.FindAllByPiece (_model);
if (count> 0)
{
ModelState.AddModelError ("", "This ticket already exists for this room. Check the piece, date and time.");
pieces = _pieceRepository.FindAll ();
model.Pieces = pieces;
return View (model);
}
- This code snippet calls a service from the repository that checks if the model (ticket) passed has the same partId, date and schedule as the one already in the bank. If there is a record with these same values, he does not add the ticket until the person creates a different one than the one that already exists.
- Here is the code of the service that accesses the bank doing this search:
public int FindAllByPiece(Ticket model)
{
return _saleTheaterTicketsContext.Tickets.Include(x => x.Piece).Where(x => x.PieceId == model.PieceId && x.Date == model.Date && x.Schedule == model.Schedule).Count();
}
- This works well and to show in the View the message I added in
ModelState.AddModelErrorI do like this in View:
<strong class = "text-danger">@ Html.ValidationSummary (true)</strong>
- My question is as I am using FluentValidation, I would like to know if there is any way to do this with it, so that the controller is not full of things. I did some research, but to no avail. I tried to instantiate the repository in the Validation class (which I will put the code below) but I don't know how to do the validation for these 3 fields together, passing a model to the repository. Would you help me?
- Here is code from the Validation class using FluentValidator:
public class TicketViewModelValidator: AbstractValidator <TicketViewModel>
{
public TicketViewModelValidator ()
{
RuleFor (x => x.Price)
.NotEmpty (). WithMessage ("Enter the price")
.GreaterThan (0) .WithMessage ("The price must be greater than 0.00");
RuleFor (x => x.QuantityOfSeats)
.NotEmpty (). WithMessage ("Enter the number of seats")
.GreaterThanOrEqualTo (10) .WithMessage ("The number of seats must be at least 10");
RuleFor (x => x.Date)
.NotEmpty (). WithMessage ("Enter Date")
.Must (ValidDate) .WithMessage ("Date must be greater than or equal to today");
RuleFor (x => x.Schedule)
.NotEmpty (). WithMessage ("Enter time");
RuleFor (x => x.PieceId)
.NotEmpty (). WithMessage ("Select the part");
}
public static bool ValidDate (DateTime date)
{
if (date.Date> = DateTime.Now.Date)
{
return true;
}
return false;
}
}