From 6e856a65dbca9003c20e77c56a6857337028d35f Mon Sep 17 00:00:00 2001 From: Artemy <109195690+stalcker2288969@users.noreply.github.com> Date: Tue, 24 Jun 2025 15:24:14 +0700 Subject: [PATCH] InvitationValidator --- .../Validators/InvitationValidatorTests.cs | 7 ++++ Govor.API/Program.cs | 2 - .../Validators/InvitationValidator.cs | 40 +++++++++++++++++++ Govor.Data/Repositories/InvitesRepository.cs | 12 +++++- 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 Govor.API.Tests/UnitTests/Infrastructure/Validators/InvitationValidatorTests.cs create mode 100644 Govor.Core/Infrastructure/Validators/InvitationValidator.cs diff --git a/Govor.API.Tests/UnitTests/Infrastructure/Validators/InvitationValidatorTests.cs b/Govor.API.Tests/UnitTests/Infrastructure/Validators/InvitationValidatorTests.cs new file mode 100644 index 0000000..e0f2314 --- /dev/null +++ b/Govor.API.Tests/UnitTests/Infrastructure/Validators/InvitationValidatorTests.cs @@ -0,0 +1,7 @@ +namespace Govor.API.Tests.UnitTests.Infrastructure.Validators; + +[TestFixture] +public class InvitationValidatorTests +{ + +} \ No newline at end of file diff --git a/Govor.API/Program.cs b/Govor.API/Program.cs index 6876745..0186809 100644 --- a/Govor.API/Program.cs +++ b/Govor.API/Program.cs @@ -134,8 +134,6 @@ if (app.Environment.IsDevelopment()) app.UseSwaggerUI(); } - - app.UseHttpsRedirection(); app.UseRouting(); diff --git a/Govor.Core/Infrastructure/Validators/InvitationValidator.cs b/Govor.Core/Infrastructure/Validators/InvitationValidator.cs new file mode 100644 index 0000000..813da99 --- /dev/null +++ b/Govor.Core/Infrastructure/Validators/InvitationValidator.cs @@ -0,0 +1,40 @@ +using Govor.Core.Models; + +namespace Govor.Core.Infrastructure.Validators; + +public class InvitationValidator : IObjectValidator +{ + 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(ex); + } + } + + public bool TryValidate(Invitation inv) + { + try + { + Validate(inv); + return true; + } + catch (Exception e) + { + return false; + } + } +} \ No newline at end of file diff --git a/Govor.Data/Repositories/InvitesRepository.cs b/Govor.Data/Repositories/InvitesRepository.cs index 0c7cc57..7b18448 100644 --- a/Govor.Data/Repositories/InvitesRepository.cs +++ b/Govor.Data/Repositories/InvitesRepository.cs @@ -1,3 +1,4 @@ +using Govor.Core.Infrastructure.Validators; using Govor.Core.Models; using Govor.Core.Repositories.Invaites; @@ -5,7 +6,16 @@ namespace Govor.Data.Repositories; public class InvitesRepository : IInvitesRepository { - public Task> GetAllAsync() + private GovorDbContext _context; + private IObjectValidator _validator; + + public InvitesRepository(GovorDbContext context, IObjectValidator validator) + { + _context = context; + _validator = validator; + } + + public async Task> GetAllAsync() { throw new NotImplementedException(); }