Skip to content

Commit

Permalink
FIX🐛: Refactor Order and Product classes
Browse files Browse the repository at this point in the history
- In Order.cs, added JSON serialization support and made some properties optional to avoid circular references during serialization. Specifically, removed the `required` keyword from `OrderItems` and `User` in the `Order` class and made the `Order` property in `OrderItem` optional with a `[JsonIgnore]` attribute.
- In Product.cs, removed the static `IStringLocalizer` field and methods, indicating a shift in localization strategy. This could suggest a move towards handling localization at a different level or through another mechanism.
  • Loading branch information
NicolasBuscarini committed Jun 16, 2024
1 parent fc68b99 commit 2885848
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gh-nuget-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Build, Test, and Publish NuGet
uses: wahinekai/actions-publish-nuget@v3.0.1
with:
version: 1.1.6
version: 1.1.7
project-path: FoodService.Models/FoodService.Models.csproj
solution-path: FoodService.Models.sln
nuget-feed-password: ${{ secrets.PACKAGES_TOKEN }}
10 changes: 6 additions & 4 deletions FoodService.Models/Entities/Order.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FoodService.Models.Auth.User;
using System.Text.Json.Serialization;

namespace FoodService.Models.Entities
{
Expand All @@ -15,12 +16,12 @@ public class Order
/// <summary>
/// Gets or sets the list of items included in the order.
/// </summary>
public required List<OrderItem> OrderItems { get; set; }
public List<OrderItem> OrderItems { get; set; }

/// <summary>
/// Gets or sets the user who placed the order.
/// </summary>
public required ClientUser User { get; set; }
public ClientUser User { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -51,7 +52,8 @@ public class OrderItem
/// <summary>
/// Gets or sets the order associated with this order item.
/// </summary>
public required Order Order { get; set; }
[JsonIgnore]
public Order? Order { get; set; }

/// <summary>
/// Gets or sets the ID of the product associated with this order item.
Expand All @@ -61,6 +63,6 @@ public class OrderItem
/// <summary>
/// Gets or sets the product associated with this order item.
/// </summary>
public required Product Product { get; set; }
public Product Product { get; set; }
}
}
39 changes: 0 additions & 39 deletions FoodService.Models/Entities/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace FoodService.Models.Entities
/// </summary>
public class Product : Item
{
private static IStringLocalizer _localizer;

/// <summary>
/// Gets or sets the ID of the Product.
/// </summary>
Expand Down Expand Up @@ -43,42 +41,5 @@ public class Product : Item
/// Gets or sets the list of product ingredients associated with this product.
/// </summary>
public List<ProductIngredient>? ProductIngredients { get; set; }

/// <summary>
/// Sets the string localizer for the Product class.
/// </summary>
/// <param name="localizer">The string localizer.</param>
public static void SetLocalizer(IStringLocalizer localizer)
{
_localizer = localizer;
}

/// <summary>
/// Gets the localized name of the product based on the specified culture.
/// </summary>
/// <param name="culture">The culture to use for localization.</param>
/// <returns>The localized name of the product.</returns>
public string GetLocalizedName(CultureInfo culture)
{
var originalCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = culture;
var localizedName = _localizer?[$"{nameof(Product)}_{Id}_Name"] ?? base.Name;
Thread.CurrentThread.CurrentCulture = originalCulture;
return localizedName;
}

/// <summary>
/// Gets the localized description of the product based on the specified culture.
/// </summary>
/// <param name="culture">The culture to use for localization.</param>
/// <returns>The localized description of the product.</returns>
public string GetLocalizedDescription(CultureInfo culture)
{
var originalCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = culture;
var localizedDescription = _localizer?[$"{nameof(Product)}_{Id}_Description"] ?? base.Description;
Thread.CurrentThread.CurrentCulture = originalCulture;
return localizedDescription;
}
}
}
2 changes: 1 addition & 1 deletion FoodService.Models/FoodService.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<PackageId>FoodService.Models</PackageId>
<Version>1.1.6</Version>
<Version>1.1.7</Version>
<Authors>FoodService</Authors>
<Company>FoodService</Company>
<PackageDescription>A library for models in FoodService project</PackageDescription>
Expand Down

0 comments on commit 2885848

Please sign in to comment.