using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using Newtonsoft.Json; using Yavsc.Abstract.Identity; using Yavsc.Abstract.Identity.Security; using Yavsc.Models.Access; using Yavsc.Models.Relationship; using Yavsc.Blogspot; namespace Yavsc.Models.Blog { public class BlogPost : IBlogPost { [Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Display(Name = "Identifiant du post")] public long Id { get; set; } [StringLength(1024)] public string? Photo { get; set; } [StringLength(1024)] [Required] public string Title { get; set; } [StringLength(56224)] public string? Article { get; set; } [InverseProperty("Target")] [Display(Name = "Liste de contrôle d'accès")] public virtual List? ACL { get; set; } [Display(Name = "Identifiant de l'auteur")] [ForeignKey("Author")] public string? AuthorId { get; set; } [Display(Name = "Auteur")] public virtual ApplicationUser Author { set; get; } [Display(Name = "Date de création")] public DateTime DateCreated { get; set; } [Display(Name = "Créateur")] public string? UserCreated { get; set; } [Display(Name = "Dernière modification")] public DateTime DateModified { get; set; } [Display(Name = "Utilisateur ayant modifé le dernier")] public string? UserModified { get; set; } public bool AuthorizeCircle(long circleId) { return ACL?.Any(i => i.CircleId == circleId) ?? true; } public ICircleAuthorization[] GetACL() { return ACL.ToArray(); } public void Tag(Tag tag) { var existent = Tags.SingleOrDefault(t => t.PostId == Id && t.TagId == tag.Id); if (existent == null) Tags.Add(new BlogTag { PostId = Id, Tag = tag }); } public void DeTag(Tag tag) { var existent = Tags.SingleOrDefault(t => ((t.TagId == tag.Id) && t.PostId == Id)); if (existent != null) Tags.Remove(existent); } public string[] GetTags() { return Tags.Select(t => t.Tag.Name).ToArray(); } [InverseProperty("Post")] public virtual List Tags { get; set; } [InverseProperty("Post")] public virtual List Comments { get; set; } /// /// Whether this post is published. Not a column: the /// existence of a row in BlogSpotPublication /// is the source of truth. EF skips this property via /// [NotMapped] so no migration is needed. The /// service hydrates it after each fetch (single bulk /// lookup, not N+1) and it surfaces through the wire /// as part of the JSON-serialised BlogPost. /// [NotMapped] public bool IsPublished { get; set; } IApplicationUser IBlogPost.Author => Author; } }