Skip to content

Commit

Permalink
Fix display issues when accessing posts saved with the old model stru…
Browse files Browse the repository at this point in the history
…cture.
  • Loading branch information
jrusbatch committed Feb 9, 2013
1 parent 99b0f9e commit a6ec6ab
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 18 deletions.
71 changes: 68 additions & 3 deletions Core/Models/Post.cs
Expand Up @@ -7,10 +7,16 @@ namespace Compilify.Models
public class Post : ICodeProject
{
private ICollection<Document> documents;
private ICollection<string> tags;

public Post()
{
documents = new HashSet<Document>();
tags = new HashSet<string>();
documents = new HashSet<Document>
{
new Document("Content", string.Empty),
new Document("Classes", string.Empty)
};
}

public string Id { get; set; }
Expand All @@ -19,6 +25,44 @@ public Post()

public int Version { get; set; }

public string Title { get; set; }

public string Description { get; set; }

public IEnumerable<string> Tags
{
get { return tags; }
set { tags = new HashSet<string>(value ?? Enumerable.Empty<string>()); }
}

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"; }
Expand Down Expand Up @@ -48,13 +92,34 @@ public IEnumerable<Document> Documents
set { documents = new HashSet<Document>(value ?? Enumerable.Empty<Document>()); }
}

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));
}
}

/// <summary>
/// The UTC date and time that the post was first persisted to the data store.</summary>
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;
}
}
}
4 changes: 2 additions & 2 deletions LanguageServices/Document.cs
Expand Up @@ -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()
{
Expand Down
4 changes: 2 additions & 2 deletions Web/Models/PostViewModel.cs
Expand Up @@ -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<DocumentModel> Documents { get; set; }
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions Web/Queries/SamplePostQuery.cs
Expand Up @@ -38,14 +38,14 @@ public Task<PostViewModel> 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);

Expand Down
18 changes: 11 additions & 7 deletions Web/Views/Home/Show.cshtml
Expand Up @@ -10,14 +10,18 @@
</div>

<div id="editors">
@foreach(var document in Model.Documents)
{
<div class="editor-container">
<div class="editor">
<textarea data-name="@document.Name">@document.Text</textarea>
</div>
<div class="editor-container">
<div id="define" class="editor">
@{ var classes = Model.Documents.FirstOrDefault(x => x.Name == "Classes"); }
<textarea data-name="@classes.Name">@classes.Text</textarea>
</div>
}
</div>
<div class="editor-container">
<div id="execute" class="editor">
@{ var content = Model.Documents.FirstOrDefault(x => x.Name == "Content"); }
<textarea data-name="@content.Name">@content.Text</textarea>
</div>
</div>
</div>

<div id="footer">
Expand Down

0 comments on commit a6ec6ab

Please sign in to comment.