コード例 #1
0
        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);
        }
コード例 #2
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("'{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>(),
            });
        }
コード例 #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>(),
            });
        }