public IActionResult UnDelete(string key)
        {
            HttpRequest req = Request;

            if (IsUnauthorizedCaller(req))
            {
                return(Unauthorized());
            }

            if (key.Length == 0)
            {
                return(BadRequest("URL value is required."));
            }

            // Check if the deleted URL exists
            LittleUrl item = _littleUrlMongoContext.GetUrl(key.ToLower(), true);

            if (item == null)
            {
                return(NotFound("URL does not exist."));
            }

            // Found document; UnDelete
            item = _littleUrlMongoContext.ToggleDelete(key.ToLower(), false);

            // return deleted item
            return(Ok(item));
        }
        // GET:
        public IActionResult Fetch(string urlkey = null)
        {
            try
            {
                if (!string.IsNullOrEmpty(urlkey))
                {
                    string sResult = _apiHelper.GetUrlByKey(urlkey).Result;
                    if (!string.IsNullOrEmpty(sResult))
                    {
                        LittleUrl url = JsonConvert.DeserializeObject <LittleUrl>(sResult);
                        return(View(url));
                    }
                    else
                    {
                        ViewData["ErrorMessage"] = "Your Little Url was not found in store.";
                    }
                }

                return(View());
            }
            catch (Exception ex)
            {
                TempData.Add("ErrorMessage", String.IsNullOrEmpty(ex.InnerException?.Message ?? "") ? ex.Message : ex.InnerException.Message);
                return(RedirectToAction("Error"));
            }
        }
        public IActionResult Show()
        {
            // Get result from tempData
            string sResult = (string)TempData["urlResult"];

            if (!String.IsNullOrEmpty(sResult))
            {
                LittleUrl url = JsonConvert.DeserializeObject <LittleUrl>(sResult);

                // Set base address
                url.BaseAddressPrefix = MyLittleUrlWebAPIHelper.BaseAddressPrefix;

                // set ReductionRate
                float fVal = 0;
                if (!String.IsNullOrEmpty(url.LongUrl) && !String.IsNullOrEmpty(url.ShortUrl))
                {
                    fVal = 100 * (((float)url.LongUrl.Length - (float)(url.BaseAddressPrefix.Length + url.ShortUrl.Length)) / (float)url.LongUrl.Length);
                }
                TempData["ReductionRate"] = fVal.ToString("0.00");

                return(View(url));
            }

            return(RedirectToAction("Index"));
        }
        // POST
        public IActionResult Create(string urlToEncode)
        {
            try
            {
                if (string.IsNullOrEmpty(urlToEncode))
                {
                    return(RedirectToAction("Index"));
                }

                if (urlToEncode.StartsWith(MyLittleUrlWebAPIHelper.BaseAddressPrefix, StringComparison.CurrentCultureIgnoreCase))
                {
                    TempData.Add("ErrorMessage", "Invalid attempt.  This is already a LittleURL.");
                    return(RedirectToAction("Error"));
                }

                string    sResult = _apiHelper.CreateNewUrl(urlToEncode).Result;
                LittleUrl url     = JsonConvert.DeserializeObject <LittleUrl>(sResult);
                return(RedirectToAction("Index", new { key = url.ShortUrl }));
            }
            catch (Exception ex)
            {
                TempData.Add("ErrorMessage", String.IsNullOrEmpty(ex.InnerException?.Message ?? "") ? ex.Message : ex.InnerException.Message);
                return(RedirectToAction("Error"));
            }
        }
