Ejemplo n.º 1
0
        public AdminModule(IdeastrikeContext dbContext, ISettingsRepository settings)
            : base("/admin")
        {
            Get["/"] = _ => "";
            Get["/moderation"] = _ => "";
            Get["/search"] = _ => "";
            Get["/forums"] = _ => "";
            Get["/forum/{forumId}"] = _ => "";
            Get["/settings"] = _ => View["Admin/Settings", settings];
            Post["/settings"] = _ =>
                                    {
                                        settings.WelcomeMessage = Request.Form.welcomemessage;
                                        settings.Title = Request.Form.title;
                                        settings.Name = Request.Form.yourname;
                                        settings.HomePage = Request.Form.homepage;
                                        settings.GAnalyticsKey = Request.Form.analyticskey;
                                        try
                                        {
                                            dbContext.SaveChanges();
                                        }
                                        catch (Exception ex)
                                        {
                                            var y = ex.Message;
                                        }

                                        return Response.AsRedirect("/admin/settings");
                                    };
        }
Ejemplo n.º 2
0
        public ApiModule(IdeastrikeContext db)
            : base("/api")
        {
            Get["/ideas"] = _ => {
                return Response.AsJson(db.Ideas.Select(idea =>
                    new {
                        idea.Id,
                        idea.Title,
                        idea.Description,
                        Time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), idea.Time),
                        Author = new { idea.Author.Id, idea.Author.Username },
                        VoteCount = idea.Votes.Sum(vote => (int?)vote.Value) ?? 0
                    }));
            };

            Get["/ideas/{id}/features"] = _ => {
                int id = _.id;
                if (!db.Ideas.Any(idea => idea.Id == id))
                    return HttpStatusCode.NotFound;
                return Response.AsJson(db.Features.Where(d => d.IdeaId == id).Select(feature =>
                    new {
                        feature.Id,
                        feature.Text,
                        Time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), feature.Time),
                    }));
            };
        }
Ejemplo n.º 3
0
        public ApiModule(IdeastrikeContext db, IIdeaRepository ideas, IUserRepository users, ISettingsRepository settings)
            : base("/api")
        {
            Get["/ideas"] = _ => {
                return Response.AsJson(db.Ideas.Select(idea =>
                    new {
                        id = idea.Id,
                        title = idea.Title,
                        description = idea.Description,
                        time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), idea.Time),
                        author = new { id = idea.Author.Id, username = idea.Author.UserName },
                        vote_count = idea.Votes.Sum(vote => (int?)vote.Value) ?? 0,
                        status = idea.Status
                    }));
            };

            Get["/ideas/{id}"] = _ => {
                int id = _.id;
                var o = db.Ideas.Where(idea => idea.Id == id).Select(idea =>
                    new {
                        id = idea.Id,
                        title = idea.Title,
                        description = idea.Description,
                        time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), idea.Time),
                        author = new { id = idea.Author.Id, username = idea.Author.UserName },
                        vote_count = idea.Votes.Sum(vote => (int?)vote.Value) ?? 0,
                        features = idea.Features.Select(feature => new { id = feature.Id, text = feature.Text, time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), feature.Time) }),
                        votes = idea.Votes.Select(vote => new { user = new { id = vote.UserId, username = vote.User.UserName }, value = vote.Value })
                    }).FirstOrDefault();
                if (o == null)
                    return HttpStatusCode.NotFound;
                return Response.AsJson(o);
            };

            Get["/ideas/{id}/features"] = _ => {
                int id = _.id;
                if (!db.Ideas.Any(idea => idea.Id == id))
                    return HttpStatusCode.NotFound;
                return Response.AsJson(db.Features.Where(d => d.Idea.Id == id).Select(feature =>
                    new {
                        id = feature.Id,
                        text = feature.Text,
                        time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), feature.Time),
                    }));
            };

            Get["/ideas/{id}/votes"] = _ => {
                int id = _.id;
                if (!db.Ideas.Any(idea => idea.Id == id))
                    return HttpStatusCode.NotFound;
                return Response.AsJson(db.Votes.Where(d => d.IdeaId == id).Select(vote =>
                    new {
                        value = vote.Value,
                        user = new { id = vote.UserId, username = vote.User.UserName }
                    }));
            };
        }
Ejemplo n.º 4
0
 public HomeModule()
 {
     Get["/"] = _ =>
     {
         using (var db = new IdeastrikeContext())
         {
             db.Ideas.Add(new Idea {Title = "I heard you like ideas, so I put an idea in your ideas", Time = DateTime.UtcNow});
             db.SaveChanges();
             return View["Home/Index", string.Format("Hello, world. There are {0} ideas", db.Ideas.Count())];
         }
     };
 }
Ejemplo n.º 5
0
        public IdeaModule()
        {
            Get["/idea/{id}"] = parameters =>
            {
                using (var db = new IdeastrikeContext())
                {
                    double id = parameters.id;
                    var idea = db.Ideas.FirstOrDefault(i => i.Id == id);
                    return string.Format("Id:{0} Title:{1} Description:{2}", idea.Id, idea.Title, idea.Description);
                }
            };

            Get["/idea/{id}/delete"] = parameters =>
            {
                using (var db = new IdeastrikeContext())
                {
                    double id = parameters.id;
                    var idea = db.Ideas.FirstOrDefault(i => i.Id == id);
                    db.Ideas.Remove(idea);
                    db.SaveChanges();
                    return string.Format("Deleted Item {0}", id);
                }
            };

                        Post["/idea/add"] = parameters =>
            {
                using (var db = new IdeastrikeContext())
                {
                    var author = Request.Form.author;
                    var title = Request.Form.title;
                    var desc = Request.Form.description;

                    var newIdea = db.Ideas.Add(new Idea {Title = title, Author = author, Description = desc });
                    if(newIdea.isValid())
                    {
                        // store the new idea and redirect to the new idea
                        db.SaveChanges();
                        return Response.AsRedirect(string.Format("/idea/{0}/", newIdea.Id));
                    }
                    else
                    {
                        // return to form with errors
                        List<string> errorList = newIdea.generateErrorList();
                        return View["Home/Add", new { errorList, author, title, desc }];
                    }
                }
            };
        }
Ejemplo n.º 6
0
        public CommentModule()
            : base("/idea")
        {
            Get["/{idea}/comment"] = parameters =>
            {
                using (var db = new IdeastrikeContext())
                {
                    db.Comments.Add(new Comment {IdeaId = parameters.idea });
                    db.SaveChanges();
                    return View["Comment/Index", string.Format("Hello, world. There are {0} comments for Idea {1}", db.Comments.Count(), parameters.idea)];
                }
            };

            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);
                }
            };
        }
Ejemplo n.º 7
0
        public IdeaModule()
        {
            Get["/idea/{id}"] = parameters =>
            {
                using (var db = new IdeastrikeContext())
                {
                    double id = parameters.id;
                    var idea = db.Ideas.FirstOrDefault(i => i.Id == id);
                    return string.Format("Id:{0} Title:{1} Description:{2}", idea.Id, idea.Title, idea.Description);
                }
            };

            Get["/idea/{id}/delete"] = parameters =>
            {
                using (var db = new IdeastrikeContext())
                {
                    double id = parameters.id;
                    var idea = db.Ideas.FirstOrDefault(i => i.Id == id);
                    db.Ideas.Remove(idea);
                    db.SaveChanges();
                    return string.Format("Deleted Item {0}", id);
                }
            };
        }
Ejemplo n.º 8
0
 public IdeaRepository(IdeastrikeContext db)
 {
     this.db = db;
 }
Ejemplo n.º 9
0
        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");
            };
        }
Ejemplo n.º 10
0
 public UserRepository(IdeastrikeContext db)
 {
     _entities = db;
 }
Ejemplo n.º 11
0
 public FeatureRepository(IdeastrikeContext db)
 {
     this.db = db;
 }
Ejemplo n.º 12
0
 public IdeaRepository(IdeastrikeContext db)
 {
     this.db = db;
 }
Ejemplo n.º 13
0
 public UserRepository(IdeastrikeContext db)
 {
     _entities = db;
 }
Ejemplo n.º 14
0
 public FeatureRepository(IdeastrikeContext db)
 {
     this.db = db;
 }
