Skip to content

Commit

Permalink
replace template with help of reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
granstel committed Nov 6, 2023
1 parent 8923e91 commit 0d46e33
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Dodo1000Bot.Models/Domain/NotificationPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
27 changes: 18 additions & 9 deletions Dodo1000Bot.Services/UnitsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0d46e33

Please sign in to comment.