From 709fe8c8cb6e6dc7a15cd4525ea24b9376fcf78d Mon Sep 17 00:00:00 2001 From: vitaliyyuras Date: Fri, 22 Sep 2023 18:27:51 +0300 Subject: [PATCH 1/5] Added the RequiredIfAttribute class for property validation --- .../Validators/RequiredIfAttribute.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 OutOfSchool/OutOfSchool.WebApi/Validators/RequiredIfAttribute.cs diff --git a/OutOfSchool/OutOfSchool.WebApi/Validators/RequiredIfAttribute.cs b/OutOfSchool/OutOfSchool.WebApi/Validators/RequiredIfAttribute.cs new file mode 100644 index 0000000000..9a9233ba06 --- /dev/null +++ b/OutOfSchool/OutOfSchool.WebApi/Validators/RequiredIfAttribute.cs @@ -0,0 +1,38 @@ +using System.ComponentModel.DataAnnotations; + +namespace OutOfSchool.WebApi.Validators; + +public class RequiredIfAttribute : ValidationAttribute +{ + private readonly string otherProperty; + private readonly bool requiredValue; + + public RequiredIfAttribute(string otherProperty, bool requiredValue) + { + this.otherProperty = otherProperty; + this.requiredValue = requiredValue; + } + + protected override ValidationResult IsValid(object value, ValidationContext validationContext) + { + var propertyInfo = validationContext.ObjectType.GetProperty(otherProperty); + + if (propertyInfo == null) + { + return new ValidationResult($"Property {otherProperty} not found."); + } + + var otherPropertyValue = propertyInfo.GetValue(validationContext.ObjectInstance); + + if ((otherPropertyValue is bool otherValue) && otherValue == requiredValue) + { + if (value == null || string.IsNullOrWhiteSpace(value.ToString())) + { + return new ValidationResult(ErrorMessage); + } + } + + return ValidationResult.Success; + } +} + From f4dc67e1e7f2d053920d2e6dfe235ce1cbeb217f Mon Sep 17 00:00:00 2001 From: vitaliyyuras Date: Fri, 22 Sep 2023 18:28:45 +0300 Subject: [PATCH 2/5] Made phone number optional for provider unblocking. --- .../OutOfSchool.WebApi/Models/Providers/ProviderBlockDto.cs | 4 ++-- OutOfSchool/OutOfSchool.WebApi/Services/ProviderService.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderBlockDto.cs b/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderBlockDto.cs index 59af14ffe3..7e160a72c2 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderBlockDto.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderBlockDto.cs @@ -1,5 +1,5 @@ using System.ComponentModel.DataAnnotations; - +using OutOfSchool.WebApi.Validators; namespace OutOfSchool.WebApi.Models.Providers; public class ProviderBlockDto @@ -16,7 +16,7 @@ public class ProviderBlockDto ErrorMessage = Constants.PhoneErrorMessage)] [DisplayFormat(DataFormatString = Constants.PhoneNumberFormat)] [MaxLength(Constants.UnifiedPhoneLength)] - [Required(ErrorMessage = "PhoneNumber is required")] + [RequiredIf("IsBlocked", true, ErrorMessage = "PhoneNumber is required")] public string BlockPhoneNumber { get; set; } = string.Empty; [MaxLength(500)] diff --git a/OutOfSchool/OutOfSchool.WebApi/Services/ProviderService.cs b/OutOfSchool/OutOfSchool.WebApi/Services/ProviderService.cs index 9f3efe714e..a8f4690203 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Services/ProviderService.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Services/ProviderService.cs @@ -348,7 +348,7 @@ public async Task Block(ProviderBlockDto providerBlockDto, string t provider.IsBlocked = providerBlockDto.IsBlocked; provider.BlockReason = providerBlockDto.IsBlocked ? providerBlockDto.BlockReason : null; - provider.BlockPhoneNumber = providerBlockDto.IsBlocked ? providerBlockDto.BlockPhoneNumber : null; + provider.BlockPhoneNumber = providerBlockDto.IsBlocked ? providerBlockDto.BlockPhoneNumber : string.Empty; await providerRepository.RunInTransaction(async () => { From cdb9f654f11a2759bbe23e04dd994b55317713ae Mon Sep 17 00:00:00 2001 From: vitaliyyuras Date: Mon, 25 Sep 2023 16:43:44 +0300 Subject: [PATCH 3/5] Moved BlockPhoneNumber field from ProviderBaseDto to the ProviderDto model. --- .../Models/Providers/ProviderBaseDto.cs | 9 --------- .../OutOfSchool.WebApi/Models/Providers/ProviderDto.cs | 9 +++++++++ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderBaseDto.cs b/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderBaseDto.cs index a6f243d48f..918b290f4a 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderBaseDto.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderBaseDto.cs @@ -89,15 +89,6 @@ public class ProviderBaseDto public bool IsBlocked { get; set; } - [DataType(DataType.PhoneNumber)] - [RegularExpression( - Constants.PhoneNumberRegexModel, - ErrorMessage = Constants.PhoneErrorMessage)] - [DisplayFormat(DataFormatString = Constants.PhoneNumberFormat)] - [MaxLength(Constants.UnifiedPhoneLength)] - [Required(ErrorMessage = "PhoneNumber is required")] - public string BlockPhoneNumber { get; set; } = string.Empty; - [MaxLength(500)] public string BlockReason { get; set; } diff --git a/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderDto.cs b/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderDto.cs index 62b3068092..a370a5b596 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderDto.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderDto.cs @@ -8,4 +8,13 @@ public class ProviderDto : ProviderBaseDto [Required] [EnumDataType(typeof(OwnershipType), ErrorMessage = Constants.EnumErrorMessage)] public OwnershipType Ownership { get; set; } + + [DataType(DataType.PhoneNumber)] + [RegularExpression( + Constants.PhoneNumberRegexModel, + ErrorMessage = Constants.PhoneErrorMessage)] + [DisplayFormat(DataFormatString = Constants.PhoneNumberFormat)] + [MaxLength(Constants.UnifiedPhoneLength)] + [Required(ErrorMessage = "PhoneNumber is required")] + public string BlockPhoneNumber { get; set; } = string.Empty; } \ No newline at end of file From 1ecff271e373534f33fba3442e01b88a30655b4b Mon Sep 17 00:00:00 2001 From: vitaliyyuras Date: Mon, 25 Sep 2023 16:52:01 +0300 Subject: [PATCH 4/5] Removed all validations in BlockPhoneNumber. --- .../OutOfSchool.WebApi/Models/Providers/ProviderDto.cs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderDto.cs b/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderDto.cs index a370a5b596..8a814e22a9 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderDto.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Models/Providers/ProviderDto.cs @@ -9,12 +9,5 @@ public class ProviderDto : ProviderBaseDto [EnumDataType(typeof(OwnershipType), ErrorMessage = Constants.EnumErrorMessage)] public OwnershipType Ownership { get; set; } - [DataType(DataType.PhoneNumber)] - [RegularExpression( - Constants.PhoneNumberRegexModel, - ErrorMessage = Constants.PhoneErrorMessage)] - [DisplayFormat(DataFormatString = Constants.PhoneNumberFormat)] - [MaxLength(Constants.UnifiedPhoneLength)] - [Required(ErrorMessage = "PhoneNumber is required")] public string BlockPhoneNumber { get; set; } = string.Empty; } \ No newline at end of file From 22e396469aa5c6bc711787f3f3d502a20aaa1adb Mon Sep 17 00:00:00 2001 From: vitaliyyuras Date: Mon, 25 Sep 2023 17:17:35 +0300 Subject: [PATCH 5/5] Added BlockPhoneNumber mapping ignoring --- OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs b/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs index c1454d0676..685e745066 100644 --- a/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs +++ b/OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs @@ -165,7 +165,8 @@ public MappingProfile() .ForMember(dest => dest.Status, opt => opt.Ignore()) .ForMember(dest => dest.StatusReason, opt => opt.Ignore()) .ForMember(dest => dest.LicenseStatus, opt => opt.Ignore()) - .ForMember(dest => dest.ProviderAdmins, opt => opt.Ignore()); + .ForMember(dest => dest.ProviderAdmins, opt => opt.Ignore()) + .ForMember(dest => dest.BlockPhoneNumber, opt => opt.Ignore()); CreateMap() .ForMember(dest => dest.ActualAddress, opt => opt.MapFrom(src => src.ActualAddress))