public async Task <IActionResult> OnPostAsync(int id) { if (!ModelState.IsValid) { return(Page()); } Bookmark existingBookmark = await _context.GetBookmarkAsync(id); if (existingBookmark == null) { return(RedirectToPage("./Manage")); } existingBookmark.Title = !string.IsNullOrWhiteSpace(Bookmark.Title) ? Bookmark.Title : null; existingBookmark.Description = !string.IsNullOrWhiteSpace(Bookmark.Description) ? Bookmark.Description : null; existingBookmark.Tags = BookmarkTagHelper.StringToList(Bookmark.Tags); existingBookmark.IsPublic = Bookmark.IsPublic; existingBookmark.Favorited = Bookmark.Favorited; _context.Update(existingBookmark); await _context.SaveChangesAsync(); string returnPage = Request.Query.ContainsKey("return") ? Request.Query["return"] : "./Manage"; return(RedirectToPage(returnPage)); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } DateTime now = DateTime.UtcNow; var bookmark = new Bookmark(); // Grab information about the user. This will be used to associate user // information with the new bookmark. string githubId = ""; string name = ""; string username = ""; string avatarUrl = ""; foreach (Claim claim in User.Claims) { if (claim.Type == ClaimTypes.NameIdentifier) { githubId = claim.Value; } else if (claim.Type == ClaimTypes.Name) { name = claim.Value; } else if (claim.Type == "urn:github:login") { username = claim.Value; } else if (claim.Type == "urn:github:avatar") { avatarUrl = claim.Value; } } // Using the bookmark URL, find the corresponding resource from the database. var existingResource = await _context.GetResourceByLocationAsync(BookmarkEntity.Location); // If it is found, take that existing resource and use its ID with the new bookmark. if (existingResource != null) { bookmark.ResourceId = existingResource.Id; } else { // If it isn't found, make a new resource. var metadata = new Dictionary <string, string>(); string imageUrl = await ImageDownloaderHelper.FetchImageFromUrl(BookmarkEntity.Location); metadata.Add("ImageURL", imageUrl); var resource = new Resource { Location = BookmarkEntity.Location, Metadata = metadata, CreateDate = now }; // Write the resource to the database and use the ID that ends up being used. var dbResponse = _context.AddResource(resource); await _context.SaveChangesAsync(); bookmark.ResourceId = dbResponse.Entity.Id; } // Repeat the process with adding a resource with adding a user. var existingUser = await _context.GetUserByGithubId(githubId); if (existingUser != null) { bookmark.UserId = existingUser.Id; } else { var user = new User { Username = username, Name = name, GithubId = githubId, CreateDate = now, AvatarUrl = avatarUrl }; existingUser = user; var dbResponse = _context.AddUser(user); await _context.SaveChangesAsync(); bookmark.UserId = dbResponse.Entity.Id; } bookmark.CreateDate = now; bookmark.Title = !string.IsNullOrWhiteSpace(BookmarkEntity.Title) ? BookmarkEntity.Title : null; bookmark.Description = !string.IsNullOrWhiteSpace(BookmarkEntity.Description) ? BookmarkEntity.Description : null; bookmark.Tags = BookmarkTagHelper.StringToList(BookmarkEntity.Tags); bookmark.IsPublic = BookmarkEntity.IsPublic; bookmark.Favorited = BookmarkEntity.Favorited; _context.AddBookmark(bookmark); string userIdentifier = !string.IsNullOrWhiteSpace(existingUser.Name) ? existingUser.Name : existingUser.Username; Notification newNotification = new Notification { UserId = existingUser.Id, CreateDate = DateTime.Now, Text = $"{userIdentifier} added a new bookmark!" }; _context.AddNotification(newNotification); await _context.SaveChangesAsync(); string returnPage = Request.Query.ContainsKey("return") ? Request.Query["return"] : "/Bookmarks/Index"; return(RedirectToPage(returnPage)); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } List <ImportBookmarkModel> importedBookmarks; try { importedBookmarks = JsonSerializer.Deserialize <List <ImportBookmarkModel> >(Json); } catch { ModelState.AddModelError("Json", "Error reading JSON."); return(Page()); } foreach (var bookmark in importedBookmarks) { var entity = new Entities.Bookmark(); var resource = await _context.GetResourceByLocationAsync(bookmark.Location); if (resource == null) { ModelState.AddModelError("Location", $"Resource does not exist for {bookmark.Location}. Resource must be added first."); return(Page()); } entity.ResourceId = resource.Id; var user = await _context.GetUserByUsernameAsync(bookmark.Username); if (user == null) { ModelState.AddModelError("Username", $"User does not exist for {bookmark.Username}. User must be added first."); return(Page()); } entity.UserId = user.Id; entity.Title = bookmark.Title ?? ""; entity.Description = bookmark.Description ?? ""; entity.IsPublic = bookmark.Public; entity.Favorited = bookmark.Favorite; entity.Tags = BookmarkTagHelper.StringToList(bookmark.Tags); entity.CreateDate = DateTime.UtcNow; _context.AddBookmark(entity); } try { await _context.SaveChangesAsync(); TempData["Message"] = $"{importedBookmarks.Count} bookmark(s) were successfully imported."; return(RedirectToPage("./Index")); } catch { ModelState.AddModelError("Json", "Error saving bookmarks to database."); } return(Page()); }