From 0d46e33815f780a681b0d1ef96fa452dd1193c17 Mon Sep 17 00:00:00 2001 From: Stepan Grankin Date: Mon, 6 Nov 2023 21:30:29 +0300 Subject: [PATCH] replace template with help of reflection --- .../Domain/NotificationPayload.cs | 2 +- Dodo1000Bot.Services/UnitsService.cs | 27 ++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Dodo1000Bot.Models/Domain/NotificationPayload.cs b/Dodo1000Bot.Models/Domain/NotificationPayload.cs index 5595f13..2067fac 100644 --- a/Dodo1000Bot.Models/Domain/NotificationPayload.cs +++ b/Dodo1000Bot.Models/Domain/NotificationPayload.cs @@ -15,7 +15,7 @@ public class NotificationPayload public string Name { get; init; } - public string[] TemplateArguments { get; set; } + public object TemplateArguments { get; set; } public override string ToString() { diff --git a/Dodo1000Bot.Services/UnitsService.cs b/Dodo1000Bot.Services/UnitsService.cs index 3e57c20..a5e3f14 100644 --- a/Dodo1000Bot.Services/UnitsService.cs +++ b/Dodo1000Bot.Services/UnitsService.cs @@ -349,17 +349,26 @@ internal async Task CheckUnitsOfBrandAtCountryAndNotify(Brands brand, int countr foreach (var unit in difference) { - var textFormat = "Wow! There is new {0} in {1}! You can find it on the map👆 \r\n" + - "It's {2} restaurant of {3} and {4} of all Dodo brands 🔥"; - var arguments = new[] - { - $"{brand}{brandEmoji}", - $"{unit.Address?.Locality?.Name}{flag}", - $"{restaurantsCountAtBrand}", - $"{brand}", - $"{totalOverall}" + var textFormat = "Wow! There is new {brandWithEmoji} in {localityWithFlag}! You can find it on the map👆 \r\n" + + "It's {restaurantsCountAtBrand} restaurant of {brand} and {totalOverall} of all Dodo brands 🔥"; + var arguments = new { + brandWithEmoji = $"{brand}{brandEmoji}", + localityWithFlag = $"{unit.Address?.Locality?.Name}{flag}", + restaurantsCountAtBrand = $"{restaurantsCountAtBrand}", + brand = $"{brand}", + totalOverall = $"{totalOverall}" }; + var properties = arguments.GetType().GetProperties(); + + var text = textFormat; + foreach (var property in properties) + { + var name = property.Name; + var value = property.GetValue(arguments)!.ToString(); + text = text.Replace($"{{{name}}}", value); + } + var notification = new Notification(NotificationType.NewUnit) { Payload = new NotificationPayload