From a6ec6ab6cd67b51825a3cfc4315eba3c50c8147a Mon Sep 17 00:00:00 2001 From: Justin Rusbatch Date: Sat, 9 Feb 2013 16:34:18 -0500 Subject: [PATCH] Fix display issues when accessing posts saved with the old model structure. --- Core/Models/Post.cs | 71 ++++++++++++++++++++++++++++++++-- LanguageServices/Document.cs | 4 +- Web/Models/PostViewModel.cs | 4 +- Web/Queries/SamplePostQuery.cs | 8 ++-- Web/Views/Home/Show.cshtml | 18 +++++---- 5 files changed, 87 insertions(+), 18 deletions(-) diff --git a/Core/Models/Post.cs b/Core/Models/Post.cs index 5b47a38..8f1b1dd 100644 --- a/Core/Models/Post.cs +++ b/Core/Models/Post.cs @@ -7,10 +7,16 @@ namespace Compilify.Models public class Post : ICodeProject { private ICollection documents; + private ICollection tags; public Post() { - documents = new HashSet(); + tags = new HashSet(); + documents = new HashSet + { + new Document("Content", string.Empty), + new Document("Classes", string.Empty) + }; } public string Id { get; set; } @@ -19,6 +25,44 @@ public Post() public int Version { get; set; } + public string Title { get; set; } + + public string Description { get; set; } + + public IEnumerable Tags + { + get { return tags; } + set { tags = new HashSet(value ?? Enumerable.Empty()); } + } + + public string Content + { + get + { + var doc = GetOrCreateDocument("Content"); + return doc != null ? doc.Text : string.Empty; + } + set + { + var doc = GetOrCreateDocument("Content"); + doc.Text = value; + } + } + + public string Classes + { + get + { + var doc = GetOrCreateDocument("Classes"); + return doc != null ? doc.Text : string.Empty; + } + set + { + var doc = GetOrCreateDocument("Classes"); + doc.Text = value; + } + } + string ICodeProject.Name { get { return "Untitled"; } @@ -48,13 +92,34 @@ public IEnumerable Documents set { documents = new HashSet(value ?? Enumerable.Empty()); } } - public void AddDocument(string name, string text) + public void AddOrUpdateDocument(string name, string text) { - documents.Add(new Document(name, text)); + var doc = documents.FirstOrDefault(x => x.Name == name); + + if (doc != null) + { + doc.Text = text; + } + else + { + documents.Add(new Document(name, text)); + } } /// /// The UTC date and time that the post was first persisted to the data store. public DateTime? Created { get; set; } + + private Document GetOrCreateDocument(string name) + { + var document = documents.FirstOrDefault(x => x.Name == name); + + if (document == null) + { + documents.Add(document = new Document(name, string.Empty)); + } + + return document; + } } } diff --git a/LanguageServices/Document.cs b/LanguageServices/Document.cs index 9b5f8c8..3995a8a 100644 --- a/LanguageServices/Document.cs +++ b/LanguageServices/Document.cs @@ -13,10 +13,10 @@ public Document(string name, string text) } [DataMember(Order = 1)] - public string Name { get; private set; } + public string Name { get; set; } [DataMember(Order = 2)] - public string Text { get; private set; } + public string Text { get; set; } public string GetText() { diff --git a/Web/Models/PostViewModel.cs b/Web/Models/PostViewModel.cs index 64d5476..5dc5681 100644 --- a/Web/Models/PostViewModel.cs +++ b/Web/Models/PostViewModel.cs @@ -38,7 +38,7 @@ public static PostViewModel Create(Post post) public string Slug { get; set; } public int Version { get; set; } - + public Guid? AuthorId { get; set; } public IEnumerable Documents { get; set; } @@ -55,7 +55,7 @@ public Post ToPost() foreach (var doc in Documents) { - post.AddDocument(doc.Name, doc.Text); + post.AddOrUpdateDocument(doc.Name, doc.Text); } return post; diff --git a/Web/Queries/SamplePostQuery.cs b/Web/Queries/SamplePostQuery.cs index 128cb3e..bf2690d 100644 --- a/Web/Queries/SamplePostQuery.cs +++ b/Web/Queries/SamplePostQuery.cs @@ -38,14 +38,14 @@ public Task Execute() .AppendLine(" }") .AppendLine("}"); - post.AddDocument("Classes", builder.ToString()); + post.Classes = builder.ToString(); builder.Clear() .AppendLine("var person = new Person(name: null);") .AppendLine() - .AppendLine("return person.Greet();"); - - post.AddDocument("Content", builder.ToString()); + .AppendLine("return person.Greet();"); + + post.Content = builder.ToString(); var result = PostViewModel.Create(post); diff --git a/Web/Views/Home/Show.cshtml b/Web/Views/Home/Show.cshtml index 73c58ec..5e0e595 100644 --- a/Web/Views/Home/Show.cshtml +++ b/Web/Views/Home/Show.cshtml @@ -10,14 +10,18 @@
- @foreach(var document in Model.Documents) - { -
-
- -
+
+
+ @{ var classes = Model.Documents.FirstOrDefault(x => x.Name == "Classes"); } +
- } +
+
+
+ @{ var content = Model.Documents.FirstOrDefault(x => x.Name == "Content"); } + +
+