forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
8 лаба
This commit is contained in:
@@ -13,29 +13,24 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MagicCarpetContracts.Resources;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using MagicCarpetContracts.Mapper;
|
||||
|
||||
namespace MagicCarpetContracts.DataModels;
|
||||
|
||||
internal class PostDataModel(string postId, string postName, PostType postType, PostConfiguration configuration) : IValidation
|
||||
{
|
||||
[AlternativeName("PostId")]
|
||||
public string Id { get; private set; } = postId;
|
||||
public string PostName { get; private set; } = postName;
|
||||
public PostType PostType { get; private set; } = postType;
|
||||
|
||||
[AlternativeName("Configuration")]
|
||||
[AlternativeName("ConfigurationJson")]
|
||||
[PostProcessing(MappingCallMethodName = "ParseJson")]
|
||||
public PostConfiguration ConfigurationModel { get; private set; } = configuration;
|
||||
|
||||
public PostDataModel(string postId, string postName, PostType postType, string configurationJson) : this(postId, postName, postType, (PostConfiguration)null)
|
||||
{
|
||||
var obj = JToken.Parse(configurationJson);
|
||||
if (obj is not null)
|
||||
{
|
||||
ConfigurationModel = obj.Value<string>("Type") switch
|
||||
{
|
||||
nameof(TravelAgentPostConfiguration) => JsonConvert.DeserializeObject<TravelAgentPostConfiguration>(configurationJson)!,
|
||||
nameof(ChiefPostConfiguration) => JsonConvert.DeserializeObject<ChiefPostConfiguration>(configurationJson)!,
|
||||
_ => JsonConvert.DeserializeObject<PostConfiguration>(configurationJson)!,
|
||||
};
|
||||
}
|
||||
}
|
||||
public PostDataModel() : this(string.Empty, string.Empty, PostType.None, null) { }
|
||||
public PostDataModel(string postId, string postName) : this(postId, postName, PostType.None, new PostConfiguration() { Rate = 10 }) { }
|
||||
|
||||
public void Validate(IStringLocalizer<Messages> localizer)
|
||||
{
|
||||
@@ -57,4 +52,32 @@ internal class PostDataModel(string postId, string postName, PostType postType,
|
||||
if (ConfigurationModel!.Rate <= 0)
|
||||
throw new ValidationException(string.Format(localizer["ValidationExceptionMessageLessOrEqualZero"], "Rate"));
|
||||
}
|
||||
|
||||
private PostConfiguration? ParseJson(object json)
|
||||
{
|
||||
if (json is PostConfiguration config)
|
||||
{
|
||||
return config;
|
||||
}
|
||||
if (json is string)
|
||||
{
|
||||
|
||||
var obj = JToken.Parse((string)json);
|
||||
var type = obj.Value<string>("Type");
|
||||
switch (type)
|
||||
{
|
||||
case nameof(TravelAgentPostConfiguration):
|
||||
ConfigurationModel = JsonConvert.DeserializeObject<TravelAgentPostConfiguration>((string)json);
|
||||
break;
|
||||
case nameof(ChiefPostConfiguration):
|
||||
ConfigurationModel = JsonConvert.DeserializeObject<ChiefPostConfiguration>((string)json);
|
||||
break;
|
||||
default:
|
||||
ConfigurationModel = JsonConvert.DeserializeObject<PostConfiguration>((string)json);
|
||||
break;
|
||||
}
|
||||
return ConfigurationModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user