From 79f5eaf40b93d64be74d8b4f1ef80d19fadbfbe1 Mon Sep 17 00:00:00 2001 From: Paweł Bernaciak Date: Fri, 29 Dec 2023 09:41:56 +0100 Subject: Cywilizowany tailwind --- CoreWiki/Models/ApplicationDbContext.cs | 2 +- CoreWiki/Models/Article.cs | 32 +++++++++++---- CoreWiki/Models/ArticleCommentListViewModel.cs | 8 ++++ CoreWiki/Models/Comment.cs | 55 ++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 CoreWiki/Models/ArticleCommentListViewModel.cs create mode 100644 CoreWiki/Models/Comment.cs (limited to 'CoreWiki/Models') diff --git a/CoreWiki/Models/ApplicationDbContext.cs b/CoreWiki/Models/ApplicationDbContext.cs index 38c3787..95eb460 100644 --- a/CoreWiki/Models/ApplicationDbContext.cs +++ b/CoreWiki/Models/ApplicationDbContext.cs @@ -15,7 +15,7 @@ public class ApplicationDbContext : DbContext { base.OnModelCreating(modelBuilder); modelBuilder.Entity
().HasData( - new Article { Topic = "Home Page", Slug = "home-page", Content = "Welcome to your new CoreWiki installation" } + new Article { Id = 1, Topic = "Home Page", Slug = "home-page", ViewCount = 0, Content = "Welcome to your new CoreWiki installation" } ); } } \ No newline at end of file diff --git a/CoreWiki/Models/Article.cs b/CoreWiki/Models/Article.cs index b989573..31485e3 100644 --- a/CoreWiki/Models/Article.cs +++ b/CoreWiki/Models/Article.cs @@ -8,15 +8,13 @@ namespace CoreWiki.Models; public class Article { - [Key] - public string Slug { get; set; } - + public int Id { get; set; } + [Required] + public required string Slug { get; set; } [Required, MaxLength(100)] - public string Topic { get; set; } - + public string? Topic { get; set; } [NotMapped] public Instant Published { get; set; } = SystemClock.Instance.GetCurrentInstant(); - [Obsolete("This property is only for serialization")] [DataType(DataType.DateTime)] [Column("Published")] @@ -26,8 +24,26 @@ public class Article get => Published.ToDateTimeUtc(); set => Published = DateTime.SpecifyKind(value, DateTimeKind.Utc).ToInstant(); } - + [Required] + public required int ViewCount { get; set; } [DataType(DataType.MultilineText)] [Required] - public string Content { get; set; } = default!; + public string? Content { get; set; } + + [NotMapped] + public int? EstimatedReadingTime + { + get + { + if (Content == null) + { + return null; + } + var wpm = 275.00m; + var wordCount = Content.Split(" ").Length; + return (int)Math.Ceiling(wordCount / wpm); + } + } + + public ICollection Comments { get; } = new List(); } \ No newline at end of file diff --git a/CoreWiki/Models/ArticleCommentListViewModel.cs b/CoreWiki/Models/ArticleCommentListViewModel.cs new file mode 100644 index 0000000..881fc01 --- /dev/null +++ b/CoreWiki/Models/ArticleCommentListViewModel.cs @@ -0,0 +1,8 @@ +namespace CoreWiki.Models; + +public class ArticleCommentListViewModel +{ + public IEnumerable Comments { get; init; } + public string Slug { get; init; } + public int PageNumber { get; init; } +} \ No newline at end of file diff --git a/CoreWiki/Models/Comment.cs b/CoreWiki/Models/Comment.cs new file mode 100644 index 0000000..4f1026a --- /dev/null +++ b/CoreWiki/Models/Comment.cs @@ -0,0 +1,55 @@ +using System.Buffers.Text; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Security.Cryptography; +using System.Text; +using Microsoft.AspNetCore.Mvc; +using Microsoft.VisualBasic; +using NodaTime; +using NodaTime.Extensions; + +namespace CoreWiki.Models; + +public class Comment +{ + public int Id { get; set; } + [Required] + public int ArticleId { get; set; } + [Required] + [DisplayName("Display Name")] + public string? DisplayName { get; set; } + [Required] + [DataType(DataType.EmailAddress)] + [DisplayName("E-Mail")] + public string? EMail { get; set; } + [NotMapped] + public string? GravatarHash + { + get + { + if (EMail == null) + { + return null; + } + var normalizedEmail = EMail.Trim().ToLower(); + var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(normalizedEmail)); + return BitConverter.ToString(bytes).Replace("-", "").ToLower(); + } + } + [Required] + public string? Content { get; set; } + [NotMapped] + public Instant Submitted { get; set; } + [Obsolete("This property only exists for EF-serialization purposes")] + [DataType(DataType.DateTime)] + [Column("Submitted")] + public DateTime SubmittedDateTime + { + get => Submitted.ToDateTimeUtc(); + // TODO: Remove this ugly hack + set => Submitted = DateTime.SpecifyKind(value, DateTimeKind.Utc).ToInstant(); + } + + public required Article Article { get; set; } +} \ No newline at end of file -- cgit v1.2.3