using Shared.Engine.Pools; using System.Text; using System.Text.Json.Serialization; namespace Shared.Models.Templates { public class SimilarTpl : ITplResult { public static string OnlineSplit => ""; public List data { get; private set; } public SimilarTpl() : this(20) { } public SimilarTpl(int capacity) { data = new List(capacity); } public void Append(string title, string year, string details, string link, string img = null) { if (!string.IsNullOrEmpty(title)) { data.Add(new SimilarDto( link, year != null && short.TryParse(year, out short _year) ? _year : (short)0, details, title, img )); } } public bool IsEmpty => data == null || data.Count == 0; public int Length => data?.Count ?? 0; public string ToHtml() { if (IsEmpty) return string.Empty; var sb = ToBuilderHtml(); string result = sb.ToString(); StringBuilderPool.Return(sb); return result; } public StringBuilder ToBuilderHtml() { if (IsEmpty) return StringBuilderPool.EmptyHtml; var html = StringBuilderPool.Rent(); bool firstjson = true; html.Append("
"); foreach (var i in data) { html.Append("
"); html.Append("
"); UtilsTpl.HtmlEncode(i.title, html); html.Append("
"); firstjson = false; } html.Append("
"); return html; } public string ToJson() { if (IsEmpty) return string.Empty; var sb = ToBuilderJson(); string result = sb.ToString(); StringBuilderPool.Return(sb); return result; } public StringBuilder ToBuilderJson() { if (IsEmpty) return StringBuilderPool.EmptyJsonObject; var json = StringBuilderPool.Rent(); UtilsTpl.WriteJson(json, new SimilarResponseDto(data), SimilarJsonContext.Default.SimilarResponseDto); return json; } } [JsonSourceGenerationOptions( DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault )] [JsonSerializable(typeof(SimilarDto))] [JsonSerializable(typeof(SimilarResponseDto))] [JsonSerializable(typeof(List))] public partial class SimilarJsonContext : JsonSerializerContext { } public readonly struct SimilarDto { public string method { get; } public string url { get; } public bool similar { get; } public short year { get; } public string details { get; } public string title { get; } public string img { get; } [JsonConstructor] public SimilarDto( string url, short year, string details, string title, string img ) { method = "link"; this.url = url; similar = true; this.year = year; this.details = details; this.title = title; this.img = img; } } public readonly struct SimilarResponseDto { public string type { get; } public IReadOnlyList data { get; } [JsonConstructor] public SimilarResponseDto(IReadOnlyList data) { type = "similar"; this.data = data; } } }