示例#5
0
        public async Task <string> CreateNewUrl(string longUrl)
        {
            try
            {
                LittleUrl newItem = new LittleUrl {
                    LongUrl = longUrl
                };
                HttpResponseMessage httpResponse = await _httpClient.PostAsJsonAsync("api/littleurl", newItem);

                if (httpResponse.StatusCode == HttpStatusCode.ServiceUnavailable)
                {
                    Exception noServiceEx = new Exception(httpResponse.ReasonPhrase);
                    throw noServiceEx;
                }

                httpResponse.EnsureSuccessStatusCode();
                //newItem = await httpResponse.Content.ReadAsAsync<LittleUrl>();
                //return newItem.ShortUrl;

                string sRet = await httpResponse.Content.ReadAsStringAsync();

                return(sRet);
            }
            catch (Exception)
            {
                throw;
            }
        }
        // GET: /<controller>/
        public IActionResult Load(string key)
        {
            if (!string.IsNullOrEmpty(key))
            {
                string sResult = _apiHelper.GetUrlByKey(key).Result;
                if (!string.IsNullOrEmpty(sResult))
                {
                    LittleUrl url = JsonConvert.DeserializeObject <LittleUrl>(sResult);
                    return(Redirect(url.LongUrl));
                }
                else
                {
                    TempData.Add("ErrorMessage", "Your Little Url was not found in store.");
                    return(RedirectToAction("Error"));
                }
            }

            return(RedirectToAction("Index"));
        }
        public IActionResult AddToList([FromBody] LittleUrl lUrl)
        {
            HttpRequest req = Request;

            if (IsUnauthorizedCaller(req))
            {
                return(Unauthorized());
            }

            if (lUrl.LongUrl.Length == 0)
            {
                return(BadRequest("URL value is required."));
            }

            // Check if the URL already exists
            LittleUrl item;

            // item = _littleUrlContext.littleUrlList.FirstOrDefault(url => url.LongUrl == lUrl.LongUrl.ToLower());

            item = _littleUrlMongoContext.CheckUrl(lUrl.LongUrl);

            if (item == null)
            {
                // create
                item = new LittleUrl {
                    UrlId = GetNextId(), LongUrl = lUrl.LongUrl, ShortUrl = GetNewKey()
                };
                //_littleUrlContext.littleUrlList.Add(item);
                //_littleUrlContext.SaveChanges();

                _littleUrlMongoContext.InsertUrl(item);
            }

            // return created/found item
            return(CreatedAtRoute("GetByKey", new { key = item.ShortUrl }, item));
        }
        public IActionResult GetByKey(string key)
        {
            HttpRequest req = Request;

            if (IsUnauthorizedCaller(req))
            {
                return(Unauthorized());
            }

            if (key.Length == 0)
            {
                return(BadRequest("Key value required."));
            }

            // LittleUrl item = _littleUrlContext.littleUrlList.FirstOrDefault(url => url.ShortUrl == key.ToLower());
            LittleUrl item = _littleUrlMongoContext.GetUrl(key.ToLower(), false);

            if (item == null)
            {
                return(NotFound("URL does not exist."));
            }

            return(Ok(item));
        }
        public IActionResult Undelete(string urlKey)
        {
            try
            {
                if (!string.IsNullOrEmpty(urlKey))
                {
                    string sResult = _apiHelper.Undelete(urlKey).Result;
                    if (!string.IsNullOrEmpty(sResult))
                    {
                        if (sResult.StartsWith("{", StringComparison.CurrentCultureIgnoreCase))
                        {
                            LittleUrl url = JsonConvert.DeserializeObject <LittleUrl>(sResult);
                            if (url != null && url.ShortUrl == urlKey)
                            {
                                return(RedirectToAction("List"));
                            }
                        }
                        else
                        {
                            TempData["ErrorMessage"] = sResult;
                        }
                    }
                }
                else
                {
                    TempData["ErrorMessage"] = "Invalid or empty Little Url.";
                }

                return(RedirectToAction("Error"));
            }
            catch (Exception ex)
            {
                TempData.Add("ErrorMessage", String.IsNullOrEmpty(ex.InnerException?.Message ?? "") ? ex.Message : ex.InnerException.Message);
                return(RedirectToAction("Error"));
            }
        }
示例#10
0
 private string _SerializeAsJson(LittleUrl item)
 {
     return("{ " + String.Format("\"id\": {0}, \"longUrl\": \"{1}\", \"shortUrl\": \"{2}\"",
                                 item.UrlId, item.LongUrl, item.ShortUrl) + " }");
 }