Ejemplo n.º 15
0
 public ActivityRepository(IdeastrikeContext db)
 {
     this.db = db;
 }
Ejemplo n.º 16
0
        public ApiModule(IdeastrikeContext db, IIdeaRepository ideas, IUserRepository users, Settings settings)
            : base("/api")
        {
            _settings = settings;

            Get["/ideas"] = _ => {
                return Response.AsJson(db.Ideas.Select(idea =>
                    new {
                        id = idea.Id,
                        title = idea.Title,
                        description = idea.Description,
                        time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), idea.Time),
                        author = new { id = idea.Author.Id, username = idea.Author.UserName },
                        vote_count = idea.Votes.Sum(vote => (int?)vote.Value) ?? 0,
                        status = idea.Status
                    }));
            };

            Get["/ideas/{id}"] = _ => {
                int id = _.id;
                var o = db.Ideas.Where(idea => idea.Id == id).Select(idea =>
                    new {
                        id = idea.Id,
                        title = idea.Title,
                        description = idea.Description,
                        time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), idea.Time),
                        author = new { id = idea.Author.Id, username = idea.Author.UserName },
                        vote_count = idea.Votes.Sum(vote => (int?)vote.Value) ?? 0,
                        features = idea.Features.Select(feature => new { id = feature.Id, text = feature.Text, time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), feature.Time) }),
                        votes = idea.Votes.Select(vote => new { user = new { id = vote.UserId, username = vote.User.UserName }, value = vote.Value })
                    }).FirstOrDefault();
                if (o == null)
                    return HttpStatusCode.NotFound;
                return Response.AsJson(o);
            };

            Get["/ideas/{id}/features"] = _ => {
                int id = _.id;
                if (!db.Ideas.Any(idea => idea.Id == id))
                    return HttpStatusCode.NotFound;
                return Response.AsJson(db.Features.Where(d => d.Idea.Id == id).Select(feature =>
                    new {
                        id = feature.Id,
                        text = feature.Text,
                        time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), feature.Time),
                    }));
            };

            Get["/ideas/{id}/votes"] = _ => {
                int id = _.id;
                if (!db.Ideas.Any(idea => idea.Id == id))
                    return HttpStatusCode.NotFound;
                return Response.AsJson(db.Votes.Where(d => d.IdeaId == id).Select(vote =>
                    new {
                        value = vote.Value,
                        user = new { id = vote.UserId, username = vote.User.UserName }
                    }));
            };

            Post["/activity"] = _ =>
            {
                string content;
                using (var reader = new StreamReader(Context.Request.Body))
                {
                    content = reader.ReadToEnd();
                }
                var j = JsonConvert.DeserializeObject<dynamic>(content);
                string repourl = j.repository.url.ToString();
                string reponame = j.repository.name.ToString();
                var idea = ideas
                            .Include("Activities")
                            .Where(i => i.GithubUrl == repourl || i.GithubName == reponame)
                            .FirstOrDefault();

                if (idea == null)
                    return HttpStatusCode.NotFound;

                foreach (var c in j.commits)
                {
                    string date = c.timestamp;
                    var activity = new GitHubActivity
                    {
                        Time = DateTime.Parse(date),
                        Message = c.message,
                        CommitUrl = c.url,
                        AuthorName = c.author.name,
                        GravatarUrl = GravatarExtensions.ToGravatarUrl(c.author.email.ToString(), 40),
                        Sha = c.id
                    };

                    if (!idea.Activities.OfType<GitHubActivity>().Any(a => a.Sha == activity.Sha))
                        idea.Activities.Add(activity);
                }

                ideas.Save();
                return HttpStatusCode.Accepted;
            };
        }
Ejemplo n.º 17
0
        public ApiModule(IdeastrikeContext db, IIdeaRepository ideas)
            : base("/api")
        {
            Get["/ideas"] = _ => {
                return Response.AsJson(db.Ideas.Select(idea =>
                    new {
                        id = idea.Id,
                        title = idea.Title,
                        description = idea.Description,
                        time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), idea.Time),
                        Author = new { idea.Author.Id, idea.Author.UserName },
                        vote_count = idea.Votes.Sum(vote => (int?)vote.Value) ?? 0
                    }));
            };

            Post["/ideas"] = _ => {
                var model = this.Bind<EditIdeaModel>();
                var idea = new Idea {
                    Title= model.title,
                    Description= model.description,
                    Time = DateTime.UtcNow
                };
                ideas.Add(idea);

                return HttpStatusCode.Created;  // TODO: Should return either the generated id or the json body
            };

            Put["/ideas/{id}"] = _ => {
                var model = this.Bind<EditIdeaModel>();
                int id = _.id;
                var idea = ideas.Get(id);
                if (idea == null)
                    return HttpStatusCode.NotFound;
                if (model.title != null)
                    idea.Title = model.title;
                if (model.description != null)
                    idea.Description = model.description;
                ideas.Edit(idea);

                return HttpStatusCode.OK;
            };

            Get["/ideas/{id}"] = _ => {
                int id = _.id;
                var o = db.Ideas.Where(idea => idea.Id == id).Select(idea =>
                    new {
                        id = idea.Id,
                        title = idea.Title,
                        description = idea.Description,
                        time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), idea.Time),
                        author = new { id = idea.Author.Id, username = idea.Author.UserName },
                        vote_count = idea.Votes.Sum(vote => (int?)vote.Value) ?? 0,
                        features = idea.Features.Select(feature => new { id = feature.Id, text = feature.Text, time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), feature.Time) }),
                        votes = idea.Votes.Select(vote => new { user = new { id = vote.UserId, username = vote.User.UserName }, value = vote.Value })
                    }).FirstOrDefault();
                if (o == null)
                    return HttpStatusCode.NotFound;
                return Response.AsJson(o);
            };

            Get["/ideas/{id}/features"] = _ => {
                int id = _.id;
                if (!db.Ideas.Any(idea => idea.Id == id))
                    return HttpStatusCode.NotFound;
                return Response.AsJson(db.Features.Where(d => d.Idea.Id == id).Select(feature =>
                    new {
                        id = feature.Id,
                        text = feature.Text,
                        time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), feature.Time),
                    }));
            };

            Get["/ideas/{id}/votes"] = _ => {
                int id = _.id;
                if (!db.Ideas.Any(idea => idea.Id == id))
                    return HttpStatusCode.NotFound;
                return Response.AsJson(db.Votes.Where(d => d.IdeaId == id).Select(vote =>
                    new {
                        value = vote.Value,
                        user = new { id = vote.UserId, username = vote.User.UserName }
                    }));
            };
        }
Ejemplo n.º 18
0
 public ActivityRepository(IdeastrikeContext db)
 {
     this.db = db;
 }
Ejemplo n.º 19
0
        public AdminModule(IdeastrikeContext dbContext, ISettingsRepository settings, IUserRepository users)
            : base("/admin")
        {
            this.RequiresAuthentication();

            Get["/"] = _ =>
            {
                var m = Context.Model(string.Format("Admin - {0}", settings.Title));
                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}", settings.Title));
                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}", settings.Title));
                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}", settings.Title));
                m.Name = settings.Name;
                m.WelcomeMessage = settings.WelcomeMessage;
                m.HomePage = settings.HomePage;
                m.GAnalyticsKey = settings.GAnalyticsKey;

                return View["Admin/Settings", m];
            };

            Post["/settings"] = _ =>
            {
                settings.WelcomeMessage = Request.Form.welcomemessage;
                settings.Title = Request.Form.title;
                settings.Name = Request.Form.yourname;
                settings.HomePage = Request.Form.homepage;
                settings.GAnalyticsKey = Request.Form.analyticskey;
                try
                {
                    dbContext.SaveChanges();
                }
                catch (Exception ex)
                {
                    var y = ex.Message;
                }

                return Response.AsRedirect("/admin/settings");
            };

            Get["/search"] = _ => "";
            Get["/forums"] = _ => "";
            Get["/forum/{forumId}"] = _ => "";
        }
Ejemplo n.º 20
0
 public ImageRepository(IdeastrikeContext dataContext)
 {
     _dataContext = dataContext;
 }