Esempio n. 1
0
        public async Task <IActionResult> OnPostAsync(int thisuser, int viewinguser)
        {
            ThisUser = await _context.GetUserAsync(thisuser);

            ViewingUser = await _context.GetUserAsync(viewinguser);

            if (ThisUser.Subscriptions.Contains(ViewingUser))
            {
                ThisUser.Subscriptions.Remove(ViewingUser);
                ViewingUser.Followers.Remove(ThisUser);
            }
            else
            {
                ThisUser.Subscriptions.Add(ViewingUser);
                ViewingUser.Followers.Add(ThisUser);
            }

            string thisname    = !string.IsNullOrWhiteSpace(ThisUser.Name) ? ThisUser.Name : ThisUser.Username;
            string viewingname = !string.IsNullOrWhiteSpace(ViewingUser.Name) ? ViewingUser.Name : ViewingUser.Username;

            _context.AddNotification(new Notification {
                UserId     = ThisUser.Id,
                CreateDate = DateTime.Now,
                Text       = $"{thisname} followed {viewingname}!"
            });

            await _context.SaveChangesAsync();

            return(RedirectToPage("/Profile/Index", new { id = ViewingUser.Id }));
        }
Esempio n. 2
0
        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));
        }