public ShortLinkResponse Post([FromBody] ShortLinkRequest slrequest)
        {
            LinkManager       urlManager = new LinkManager();
            ShortLinkResponse ro         = new ShortLinkResponse();

            ro = urlManager.AddShortLink(slrequest);

            return(ro);
        }
        public ShortLinkResponse AggiungiLink([FromBody] ShortLinkRequest slrequest)
        {
            //ShortLinkRequest slrequest = JsonConvert.DeserializeObject<ShortLinkRequest>(data);
            LinkManager       urlManager = new LinkManager();
            ShortLinkResponse ro         = new ShortLinkResponse();

            ro = urlManager.AddShortLink(slrequest);

            //return ro;
            var response = new HttpResponseMessage(HttpStatusCode.Created)
            {
                Content = new StringContent(JsonConvert.SerializeObject(ro))
            };

            return(ro);
        }
Ejemplo n.º 3
0
        public ShortLinkResponse AddShortLink(ShortLinkRequest linkRequest)
        {
            ShortLinkResponse ro = new ShortLinkResponse();

            ro.ErrorMessage = string.Empty;

            string Url = linkRequest.Link;

            if (!string.IsNullOrWhiteSpace(Url))
            {
                Url = Url.Trim().ToLower();
            }

            String cached = null;

            cached = CacheManager.GetCached <String>(Url);
            if (!String.IsNullOrWhiteSpace(cached))
            {
                ro.ShortLink         = cached;
                ro.OperationSucceded = true;

                return(ro);
            }

            try
            {
                //var url = new Uri(Url);
                UriBuilder uriBuilder = new UriBuilder(Url);
            }
            catch (Exception)
            {
                ro.OperationSucceded = false;
                ro.ErrorMessage      = "Invalid Link";
                return(ro);
            }

            String newKey = null;

            while (string.IsNullOrEmpty(newKey))
            {
                if (!unitOfWork.ShortLinkRepository.Exists(l => l.Url == Url))
                {
                    newKey = Guid.NewGuid().ToString("N").Substring(0, ConfigManager.KeyLength).ToLower();
                    Data.Models.ShortLink link = new Data.Models.ShortLink();
                    link.Key         = newKey;
                    link.Url         = Url;
                    link.DateCreated = DateTime.Now;
                    try
                    {
                        unitOfWork.ShortLinkRepository.Insert(link);
                        unitOfWork.Save();
                    }
                    catch (Exception)
                    {
                        ro.OperationSucceded = false;
                        ro.ErrorMessage      = "Error insert link in database";
                    }
                }
                else
                {
                    var lnks = unitOfWork.ShortLinkRepository.Get(l => l.Url == Url);
                    newKey = lnks.FirstOrDefault().Key;
                }
            }

            CacheManager.TryAddToCache <String>(Url, newKey);

            AddGuest(linkRequest.GuestGuid);
            AddVisit(newKey, linkRequest.GuestGuid);

            ro.ShortLink         = newKey;
            ro.OperationSucceded = true;

            return(ro);
        }