Skip to content

Commit

Permalink
act 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Tore Lervik committed Sep 5, 2011
1 parent c6aa9a2 commit fb5fa55
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 4 deletions.
47 changes: 45 additions & 2 deletions MvcTechdaysBlog/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,51 @@
using System.Web.Mvc;
using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcTechdaysBlog.Models;

namespace MvcTechdaysBlog.Controllers
{
public partial class HomeController : Controller
{
{
private DataService db = new DataService();

public virtual ActionResult Index()
{
var articles = db.Articles.OrderBy(a => a.Date).ToList();
return View(articles);
}

public virtual ActionResult Article(string id)
{
var article = db.Articles.SingleOrDefault(a => a.Url == id);

if (article != null)
{
ViewBag.Comments = article.Comments.OrderByDescending(c => c.Date).ToList();
return View(article);
}

throw new HttpException(404, "NotFound");
}

[HttpPost]
public virtual ActionResult PostComment(Comment comment)
{
if (ModelState.IsValid)
{
comment.Date = DateTime.Now;
db.Comments.Add(comment);
db.SaveChanges();
}

return PartialView("Comment", comment);
}

protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
10 changes: 9 additions & 1 deletion MvcTechdaysBlog/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.IgnoreRoute("favicon.ico");

routes.MapRoute(
"Article",
"{id}",
new { controller = "Home", action = "Article" }
);

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
Expand All @@ -28,7 +36,7 @@ public static void RegisterRoutes(RouteCollection routes)
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
//Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DataService>());
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DataService>());
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
Expand Down
2 changes: 2 additions & 0 deletions MvcTechdaysBlog/Models/Article.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class Article : IValidatableObject
[DataType(DataType.Date)]
public DateTime Date { get; set; }

public virtual ICollection<Comment> Comments { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Date.Date < DateTime.Now.AddDays(-7).Date)
Expand Down
33 changes: 33 additions & 0 deletions MvcTechdaysBlog/Models/Comment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcTechdaysBlog.Models
{
public class Comment
{
[Key]
public int Id { get; set; }

[Required]
[MaxLength(50)]
public string Name { get; set; }

[Required]
[DataType(DataType.MultilineText)]
[MaxLength(500)]
[Remote("BadWords", "Validation")]
public string Content { get; set; }

[Required]
[DataType(DataType.Date)]
public DateTime Date { get; set; }

public int ArticleId { get; set; }

public virtual Article Article { get; set; }
}
}
1 change: 1 addition & 0 deletions MvcTechdaysBlog/Models/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public class DataService : DbContext
// System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<MvcTechdaysBlog.DataService>());

public DbSet<Article> Articles { get; set; }
public DbSet<Comment> Comments { get; set; }
}
}
5 changes: 5 additions & 0 deletions MvcTechdaysBlog/MvcTechdaysBlog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<Compile Include="Areas\Admin\AreaAreaRegistration.cs" />
<Compile Include="Areas\Admin\Controllers\ArticleController.cs" />
<Compile Include="Controllers\AccountController.cs" />
<Compile Include="Models\Comment.cs" />
<Compile Include="Models\DataService.cs" />
<Compile Include="Models\Article.cs" />
<Compile Include="Models\LogOnViewModel.cs" />
Expand Down Expand Up @@ -294,6 +295,10 @@
</None>
<Content Include="Views\Shared\EditorTemplates\String.cshtml" />
<Content Include="Views\Shared\EditorTemplates\MultilineText.cshtml" />
<Content Include="Views\Home\Index.cshtml" />
<Content Include="Views\Home\Article.cshtml" />
<Content Include="Views\Shared\Comment.cshtml" />
<Content Include="Views\Shared\CommentForm.cshtml" />
</ItemGroup>
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
Expand Down
25 changes: 25 additions & 0 deletions MvcTechdaysBlog/Views/Home/Article.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@model MvcTechdaysBlog.Models.Article

@{
ViewBag.Title = @Model.Title;
}

<div class="two-third">
<div class="article">
<h2>@Model.Title</h2>
<p>@Html.Raw(Model.Content)</p>
</div>
</div>

<div class="one-third last">
@Html.Partial("CommentForm", new MvcTechdaysBlog.Models.Comment { ArticleId = Model.Id })
@if (ViewBag.Comments != null)
{
<div class="comments">
@foreach (MvcTechdaysBlog.Models.Comment comment in ViewBag.Comments)
{
@Html.Partial("Comment", comment)
}
</div>
}
</div>
16 changes: 16 additions & 0 deletions MvcTechdaysBlog/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@model List<MvcTechdaysBlog.Models.Article>

@{
ViewBag.Title = "Index";
}

<div class="one">
@foreach (var article in Model)
{
<div class="article">
<a href="@Url.Action("Article", new { id = article.Url })"><h2>@article.Title</h2></a>
<p>@article.Description</p>
<a href="@Url.Action("Article", new { id = article.Url })" class="fancy-button" style="margin-top: 10px;">Les mer</a>
</div>
}
</div>
7 changes: 7 additions & 0 deletions MvcTechdaysBlog/Views/Shared/Comment.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@model MvcTechdaysBlog.Models.Comment

<div class="comment">
<div class="comment-name">@Model.Name</div>
<div class="comment-date">@Model.Date.ToString("dd.MM.yyyy - HH:mm")</div>
<div class="comment-content">@Model.Content</div>
</div>
58 changes: 58 additions & 0 deletions MvcTechdaysBlog/Views/Shared/CommentForm.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
@model MvcTechdaysBlog.Models.Comment

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"> </script>
<script type="text/javascript">
function onCommentPostSuccess(data) {
$(".comments").prepend($(data));
cancelCommentForm();
}
$(function() {
$(".comment-form-button").click(function() {
$(".comment-form").slideDown(function() {
$(".comment-form #Name").focus();
});
$(".comment-form-button").slideUp();
});
$(".comment-form-cancel").click(cancelCommentForm);
});
function cancelCommentForm() {
$(".comment-form").slideUp();
$(".comment-form-button").slideDown();
$(".comment-form input[type='text'], .comment-form textarea").val("");
}
</script>

<div class="comment-form" style="display: none;">
@using (Ajax.BeginForm("PostComment", "Home", new AjaxOptions { OnSuccess = "onCommentPostSuccess" }))
{
@Html.ValidationSummary(true)
<fieldset>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.Content)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Content)
@Html.ValidationMessageFor(model => model.Content)
</div>

@Html.HiddenFor(model => model.ArticleId)
<input type="submit" value="Post" /> <div class="fancy-button comment-form-cancel">Cancel</div>
</fieldset>
}
</div>
<div class="comment-form-button fancy-button">New comment</div>
2 changes: 1 addition & 1 deletion MvcTechdaysBlog/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<add name="CompilableFileModule" type="SassAndCoffee.AspNet.CompilableFileModule" />
<add name="Glimpse" type="Glimpse.Core.Module" />
</httpModules>
<customErrors mode="On"></customErrors>
<customErrors mode="RemoteOnly"></customErrors>
<trace writeToDiagnosticsTrace="true" enabled="true" pageOutput="false" />
<httpHandlers>
<add path="glimpse.axd" verb="GET,POST" type="Glimpse.Core.Handler" />
Expand Down

0 comments on commit fb5fa55

Please sign in to comment.