Skip to content

Instantly share code, notes, and snippets.

@ritasker
Last active December 20, 2015 06:58
Show Gist options
  • Save ritasker/6089543 to your computer and use it in GitHub Desktop.
Save ritasker/6089543 to your computer and use it in GitHub Desktop.
The Home Module of my first Nancy app.
public class HomeModule : NancyModule
{
private MongoCollection<Comment> _comments;
public HomeModule(MongoCollection<Comment> comments)
{
_comments = comments;
Get["/"] = Index;
Post["/AddComment"] = AddComment;
Post["/DeleteComment"] = DeleteCommente;
}
private Negotiator Index(dynamic parameters)
{
var model = new
{
Title = "Hello Nancy!",
Comments = _comments.FindAll().ToList()
};
return Negotiate
.WithView("Index")
.WithModel(model);
}
private Response DeleteComment(dynamic parameter)
{
var query = Query<Comment>.EQ(m => m.Id, (string)Request.Form.Id);
_comments.Remove(query);
return Response.AsRedirect("/", RedirectResponse.RedirectType.Permanent);
}
private Response AddComment(dynamic parameter)
{
if (!Request.Form.Comment.HasValue)
return Response.Context.Response.StatusCode = HttpStatusCode.BadRequest;
_comments.Save(new Comment { Text = Request.Form.Comment });
return Response.AsRedirect("/", RedirectResponse.RedirectType.Permanent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment