public CommentModule(IIdeaRepository ideas, IActivityRepository activities) : base("/idea") { _ideas = ideas; _activities = activities; Post["/{idea}/comment"] = _ => { int id = _.Idea; var comment = new Comment { Time = DateTime.UtcNow, Text = Request.Form.comment }; _activities.Add(id, comment); return Response.AsRedirect(string.Format("/idea/{0}#{1}", id, comment.Id)); }; /* Get["/{idea}/comment/{id}"] = parameters => { using (var db = new IdeastrikeContext()) { int id = parameters.id; int idea = parameters.idea; var comment = db.Comments.FirstOrDefault(i => i.Id == id && i.IdeaId == idea); return string.Format("Comment Id:{0}", comment.Id); } }; Get["/{idea}/comment/{id}/delete"] = parameters => { using (var db = new IdeastrikeContext()) { int id = parameters.id; int idea = parameters.idea; var comment = db.Comments.FirstOrDefault(i => i.Id == id && i.IdeaId == idea); db.Comments.Remove(comment); db.SaveChanges(); return string.Format("Deleted Comment {0} for Idea {1}", id, idea); } };*/ }
public CommentModule(IIdeaRepository ideas, IActivityRepository activities) : base("/comment") { _ideas = ideas; _activities = activities; Post["/{idea}/add"] = parameters => { int id = parameters.Idea; int userId = Request.Form.userId; // addtional validation required var text = Request.Form.comment; if (string.IsNullOrEmpty(text)) // additional validation required { return Response.AsJson(new { result = "Error" }); } var comment = new Comment { UserId = userId, Time = DateTime.UtcNow, Text = Request.Form.comment }; _activities.Add(id, comment); // why not return JSON here and leave it up to client to render inline? return Response.AsRedirect(string.Format("/idea/{0}#{1}", id, comment.Id)); }; // TODO: shouldn't these actually sit under the Comment root // TODO: user should be able to edit their own comment Post["/{id}/edit"] = parameters => Response.AsJson(new { result = "Error" }); // TODO: user should be able to remove their own comment Post["/{id}/delete"] = parameters => Response.AsJson(new { result = "Error" }); }
public AdminModule(IdeastrikeContext dbContext, Settings settings, IUserRepository users, IIdeaRepository ideas, IActivityRepository activities) : base("/admin") { this.RequiresAuthentication(); this.RequiresValidatedClaims(c => c.Contains("admin")); _settings = settings; _users = users; _ideas = ideas; _activities = activities; Get["/"] = _ => { var m = Context.Model(string.Format("Admin - {0}", (string)_settings.SiteTitle)); m.Name = _settings.Name; m.WelcomeMessage = _settings.WelcomeMessage; m.HomePage = _settings.HomePage; m.GAnalyticsKey = _settings.GAnalyticsKey; return View["Admin/Index", m]; }; Get["/users"] = _ => { var m = Context.Model(string.Format("Admin - {0}", (string)_settings.SiteTitle)); m.Name = _settings.Name; m.WelcomeMessage = _settings.WelcomeMessage; m.HomePage = _settings.HomePage; m.GAnalyticsKey = _settings.GAnalyticsKey; m.Users = users.GetAll(); return View["Admin/Users", m]; }; Get["/moderation"] = _ => { var m = Context.Model(string.Format("Admin - {0}", (string)_settings.SiteTitle)); m.Name = _settings.Name; m.WelcomeMessage = _settings.WelcomeMessage; m.HomePage = _settings.HomePage; m.GAnalyticsKey = _settings.GAnalyticsKey; return View["Admin/Moderation", m]; }; Get["/settings"] = _ => { var m = Context.Model(string.Format("Admin - {0}", (string)_settings.SiteTitle)); m.Name = _settings.Name; m.SiteTitle = _settings.SiteTitle; m.WelcomeMessage = _settings.WelcomeMessage; m.HomePage = _settings.HomePage; m.GAnalyticsKey = _settings.GAnalyticsKey; m.MaxThumbnailWidth = _settings.MaxThumbnailWidth; return View["Admin/Settings", m]; }; Post["/settings"] = _ => { _settings.WelcomeMessage = Request.Form.welcomemessage; _settings.SiteTitle = Request.Form.sitetitle; _settings.Name = Request.Form.yourname; _settings.HomePage = Request.Form.homepage; _settings.GAnalyticsKey = Request.Form.analyticskey; _settings.MaxThumbnailWidth = Request.Form.maxthumbnailwidth; return Response.AsRedirect("/admin/settings"); }; Get["/search"] = _ => ""; Get["/forums"] = _ => ""; Get["/forum/{forumId}"] = _ => ""; Get["/uservoice"] = _ => View["Admin/Uservoice", Context.Model("Admin")]; Post["/uservoice"] = _ => { var client = new WebClient(); var suggestions = GetSuggestions(client, Request.Form.channel, Request.Form.forumid, Request.Form.apikey, Request.Form.trusted); foreach (var s in suggestions) { string title = s.title; //If the idea exists, skip it if (ideas.FindBy(i => i.Title == title).Any()) continue; string date = s.created_at; var idea = new Idea { Title = title, Description = s.text, Time = DateTime.Parse(date.Substring(0, date.Length - 5)), }; string status = string.Empty; switch ((string)s.state) { case "approved": status = "Active"; break; case "closed" : if (s.status.key == "completed") status = "Completed"; else status = "Declined"; break; default: status = "New"; break; } idea.Status = status; //Get the author, or create string name = s.creator.name; var existing = users.FindBy(u => u.UserName == name).FirstOrDefault(); if (existing != null) idea.Author = existing; else { idea.Author = NewUser(s.creator); users.Add(idea.Author); } ideas.Add(idea); //Process all comments var comments = GetComments(client, (string)s.id, Request.Form.channel, Request.Form.forumid, Request.Form.apikey, Request.Form.trusted); List<Activity> ideaComments = new List<Activity>(); foreach (var c in comments) { string commentdate = c.created_at; var comment = new Comment { Time = DateTime.Parse(commentdate), Text = c.text }; string commentname = c.creator.name; existing = users.FindBy(u => u.UserName == commentname).FirstOrDefault(); if (existing != null) comment.User = existing; else { comment.User = NewUser(c.creator); users.Add(comment.User); } activities.Add(idea.Id, comment); } //Process all votes var votes = GetVotes(client, (string)s.id, Request.Form.channel, Request.Form.forumid, Request.Form.apikey, Request.Form.trusted); foreach (var v in votes) { string votername = v.user.name; string votesfor = v.votes_for; int vote; if (Int32.TryParse(votesfor, out vote)) { existing = users.FindBy(u => u.UserName == votername).FirstOrDefault(); if (existing != null) ideas.Vote(idea.Id, existing.Id, vote); else { var author = NewUser(v.user); users.Add(author); ideas.Vote(idea.Id, author.Id, vote); } } } } return Response.AsRedirect("/admin"); }; }