public AddBookmarkResponse AddBookmark(AddBookmarkRequest request) { LogInfo($"Add Bookmark: Url={request.Url}"); Uri url; if (!Uri.TryCreate(request.Url, UriKind.Absolute, out url)) { AbortBadRequest("Url Not Valid"); } // Level 1: generate a short ID that is still unique var id = "1"; SHA256 mySHA256 = SHA256.Create(); byte[] hashValue = mySHA256.ComputeHash(Encoding.ASCII.GetBytes(request.Url)); var encoded = System.Convert.ToBase64String(hashValue).Replace('/', '_').Replace('+', '-'); for (int i = 3; i < 30; i++) { var temporaryId = encoded.Substring(0, i); var temporaryBookmark = RetrieveBookmark(temporaryId); if (temporaryBookmark == null) { id = temporaryId; break; } else if (temporaryBookmark.Url.ToString() == request.Url) { id = temporaryId; break; } } var bookmark = new Bookmark { ID = id, Url = url, }; _table.PutItemAsync(Document.FromJson(SerializeJson(bookmark))).Wait(); return(new AddBookmarkResponse { ID = bookmark.ID }); }
public AddBookmarkResponse AddBookmark(AddBookmarkRequest request) { LogInfo($"Add Bookmark: Url={request.Url}"); Uri url; if (!Uri.TryCreate(request.Url, UriKind.Absolute, out url)) { AbortBadRequest("Url Not Valid"); } // Level 1: generate a short ID that is still unique var id = Guid.NewGuid().ToString("D"); var bookmark = new Bookmark { ID = id, Url = url, }; _table.PutItemAsync(Document.FromJson(SerializeJson(bookmark))).Wait(); return(new AddBookmarkResponse { ID = bookmark.ID }); }