Exemple #1
0
        public object Delete(DeleteTechnology request)
        {
            var existingTech = Db.SingleById <Technology>(request.Id);

            if (existingTech == null)
            {
                throw HttpError.NotFound("Tech not found");
            }

            var session = SessionAs <AuthUserSession>();

            if (existingTech.OwnerId != session.UserAuthId && !session.HasRole(RoleNames.Admin))
            {
                throw HttpError.Unauthorized("Only the Owner or Admins can delete this Technology");
            }

            Db.DeleteById <Technology>(request.Id);

            var history = existingTech.ConvertTo <TechnologyHistory>();

            history.TechnologyId   = existingTech.Id;
            history.LastModified   = DateTime.UtcNow;
            history.LastModifiedBy = session.UserName;
            history.Operation      = "DELETE";
            Db.Insert(history);

            ContentCache.ClearAll();

            return(new DeleteTechnologyResponse
            {
                Result = new Technology {
                    Id = (long)request.Id
                }
            });
        }
Exemple #2
0
        public object Get(GetTechnology request)
        {
            var key = ContentCache.TechnologyKey(request.Slug, clear: request.Reload);

            return(base.Request.ToOptimizedResultUsingCache(ContentCache.Client, key, () =>
            {
                int id;
                var tech = int.TryParse(request.Slug, out id)
                    ? Db.SingleById <Technology>(id)
                    : Db.Single <Technology>(x => x.Slug == request.Slug.ToLower());

                if (tech == null)
                {
                    throw HttpError.NotFound("Tech stack not found");
                }

                var techStacks = Db.Select(Db.From <TechnologyStack>()
                                           .Join <TechnologyChoice>()
                                           .Join <TechnologyChoice, Technology>()
                                           .Where <TechnologyChoice>(x => x.TechnologyId == tech.Id));

                return new GetTechnologyResponse
                {
                    Technology = tech,
                    TechnologyStacks = techStacks
                };
            }));
        }
        public object Delete(DeleteTechnologyStack request)
        {
            var stack = Db.SingleById <TechnologyStack>(request.Id);

            if (stack == null)
            {
                throw HttpError.NotFound("TechStack not found");
            }

            var session = SessionAs <AuthUserSession>();

            if (stack.OwnerId != session.UserAuthId && !session.HasRole(RoleNames.Admin))
            {
                throw HttpError.Unauthorized("Only the Owner or Admins can delete this TechStack");
            }

            Db.Delete <TechnologyChoice>(q => q.TechnologyStackId == request.Id);
            Db.DeleteById <TechnologyStack>(request.Id);

            var history = stack.ConvertTo <TechnologyStackHistory>();

            history.TechnologyStackId = stack.Id;
            history.LastModified      = DateTime.UtcNow;
            history.LastModifiedBy    = session.UserName;
            history.Operation         = "DELETE";
            Db.Insert(history);

            ContentCache.ClearAll();

            return(new DeleteTechnologyStackResponse
            {
                Result = stack.ConvertTo <TechStackDetails>()
            });
        }
Exemple #4
0
        //Cached AutoQuery
        public object Any(FindTechnologies request)
        {
            var key = ContentCache.TechnologyKey(Request.QueryString.ToString(), clear: request.Reload);

            return(base.Request.ToOptimizedResultUsingCache(ContentCache.Client, key, () =>
            {
                var q = AutoQuery.CreateQuery(request, Request.GetRequestParams());
                return AutoQuery.Execute(request, q);
            }));
        }
Exemple #5
0
        public object Post(CreateTechnology request)
        {
            var slug         = request.Name.GenerateSlug();
            var existingTech = Db.Single <Technology>(q => q.Name == request.Name || q.Slug == slug);

            if (existingTech != null)
            {
                throw new ArgumentException("'{0}' already exists".Fmt(slug));
            }

            var tech    = request.ConvertTo <Technology>();
            var session = SessionAs <AuthUserSession>();

            tech.CreatedBy      = session.UserName;
            tech.Created        = DateTime.UtcNow;
            tech.LastModifiedBy = session.UserName;
            tech.LastModified   = DateTime.UtcNow;
            tech.OwnerId        = session.UserAuthId;
            tech.LogoApproved   = true;
            tech.Slug           = slug;

            var id = Db.Insert(tech, selectIdentity: true);
            var createdTechStack = Db.SingleById <Technology>(id);

            var history = createdTechStack.ConvertTo <TechnologyHistory>();

            history.TechnologyId = id;
            history.Operation    = "INSERT";
            Db.Insert(history);

            ContentCache.ClearAll();

            var postUpdate = AppSettings.EnableTwitterUpdates();

            if (postUpdate)
            {
                var url = new ClientTechnology {
                    Slug = tech.Slug
                }.ToAbsoluteUri();
                PostTwitterUpdate(
                    "Who's using #{0}? {1}".Fmt(tech.Slug.Replace("-", ""), url),
                    Db.ColumnDistinct <long>(Db.From <TechnologyChoice>()
                                             .Where(x => x.TechnologyId == tech.Id)
                                             .Select(x => x.TechnologyStackId)).ToList(),
                    maxLength: 140 - (TweetUrlLength - url.Length));
            }

            return(new CreateTechnologyResponse
            {
                Result = createdTechStack,
            });
        }
Exemple #6
0
        public object Get(GetTechnologyFavoriteDetails request)
        {
            var key = ContentCache.TechnologyFavoriteKey(request.Slug, clear: request.Reload);

            return(base.Request.ToOptimizedResultUsingCache(ContentCache.Client, key, () =>
            {
                int id;
                var tech = int.TryParse(request.Slug, out id)
                    ? Db.SingleById <Technology>(id)
                    : Db.Single <Technology>(x => x.Slug == request.Slug.ToLower());

                var favoriteCount =
                    Db.Count <UserFavoriteTechnology>(x => x.TechnologyId == tech.Id);

                return new GetTechnologyFavoriteDetailsResponse
                {
                    FavoriteCount = (int)favoriteCount
                };
            }));
        }
        public object Get(GetTechnologyStack request)
        {
            DbFactory.RegisterPageView("/stack/" + request.Slug);

            var key = ContentCache.TechnologyStackKey(request.Slug, clear: request.Reload);

            return(base.Request.ToOptimizedResultUsingCache(ContentCache.Client, key, () =>
            {
                int id;
                var techStack = int.TryParse(request.Slug, out id)
                    ? Db.SingleById <TechnologyStack>(id)
                    : Db.Single <TechnologyStack>(x => x.Slug == request.Slug.ToLower());

                if (techStack == null)
                {
                    throw HttpError.NotFound("Tech stack not found");
                }

                var techChoices = Db.LoadSelect(Db.From <TechnologyChoice>()
                                                .Join <Technology>()
                                                .Join <TechnologyStack>()
                                                .Where(x => x.TechnologyStackId == techStack.Id));

                var result = techStack.ConvertTo <TechStackDetails>();
                if (!string.IsNullOrEmpty(techStack.Details))
                {
                    result.DetailsHtml = new Markdown().Transform(techStack.Details);
                }

                result.TechnologyChoices = techChoices.Map(x => x.ToTechnologyInStack());

                var response = new GetTechnologyStackResponse
                {
                    Created = DateTime.UtcNow,
                    Result = result
                };
                return response;
            }));
        }
        public object Any(AppOverview request)
        {
            var key = ContentCache.AppOverviewKey(clear: request.Reload);

            return(base.Request.ToOptimizedResultUsingCache(ContentCache.Client, key, () =>
            {
                var response = new AppOverviewResponse
                {
                    Created = DateTime.UtcNow,
                    AllTiers = GetAllTiers(),
                    TopTechnologies = GetTopTechByCategory(minCount: 1)
                                      .OrderByDescending(x => x.StacksCount)
                                      .Take(100)
                                      .ToList(),
                };

                response.AllTiers.Insert(0, new Option {
                    Title = "[ Top 100 Technologies ]"
                });

                return response;
            }));
        }
Exemple #9
0
        public object Put(UpdateTechnology request)
        {
            var tech = Db.SingleById <Technology>(request.Id);

            if (tech == null)
            {
                throw HttpError.NotFound("Tech not found");
            }

            var session = SessionAs <AuthUserSession>();

            if (tech.IsLocked && !(tech.OwnerId == session.UserAuthId || session.HasRole(RoleNames.Admin)))
            {
                throw HttpError.Unauthorized("This Technology is locked and can only be modified by its Owner or Admins.");
            }

            //Only Post an Update if there was no other update today
            var postUpdate = AppSettings.EnableTwitterUpdates() &&
                             tech.LastStatusUpdate.GetValueOrDefault(DateTime.MinValue) < DateTime.UtcNow.Date;

            tech.PopulateWith(request);
            tech.LastModifiedBy = session.UserName;
            tech.LastModified   = DateTime.UtcNow;

            if (postUpdate)
            {
                tech.LastStatusUpdate = tech.LastModified;
            }

            Db.Save(tech);

            var history = tech.ConvertTo <TechnologyHistory>();

            history.TechnologyId = tech.Id;
            history.Operation    = "UPDATE";
            Db.Insert(history);

            ContentCache.ClearAll();

            var response = new UpdateTechnologyResponse
            {
                Result = tech
            };

            if (postUpdate)
            {
                var url = new ClientTechnology {
                    Slug = tech.Slug
                }.ToAbsoluteUri();
                response.ResponseStatus = new ResponseStatus
                {
                    Message = PostTwitterUpdate(
                        "Who's using #{0}? {1}".Fmt(tech.Slug.Replace("-", ""), url),
                        Db.ColumnDistinct <long>(Db.From <TechnologyChoice>()
                                                 .Where(x => x.TechnologyId == tech.Id)
                                                 .Select(x => x.TechnologyStackId)).ToList(),
                        maxLength: 140 - (TweetUrlLength - url.Length))
                };
            }

            return(response);
        }
        public object Post(CreateTechnologyStack request)
        {
            var slug          = request.Name.GenerateSlug();
            var existingStack = Db.Single <TechnologyStack>(q => q.Name == request.Name || q.Slug == slug);

            if (existingStack != null)
            {
                throw new ArgumentException("'{0}' already exists".Fmt(slug));
            }

            var techStack = request.ConvertTo <TechnologyStack>();
            var session   = SessionAs <AuthUserSession>();

            techStack.CreatedBy      = session.UserName;
            techStack.LastModifiedBy = session.UserName;
            techStack.OwnerId        = session.UserAuthId;
            techStack.Created        = DateTime.UtcNow;
            techStack.LastModified   = techStack.Created;
            techStack.Slug           = slug;

            var techIds = (request.TechnologyIds ?? new List <long>()).ToHashSet();

            //Only Post an Update if Stack has TechCount >= 4
            var postUpdate = AppSettings.EnableTwitterUpdates() && techIds.Count >= 4;

            if (postUpdate)
            {
                techStack.LastStatusUpdate = techStack.Created;
            }

            long id;

            using (var trans = Db.OpenTransaction())
            {
                id = Db.Insert(techStack, selectIdentity: true);

                if (techIds.Count > 0)
                {
                    var techChoices = request.TechnologyIds.Map(x => new TechnologyChoice
                    {
                        TechnologyId      = x,
                        TechnologyStackId = id,
                        CreatedBy         = techStack.CreatedBy,
                        LastModifiedBy    = techStack.LastModifiedBy,
                        OwnerId           = techStack.OwnerId,
                    });

                    Db.InsertAll(techChoices);
                }

                trans.Commit();
            }

            var createdTechStack = Db.SingleById <TechnologyStack>(id);
            var history          = createdTechStack.ConvertTo <TechnologyStackHistory>();

            history.TechnologyStackId = id;
            history.Operation         = "INSERT";
            history.TechnologyIds     = techIds.ToList();
            Db.Insert(history);

            ContentCache.ClearAll();

            if (postUpdate)
            {
                var url = new ClientTechnologyStack {
                    Slug = techStack.Slug
                }.ToAbsoluteUri();
                PostTwitterUpdate(
                    "{0}'s Stack! {1} ".Fmt(techStack.Name, url),
                    request.TechnologyIds,
                    maxLength: 140 - (TweetUrlLength - url.Length));
            }

            return(new CreateTechnologyStackResponse
            {
                Result = createdTechStack.ConvertTo <TechStackDetails>(),
            });
        }
        public object Any(Overview request)
        {
            var key = ContentCache.OverviewKey(clear: request.Reload);

            return(base.Request.ToOptimizedResultUsingCache(ContentCache.Client, key, () =>
            {
                var topTechByCategory = Db.Select <TechnologyInfo>(
                    @"select t.tier, t.slug as Slug, t.name, t.logo_url, COUNT(*) as StacksCount 
                        from technology_choice tc
	                     inner join
	                     technology t on (tc.technology_id = t.id)
                        group by t.tier, t.slug, t.name, t.logo_url
                        having COUNT(*) > 2
                        order by 4 desc");

                var map = new Dictionary <TechnologyTier, List <TechnologyInfo> >();
                foreach (var tech in topTechByCategory)
                {
                    List <TechnologyInfo> techs;
                    if (!map.TryGetValue(tech.Tier, out techs))
                    {
                        map[tech.Tier] = techs = new List <TechnologyInfo>();
                    }

                    techs.Add(tech);
                }

                foreach (var tier in map.Keys)
                {
                    var list = map[tier];
                    list.Sort((x, y) => y.StacksCount - x.StacksCount);
                    if (list.Count > 3)
                    {
                        list.RemoveRange(3, list.Count - 3);
                    }
                }

                var response = new OverviewResponse
                {
                    Created = DateTime.UtcNow,

                    LatestTechStacks = Db.GetTechstackDetails(Db.From <TechnologyStack>().OrderByDescending(x => x.LastModified).Limit(20)),

                    TopUsers = Db.Select <UserInfo>(
                        @"select u.user_name as UserName, u.default_profile_url as AvatarUrl, COUNT(*) as StacksCount
                          from technology_stack ts
	                          left join
	                          user_favorite_technology_stack uf on (ts.id = uf.technology_stack_id)
	                          left join
	                          custom_user_auth u on (u.id = ts.owner_id::integer)
                          group by u.user_name, u.default_profile_url
                          having count(*) > 0
                          order by StacksCount desc
                          limit 20"),

                    TopTechnologies = topTechByCategory
                                      .OrderByDescending(x => x.StacksCount)
                                      .Take(20)
                                      .ToList(),

                    TopTechnologiesByTier = map,
                };

                //Lighten payload
                response.LatestTechStacks.Each(x => {
                    x.Details = x.DetailsHtml = null;
                    x.TechnologyChoices.Each(y => {
                        y.Description = null;
                    });
                });

                //Put TechStacks entry first to provide a first good experience
                var techStacksApp = response.LatestTechStacks.FirstOrDefault(x => x.Id == TechStacksAppId);
                if (techStacksApp != null)
                {
                    response.LatestTechStacks.RemoveAll(x => x.Id == TechStacksAppId);
                    response.LatestTechStacks.Insert(0, techStacksApp);
                }

                return response;
            }));
        }
        public object Put(UpdateTechnologyStack request)
        {
            var techStack = Db.SingleById <TechnologyStack>(request.Id);

            if (techStack == null)
            {
                throw HttpError.NotFound("Tech stack not found");
            }

            var session = SessionAs <AuthUserSession>();

            if (techStack.IsLocked && !(techStack.OwnerId == session.UserAuthId || session.HasRole(RoleNames.Admin)))
            {
                throw HttpError.Unauthorized("This TechStack is locked and can only be modified by its Owner or Admins.");
            }

            var techIds = (request.TechnologyIds ?? new List <long>()).ToHashSet();

            //Only Post an Update if there was no other update today and Stack as TechCount >= 4
            var postUpdate = AppSettings.EnableTwitterUpdates() &&
                             techStack.LastStatusUpdate.GetValueOrDefault(DateTime.MinValue) < DateTime.UtcNow.Date &&
                             techIds.Count >= 4;

            techStack.PopulateWith(request);
            techStack.LastModified   = DateTime.UtcNow;
            techStack.LastModifiedBy = session.UserName;

            if (postUpdate)
            {
                techStack.LastStatusUpdate = techStack.LastModified;
            }

            using (var trans = Db.OpenTransaction())
            {
                Db.Save(techStack);

                var existingTechChoices = Db.Select <TechnologyChoice>(q => q.TechnologyStackId == request.Id);
                var techIdsToAdd        = techIds.Except(existingTechChoices.Select(x => x.TechnologyId)).ToHashSet();
                var techChoices         = techIdsToAdd.Map(x => new TechnologyChoice
                {
                    TechnologyId      = x,
                    TechnologyStackId = request.Id,
                    CreatedBy         = techStack.CreatedBy,
                    LastModifiedBy    = techStack.LastModifiedBy,
                    OwnerId           = techStack.OwnerId,
                });

                var unusedTechChoices = Db.From <TechnologyChoice>().Where(x => x.TechnologyStackId == request.Id);
                if (techIds.Count > 0)
                {
                    unusedTechChoices.And(x => !techIds.Contains(x.TechnologyId));
                }

                Db.Delete(unusedTechChoices);

                Db.InsertAll(techChoices);

                trans.Commit();
            }

            var history = techStack.ConvertTo <TechnologyStackHistory>();

            history.TechnologyStackId = techStack.Id;
            history.Operation         = "UPDATE";
            history.TechnologyIds     = techIds.ToList();
            Db.Insert(history);

            ContentCache.ClearAll();

            var response = new UpdateTechnologyStackResponse
            {
                Result = techStack.ConvertTo <TechStackDetails>()
            };

            if (postUpdate)
            {
                var url = new ClientTechnologyStack {
                    Slug = techStack.Slug
                }.ToAbsoluteUri();
                response.ResponseStatus = new ResponseStatus
                {
                    Message = PostTwitterUpdate(
                        "{0}'s Stack! {1} ".Fmt(techStack.Name, url),
                        request.TechnologyIds,
                        maxLength: 140 - (TweetUrlLength - url.Length))
                };
            }

            return(response);
        }