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("Type") switch { nameof(TravelAgentPostConfiguration) => JsonConvert.DeserializeObject(configurationJson)!, nameof(ChiefPostConfiguration) => JsonConvert.DeserializeObject(configurationJson)!, _ => JsonConvert.DeserializeObject(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"); } }