Example #1
0
        public static void FilterCreateTechStackRequest(IRequest req, IResponse res, CreateTechnologyStack dto)
        {
            var dbFactory = req.TryResolve <IDbConnectionFactory>();

            if (req.Verb == "POST")
            {
                using (var db = dbFactory.OpenDbConnection())
                {
                    //Check unqiue name
                    var exists = db.Single <TechnologyStack>(x => x.Name.ToLower() == dto.Name.ToLower());

                    if (exists != null)
                    {
                        throw HttpError.Conflict("A TechnologyStack with that name already exists");
                    }
                }
            }
        }
        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>(),
            });
        }
Example #3
0
        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($"'{slug}' already exists");
            }

            if (string.IsNullOrEmpty(request.AppUrl) || request.AppUrl.IndexOf("://", StringComparison.Ordinal) == -1)
            {
                throw new ArgumentException("A valid URL to the Website or App is required");
            }

            if (string.IsNullOrEmpty(request.Description) || request.Description.Length < 100)
            {
                throw new ArgumentException("Summary needs to be a min of 100 chars");
            }

            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);

            Cache.FlushAll();

            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 async Task <CreateTechnologyStackResponse> Post(CreateTechnologyStack request)
        {
            var slug          = request.Slug;
            var existingStack = await Db.SingleAsync <TechnologyStack>(q => q.Name == request.Name || q.Slug == slug);

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

            if (string.IsNullOrEmpty(request.AppUrl) || request.AppUrl.IndexOf("://", StringComparison.Ordinal) == -1)
            {
                throw new ArgumentException("A valid URL to the Website or App is required");
            }

            if (string.IsNullOrEmpty(request.Description) || request.Description.Length < 100)
            {
                throw new ArgumentException("Summary needs to be a min of 100 chars");
            }

            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;
            techStack.DetailsHtml    = await Markdown.TransformAsync(request.Details, session.GetGitHubToken());

            if (string.IsNullOrEmpty(techStack.ScreenshotUrl) && Request.Files.Length > 0)
            {
                techStack.ScreenshotUrl = Request.Files[0].UploadToImgur(AppSettings.GetString("oauth.imgur.ClientId"),
                                                                         nameof(techStack.ScreenshotUrl), minWidth: 858, minHeight: 689, maxWidth: 2600, maxHeight: 2600);
            }

            if (string.IsNullOrEmpty(techStack.ScreenshotUrl))
            {
                throw new ArgumentException("Screenshot is Required", nameof(techStack.ScreenshotUrl));
            }

            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 = await Db.InsertAsync(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,
                    });

                    await Db.InsertAllAsync(techChoices);
                }

                trans.Commit();
            }

            var createdTechStack = await Db.SingleByIdAsync <TechnologyStack>(id);

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

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

            await Db.ExecuteSqlAsync(
                @"update user_activity set 
                         tech_stacks_count = (select count(*) from technology_stack where owner_id = @userIdStr)
                   where id = @userId",
                new { userId = session.GetUserId(), userIdStr = session.UserAuthId });

            Cache.FlushAll();

            if (postUpdate)
            {
                var url = TwitterUpdates.BaseUrl.CombineWith(new ClientTechnologyStack {
                    Slug = techStack.Slug
                }.ToUrl());
                PostTwitterUpdate(
                    $"{techStack.Name}'s Stack! {url} ",
                    request.TechnologyIds,
                    maxLength: 140 - (TweetUrlLength - url.Length));
            }

            return(new CreateTechnologyStackResponse
            {
                Result = createdTechStack.ConvertTo <TechStackDetails>(),
            });
        }