-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56cef1b
commit f203c08
Showing
10 changed files
with
154 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/ElectricityOffNotifier.AppHost/Helpers/ValidationHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using FluentValidation.Results; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace ElectricityOffNotifier.AppHost.Helpers; | ||
|
||
public static class ValidationHelper | ||
{ | ||
public static ActionResult BadRequestExt(this ControllerBase controller, ValidationResult validationResult) | ||
{ | ||
foreach (ValidationFailure validationFailure in validationResult.Errors) | ||
{ | ||
controller.ModelState.TryAddModelError(validationFailure.PropertyName, validationFailure.ErrorMessage); | ||
} | ||
|
||
return controller.BadRequest(controller.ModelState); | ||
} | ||
} |
20 changes: 15 additions & 5 deletions
20
src/ElectricityOffNotifier.AppHost/Models/FindAddressesModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using FluentValidation; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace ElectricityOffNotifier.AppHost.Models; | ||
|
||
public sealed class FindAddressesModel | ||
{ | ||
[Range(1, int.MaxValue), FromQuery(Name = "cityId")] | ||
[FromQuery(Name = "cityId")] | ||
public int CityId { get; set; } | ||
[Required, RegularExpression("^[а-яА-Яa-zA-ZїЇєЄґҐ0-9- .']+$"), FromQuery(Name = "street")] | ||
[FromQuery(Name = "street")] | ||
public string Street { get; set; } | ||
[Required, RegularExpression("^[0-9a-zA-Zа-яА-Я- /]+$"), FromQuery(Name = "buildingNo")] | ||
public string BuildingNo { get; set; } | ||
[FromQuery(Name = "buildingNo")] | ||
public string? BuildingNo { get; set; } | ||
} | ||
|
||
public sealed class FindAddressesModelValidator : AbstractValidator<FindAddressesModel> | ||
{ | ||
public FindAddressesModelValidator() | ||
{ | ||
RuleFor(m => m.CityId).GreaterThanOrEqualTo(1); | ||
RuleFor(m => m.Street).NotNull().Matches("^[а-яА-Яa-zA-ZїЇєЄґҐ0-9- .']+$"); | ||
RuleFor(m => m.BuildingNo).Matches("^[0-9a-zA-Zа-яА-Я-\\/]+$"); | ||
} | ||
} |
46 changes: 40 additions & 6 deletions
46
src/ElectricityOffNotifier.AppHost/Models/ProducerRegisterModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,48 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json.Serialization; | ||
using ElectricityOffNotifier.Data; | ||
using ElectricityOffNotifier.Data.Models.Enums; | ||
using FluentValidation; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace ElectricityOffNotifier.AppHost.Models; | ||
|
||
public sealed record ProducerRegisterModel( | ||
[Range(1, int.MaxValue)] | ||
int AddressId, | ||
[property: JsonConverter(typeof(JsonStringEnumConverter))] | ||
[Required] | ||
ProducerMode Mode, | ||
[Url] | ||
string? WebhookUrl); | ||
string? WebhookUrl); | ||
|
||
public sealed class ProducerRegisterModelValidator : AbstractValidator<ProducerRegisterModel> | ||
{ | ||
public ProducerRegisterModelValidator(ElectricityDbContext context) | ||
{ | ||
// Validate address identifier | ||
RuleFor(m => m.AddressId) | ||
.Cascade(CascadeMode.Stop) | ||
.GreaterThanOrEqualTo(1) | ||
.MustAsync(async (addressId, cancellationToken) => | ||
await context.Addresses.AnyAsync(a => a.Id == addressId, cancellationToken)) | ||
.WithMessage("Address with specified id was not found"); | ||
|
||
// Validate mode | ||
RuleFor(m => m.Mode).IsInEnum(); | ||
|
||
// Validate webhook URL | ||
When(m => m.Mode == ProducerMode.Webhook, | ||
() => | ||
{ | ||
RuleFor(m => m.WebhookUrl) | ||
.NotNull() | ||
.WithMessage($"'{{PropertyName}}' must be set when '{nameof(ProducerMode.Webhook)}' mode is specified") | ||
.Must(webhookUrl => | ||
Uri.TryCreate(webhookUrl, UriKind.Absolute, out Uri? uri) && uri.Scheme is "http" or "https") | ||
.WithMessage("'{PropertyName}' must be a valid URL"); | ||
}) | ||
.Otherwise(() => | ||
{ | ||
RuleFor(m => m.WebhookUrl) | ||
.Null() | ||
.WithMessage($"'{{PropertyName}}' must be set ONLY when '{nameof(ProducerMode.Webhook)}' mode is specified"); | ||
}); | ||
} | ||
} |
46 changes: 45 additions & 1 deletion
46
src/ElectricityOffNotifier.AppHost/Models/SubscriberRegisterModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,53 @@ | ||
namespace ElectricityOffNotifier.AppHost.Models; | ||
using System.Globalization; | ||
using FluentValidation; | ||
using TimeZoneConverter; | ||
|
||
namespace ElectricityOffNotifier.AppHost.Models; | ||
|
||
public sealed class SubscriberRegisterModel | ||
{ | ||
public long TelegramId { get; set; } | ||
public int? TelegramThreadId { get; set; } | ||
public string? TimeZone { get; set; } | ||
public string? Culture { get; set; } | ||
} | ||
|
||
public sealed class SubscriberRegisterModelValidator : AbstractValidator<SubscriberRegisterModel> | ||
{ | ||
public SubscriberRegisterModelValidator() | ||
{ | ||
// Validate telegram chat identifier | ||
RuleFor(m => m.TelegramId).NotEqual(0L) | ||
.WithMessage("'{PropertyName}' must be a valid Telegram chat identifier"); | ||
|
||
// Validate telegram chat thread's identifier if set | ||
RuleFor(m => m.TelegramThreadId) | ||
.GreaterThanOrEqualTo(1) | ||
.When(m => m.TelegramThreadId.HasValue) | ||
.WithMessage("'{PropertyName}' must be a valid Telegram chat thread's identifier"); | ||
|
||
// Validate RFC 4646 culture name | ||
RuleFor(m => m.Culture) | ||
.Must(culture => | ||
{ | ||
try | ||
{ | ||
_ = new CultureInfo(culture!); | ||
} | ||
catch (CultureNotFoundException) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
}) | ||
.When(m => m.Culture != null) | ||
.WithMessage("'{PropertyName}' must be a valid RFC 4646 culture name"); | ||
|
||
// Validate time zone identifier | ||
RuleFor(m => m.TimeZone) | ||
.Must(timeZone => TZConvert.TryGetTimeZoneInfo(timeZone!, out _)) | ||
.When(m => m.TimeZone != null) | ||
.WithMessage("'{PropertyName}' must be a valid Windows or IANA time zone identifier"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters