public GitHubActivity AddNewActivity(GitHubActivity activity)
        {
            var activityAdded = context.GitHubActivities.Add(activity);

            context.SaveChanges();

            return(activityAdded);
        }
 public GitHubActivityViewModel(GitHubActivity github)
 {
     Text = github.Message;
     AuthorName = github.AuthorName;
     AuthorUrl = github.AuthorUrl;
     Sha = github.Sha;
     ShaShortened = github.Sha.Substring(0, 8);
     GravatarUrl = github.GravatarUrl;
     FriendlyTime = github.Time.ToFriendly().ToHtmlString();
     CommitUrl = github.CommitUrl;
 }
 public GitHubActivityViewModel(GitHubActivity github)
 {
     Text         = github.Message;
     AuthorName   = github.AuthorName;
     AuthorUrl    = github.AuthorUrl;
     Sha          = github.Sha;
     ShaShortened = github.Sha.Substring(0, 8);
     GravatarUrl  = github.GravatarUrl;
     FriendlyTime = github.Time.ToFriendly().ToHtmlString();
     CommitUrl    = github.CommitUrl;
 }
Example #4
0
        public GitHubActivity ConvertToActivity()
        {
            var activity = new GitHubActivity();

            activity.ActivityType = ActivityType.ToString();
            if (ActivityType == GitHubActivityType.IssueClosed || ActivityType == GitHubActivityType.IssueOpened)
            {
                activity.Link        = Issue.GetValue("html_url").Value <string>();
                activity.Description = Issue.GetValue("title").Value <string>();
            }
            else if (ActivityType == GitHubActivityType.Commit)
            {
                activity.Link        = Commits[0].GetValue("url").Value <string>();
                activity.Description = Commits[0].GetValue("message").Value <string>();
            }
            else
            {
                activity.Link        = Comment.GetValue("html_url").Value <string>();
                activity.Description = Comment.GetValue("body").Value <string>();
            }

            return(activity);
        }
Example #5
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);
            };
        }