summaryrefslogtreecommitdiff
path: root/CoreWiki/Models
diff options
context:
space:
mode:
Diffstat (limited to 'CoreWiki/Models')
-rw-r--r--CoreWiki/Models/ApplicationDbContext.cs2
-rw-r--r--CoreWiki/Models/Article.cs32
-rw-r--r--CoreWiki/Models/ArticleCommentListViewModel.cs8
-rw-r--r--CoreWiki/Models/Comment.cs55
4 files changed, 88 insertions, 9 deletions
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<Article>().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<Comment> Comments { get; } = new List<Comment>();
} \ 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<Comment> 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