Last active
December 20, 2015 06:58
-
-
Save ritasker/6089543 to your computer and use it in GitHub Desktop.
The Home Module of my first Nancy app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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