Exemplo n.º 1
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,
            });
        }
Exemplo n.º 2
0
        public static void FilterCreateTechRequest(IRequest req, IResponse res, CreateTechnology dto)
        {
            var dbFactory = req.TryResolve <IDbConnectionFactory>();

            if (req.Verb == "POST")
            {
                using (var db = dbFactory.OpenDbConnection())
                {
                    //Check unqiue name
                    var exists = db.Single <Technology>(x => x.Name.ToLower() == dto.Name.ToLower());
                    if (exists != null)
                    {
                        throw HttpError.Conflict("A Technology with that name already exists");
                    }
                }
            }
        }
        public object Post(CreateTechnology request)
        {
            var slug         = request.Slug;
            var existingTech = Db.Single <Technology>(q => q.Name == request.Name || q.Slug == slug);

            if (existingTech != null)
            {
                throw new ArgumentException($"'{slug}' already exists", nameof(request.Name));
            }

            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;
            tech.LogoUrl        = request.LogoUrl;

            if (string.IsNullOrEmpty(tech.LogoUrl) && Request.Files.Length > 0)
            {
                tech.LogoUrl = Request.Files[0].UploadToImgur(AppSettings.GetString("oauth.imgur.ClientId"),
                                                              nameof(tech.LogoUrl), minWidth: 100, minHeight: 50, maxWidth: 2000, maxHeight: 1000);
            }

            if (string.IsNullOrEmpty(tech.LogoUrl))
            {
                throw new ArgumentException("Logo is Required", nameof(request.LogoUrl));
            }

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

            Db.ExecuteSql(
                @"update user_activity set 
                                 technology_count = (select count(*) from technology where owner_id = @userIdStr)
                           where id = @userId",
                new { userId = session.GetUserId(), userIdStr = session.UserAuthId });

            Cache.FlushAll();

            var postUpdate = AppSettings.EnableTwitterUpdates();

            if (postUpdate)
            {
                var url = TwitterUpdates.BaseUrl.CombineWith(new ClientTechnology {
                    Slug = tech.Slug
                }.ToUrl());
                var twitterSlug = tech.Slug.Replace("-", "");
                PostTwitterUpdate(
                    $"Who's using #{twitterSlug}? {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,
            });
        }