From 2b880ac772b5ac6129f1ab16f6d055f3f1b62ce6 Mon Sep 17 00:00:00 2001 From: Dimitar Georgiev Dimitrov Date: Tue, 26 Aug 2025 15:32:28 +0300 Subject: [PATCH 1/2] Make address fields nullable --- NorthwindCRUD/Models/Contracts/IAddress.cs | 10 +++++----- NorthwindCRUD/Models/DbModels/AddressDb.cs | 10 +++++----- NorthwindCRUD/Models/Dtos/AddressDto.cs | 15 +++++---------- NorthwindCRUD/Services/SalesService.cs | 2 +- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/NorthwindCRUD/Models/Contracts/IAddress.cs b/NorthwindCRUD/Models/Contracts/IAddress.cs index 5d9a55c..40ace5e 100644 --- a/NorthwindCRUD/Models/Contracts/IAddress.cs +++ b/NorthwindCRUD/Models/Contracts/IAddress.cs @@ -4,15 +4,15 @@ namespace NorthwindCRUD.Models.Contracts { public interface IAddress { - string Street { get; set; } + string? Street { get; set; } - string City { get; set; } + string? City { get; set; } - string Region { get; set; } + string? Region { get; set; } - string PostalCode { get; set; } + string? PostalCode { get; set; } - string Country { get; set; } + string? Country { get; set; } string? Phone { get; set; } } diff --git a/NorthwindCRUD/Models/DbModels/AddressDb.cs b/NorthwindCRUD/Models/DbModels/AddressDb.cs index 527ebb9..72b3e51 100644 --- a/NorthwindCRUD/Models/DbModels/AddressDb.cs +++ b/NorthwindCRUD/Models/DbModels/AddressDb.cs @@ -10,15 +10,15 @@ public class AddressDb : IAddress [DatabaseGenerated(DatabaseGeneratedOption.None)] public string AddressId { get; set; } - public string Street { get; set; } + public string? Street { get; set; } - public string City { get; set; } + public string? City { get; set; } - public string Region { get; set; } + public string? Region { get; set; } - public string PostalCode { get; set; } + public string? PostalCode { get; set; } - public string Country { get; set; } + public string? Country { get; set; } public string? Phone { get; set; } diff --git a/NorthwindCRUD/Models/Dtos/AddressDto.cs b/NorthwindCRUD/Models/Dtos/AddressDto.cs index 10bc810..ce23bf9 100644 --- a/NorthwindCRUD/Models/Dtos/AddressDto.cs +++ b/NorthwindCRUD/Models/Dtos/AddressDto.cs @@ -5,25 +5,20 @@ namespace NorthwindCRUD.Models.Dtos { public class AddressDto : IAddress { - [Required(ErrorMessage = "Street is required.")] [StringLength(100, ErrorMessage = "Street cannot exceed 100 characters.")] - public string Street { get; set; } + public string? Street { get; set; } - [Required(ErrorMessage = "City is required.")] [StringLength(50, ErrorMessage = "City cannot exceed 50 characters.")] - public string City { get; set; } + public string? City { get; set; } - [Required(ErrorMessage = "Region is required.")] [StringLength(50, ErrorMessage = "Region cannot exceed 50 characters.")] - public string Region { get; set; } + public string? Region { get; set; } - [Required(ErrorMessage = "PostalCode is required.")] [StringLength(20, ErrorMessage = "Postal code cannot exceed 20 characters.")] - public string PostalCode { get; set; } + public string? PostalCode { get; set; } - [Required(ErrorMessage = "Country is required.")] [StringLength(50, ErrorMessage = "Country cannot exceed 50 characters.")] - public string Country { get; set; } + public string? Country { get; set; } [RegularExpression(@"^\+?[1-9]\d{1,14}$", ErrorMessage = "Phone number is not valid.")] public string? Phone { get; set; } diff --git a/NorthwindCRUD/Services/SalesService.cs b/NorthwindCRUD/Services/SalesService.cs index 8f0dffc..14e2145 100644 --- a/NorthwindCRUD/Services/SalesService.cs +++ b/NorthwindCRUD/Services/SalesService.cs @@ -71,7 +71,7 @@ public SalesDto[] RetrieveSalesDataByCountry(string startDate, string endDate, s .ToList(); var filteredData = salesData - .Where(od => od.Order.ShipAddress?.Country.ToLower(CultureInfo.InvariantCulture) == normalizedCountry && DateTime.Parse(od.Order.OrderDate, CultureInfo.InvariantCulture) >= parsedStartDate && DateTime.Parse(od.Order.RequiredDate, CultureInfo.InvariantCulture) <= parsedEndDate) + .Where(od => od.Order.ShipAddress?.Country?.ToLower(CultureInfo.InvariantCulture) == normalizedCountry && DateTime.Parse(od.Order.OrderDate, CultureInfo.InvariantCulture) >= parsedStartDate && DateTime.Parse(od.Order.RequiredDate, CultureInfo.InvariantCulture) <= parsedEndDate) .ToList(); var sales = filteredData.Select(od => new SalesDto From 551ee27a4c16787872b26c296a1d4b0443e19b7a Mon Sep 17 00:00:00 2001 From: Dimitar Georgiev Dimitrov Date: Tue, 26 Aug 2025 16:43:42 +0300 Subject: [PATCH 2/2] improve phone pattern to support dashes and phones starting with zero --- NorthwindCRUD/Models/Dtos/AddressDto.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NorthwindCRUD/Models/Dtos/AddressDto.cs b/NorthwindCRUD/Models/Dtos/AddressDto.cs index ce23bf9..2766983 100644 --- a/NorthwindCRUD/Models/Dtos/AddressDto.cs +++ b/NorthwindCRUD/Models/Dtos/AddressDto.cs @@ -20,7 +20,7 @@ public class AddressDto : IAddress [StringLength(50, ErrorMessage = "Country cannot exceed 50 characters.")] public string? Country { get; set; } - [RegularExpression(@"^\+?[1-9]\d{1,14}$", ErrorMessage = "Phone number is not valid.")] + [RegularExpression(@"^\+?[0-9][0-9\-]{1,14}$", ErrorMessage = "Phone number is not valid.")] public string? Phone { get; set; } } }