forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
54 lines
2.3 KiB
C#
54 lines
2.3 KiB
C#
using MagicCarpetContracts.Enums;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.Extensions;
|
|
using MagicCarpetContracts.Infrastructure;
|
|
using MagicCarpetContracts.Infrastructure.PostConfigurations;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetContracts.DataModels;
|
|
|
|
public class PostDataModel(string postId, string postName, PostType postType, PostConfiguration configuration) : IValidation
|
|
{
|
|
public string Id { get; private set; } = postId;
|
|
public string PostName { get; private set; } = postName;
|
|
public PostType PostType { get; private set; } = postType;
|
|
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 void Validate()
|
|
{
|
|
if (Id.IsEmpty())
|
|
throw new ValidationException("Field Id is empty");
|
|
if (!Id.IsGuid())
|
|
throw new ValidationException("The value in the field Id is not a unique identifier");
|
|
if (PostName.IsEmpty())
|
|
throw new ValidationException("Field PostName is empty");
|
|
if (PostType == PostType.None)
|
|
throw new ValidationException("Field PostType is empty");
|
|
if (ConfigurationModel is null)
|
|
throw new ValidationException("Field ConfigurationModel is not initialized");
|
|
if (ConfigurationModel!.Rate <= 0)
|
|
throw new ValidationException("Field Rate is less or equal zero");
|
|
}
|
|
}
|