InvitationValidator

This commit is contained in:
Artemy
2025-06-24 15:24:14 +07:00
parent 672fc95823
commit 6e856a65db
4 changed files with 58 additions and 3 deletions
@@ -0,0 +1,7 @@
namespace Govor.API.Tests.UnitTests.Infrastructure.Validators;
[TestFixture]
public class InvitationValidatorTests
{
}
-2
View File
@@ -134,8 +134,6 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI(); app.UseSwaggerUI();
} }
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseRouting(); app.UseRouting();
@@ -0,0 +1,40 @@
using Govor.Core.Models;
namespace Govor.Core.Infrastructure.Validators;
public class InvitationValidator : IObjectValidator<Invitation>
{
public void Validate(Invitation inv)
{
try
{
if(inv is null)
throw new ArgumentNullException(nameof(inv));
if(inv.Id == Guid.Empty)
throw new ArgumentException("Id cannot be empty", nameof(inv.Id));
if(inv.DateCreated == DateTime.MinValue)
throw new ArgumentException("DateCreated cannot be empty", nameof(inv.DateCreated));
if(inv.EndDate < inv.DateCreated)
throw new ArgumentException("EndDate cannot be less than StartDate", nameof(inv.EndDate));
if(inv.MaxParticipants <= 0)
throw new ArgumentException("MaxParticipants cannot be less than 0", nameof(inv.MaxParticipants));
}
catch (Exception ex)
{
throw new InvalidObjectException<Invitation>(ex);
}
}
public bool TryValidate(Invitation inv)
{
try
{
Validate(inv);
return true;
}
catch (Exception e)
{
return false;
}
}
}
+11 -1
View File
@@ -1,3 +1,4 @@
using Govor.Core.Infrastructure.Validators;
using Govor.Core.Models; using Govor.Core.Models;
using Govor.Core.Repositories.Invaites; using Govor.Core.Repositories.Invaites;
@@ -5,7 +6,16 @@ namespace Govor.Data.Repositories;
public class InvitesRepository : IInvitesRepository public class InvitesRepository : IInvitesRepository
{ {
public Task<List<Invitation>> GetAllAsync() private GovorDbContext _context;
private IObjectValidator<Invitation> _validator;
public InvitesRepository(GovorDbContext context, IObjectValidator<Invitation> validator)
{
_context = context;
_validator = validator;
}
public async Task<List<Invitation>> GetAllAsync()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }