예제 #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
                }
            });
        }
        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>()
            });
        }
예제 #3
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,
            });
        }
예제 #4
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 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);
        }