コード例 #1
0
        public ActionResult <ShortLinkPair> Encode(string longLink)
        {
            try
            {
                // Check to see if the link to be encoded is a valid uri. If not, throw an exception.
                VerifyLinkValidity(longLink);
                var pair = GetPairByLongLink(longLink);

                if (pair != null)
                {
                    return(pair);
                }

                // The long link isn't in the database so generate a shortened link for it to be saved to the database.
                var shortenedLink = GenerateShortLink(longLink);

                // Create a new pair and add it to the database.
                pair = new ShortLinkPair()
                {
                    LongLink = longLink, ShortenedLink = shortenedLink
                };

                this.apiContext.AddPair(pair);
                this.apiContext.SaveChanges();

                return(pair);
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
コード例 #2
0
ファイル: ApiContext.cs プロジェクト: athomas-wtv/shortlink
 // POST
 public void AddPair(ShortLinkPair pair)
 {
     // Adds a pair to the database.
     Pairs.Add(pair);
 }