public async Task <int> Follow([FromBody] FollowerDTO follower)
        {
            var f = mapper.Map <FollowerDTO, Follower>(follower);

            try
            {
                var m = await context.followers
                        .SingleAsync(a => a.followerId == follower.followerId && a.userId == follower.userId);

                context.followers.Remove(m);
                await context.SaveChangesAsync();

                return(-1);
            }
            catch
            {
                await context.followers.AddAsync(f);

                await context.SaveChangesAsync();

                var not = new Notification()
                {
                    actionId    = f.id,
                    ownerUserId = f.userId,
                    userId      = f.followerId,
                    seen        = false,
                    type        = "follow",
                    postId      = null
                };
                await context.notifications.AddAsync(not);

                await context.SaveChangesAsync();
            }
            return(f.id);
        }
        public async Task DeleteAsync(int id)
        {
            FitnessPost fitnessPost = await _context.FitnessPosts.FindAsync(id);

            if (fitnessPost != null)
            {
                _context.Remove(fitnessPost);
                await _context.SaveChangesAsync();
            }
        }
Пример #3
0
        /// <summary>
        /// Allow a user to follow another user so they see their posts in their time line.
        /// </summary>
        /// <param name="followerName"></param>
        /// <param name="followedName"></param>
        /// <param name="_ctx"></param>
        /// <returns></returns>
        public async Task <bool> Follow(string followerName, string followedName, InstaDbContext _ctx)
        {
            // Check if user is already following
            dynamic following = await IsFollowing(followedName, followedName, _ctx);

            if (following)
            {
                return(true);
            }

            // Can't follow yourself
            if (followerName == followedName)
            {
                return(false);
            }

            following = new Following
            {
                Accepted      = true,
                Timestamp     = DateTime.Now,
                UserFollowed  = await _userService.GetByUsername(followedName, _ctx),
                UserFollowing = await _userService.GetByUsername(followerName, _ctx)
            };

            _ctx.Following.Add(following);
            await _ctx.SaveChangesAsync();

            return(true);
        }
Пример #4
0
        public async void CanGetAllPosts()
        {
            DbContextOptions <InstaDbContext> options =
                new DbContextOptionsBuilder <InstaDbContext>
                    ().UseInMemoryDatabase("GetPosts").Options;

            using (InstaDbContext context = new InstaDbContext(options))
            {
                FitnessPost post = new FitnessPost();
                post.ID          = 1;
                post.Location    = "Golds Gym";
                post.URL         = "crimson.jpeg";
                post.Description = "Doing my best to stay fit";

                FitnessPost post2 = new FitnessPost();
                post2.ID          = 2;
                post2.Location    = "Anytime fitness";
                post2.URL         = "packer.jpeg";
                post2.Description = "Loving life";

                FitMangementService fitService = new FitMangementService(context);
                await context.FitnessPosts.AddAsync(post);

                await context.FitnessPosts.AddAsync(post2);

                await context.SaveChangesAsync();

                List <FitnessPost> find = await fitService.GetFitnessPosts();

                Assert.Equal(2, find.Count);
            }
        }
Пример #5
0
        public async Task <string> Toggle(int postId)
        {
            var user = await _ctx.Users.FirstOrDefaultAsync(u => u.UserName == User.Identity.Name);

            var post = await _ctx.Posts.FirstOrDefaultAsync(p => p.Id == postId);

            var like = await _ctx.Likes.FirstOrDefaultAsync(l => l.Post.Id == postId && l.User.UserName == User.Identity.Name);

            if (like == null)
            {
                // Not yet liked, add new like
                var newLike = new Like
                {
                    Post      = post,
                    Timestamp = DateTime.Now,
                    User      = user
                };

                _ctx.Likes.Add(newLike);

                var notification = await _notificationService.CreateNotification(post.User.UserName, User.Identity.Name, string.Format("{0} has liked your post.", User.Identity.Name), _ctx);

                await _notificationService.SendNotification(notification, _ctx);
            }
            else
            {
                // Already liked post, unlike it
                _ctx.Likes.Remove(like);
            }

            await _ctx.SaveChangesAsync();

            return(String.Empty);
        }
        /// <summary>
        /// Mark a user's notifications as viewed.
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="_ctx"></param>
        /// <returns></returns>
        public async Task MarkNotificationsViewed(string userName, InstaDbContext _ctx)
        {
            var notifications = _ctx.Notifications.Where(n => n.ToUser.UserName == userName).ToList();

            notifications.ForEach(n => n.Viewed = true);

            await _ctx.SaveChangesAsync();
        }
Пример #7
0
        public async Task <int> AddPost([FromBody] PostDTO post)
        {
            try
            {
                var newPost = mapper.Map <PostDTO, Post>(post);
                newPost.date = DateTime.Now;
                await context.posts.AddAsync(newPost);

                await context.SaveChangesAsync();

                return(newPost.Id);
            }
            catch
            {
                return(-1);
            }
        }
Пример #8
0
        /// <summary>
        /// Save the new post in the database
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="postMedia"></param>
        /// <param name="post"></param>
        /// <param name="_ctx"></param>
        /// <returns></returns>
        public async Task <int> SavePost(string userName, string postMedia, Post post, InstaDbContext _ctx)
        {
            post.Image     = postMedia;
            post.Timestamp = DateTime.Now;
            post.User      = await _userService.GetByUsername(userName, _ctx);

            _ctx.Posts.Add(post);
            return(await _ctx.SaveChangesAsync());
        }
        /// <summary>
        /// Send the notification.
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="_ctx"></param>
        /// <returns></returns>
        public async Task SendNotification(Notification notification, InstaDbContext _ctx)
        {
            if (notification == null || notification.FromUser.Id == notification.ToUser.Id)
            {
                return;
            }

            _ctx.Notifications.Add(notification);
            await _ctx.SaveChangesAsync();
        }
Пример #10
0
        /// <summary>
        /// Allow a user to un-follow somebody they were previously following.
        /// </summary>
        /// <param name="followerName"></param>
        /// <param name="followedName"></param>
        /// <param name="_ctx"></param>
        /// <returns></returns>
        public async Task <bool> Unfollow(string followerName, string followedName, InstaDbContext _ctx)
        {
            dynamic following = await IsFollowing(followerName, followedName, _ctx);

            if (!following)
            {
                return(false);
            }

            _ctx.Following.RemoveRange(await _ctx.Following.Where(f =>
                                                                  f.UserFollowing.UserName == followerName && f.UserFollowed.UserName == followedName
                                                                  ).ToListAsync());

            await _ctx.SaveChangesAsync();

            return(true);
        }
Пример #11
0
        public async Task <Object> Register([FromBody] AuthUser user)
        {
            var newUser = new IdentityUser()
            {
                UserName = user.name, Email = user.email
            };
            var reg = await this.manager.CreateAsync(newUser, user.password);

            if (!reg.Succeeded)
            {
                return new { id = String.Empty, errors = reg.Errors }
            }
            ;

            var res = await this.signInManager.PasswordSignInAsync(user.name, user.password, false, false);

            if (!res.Succeeded)
            {
                return new { id = String.Empty }
            }
            ;
            try
            {
                var model = new InstaUser()
                {
                    IdentityId = newUser.Id,
                    email      = newUser.Email,
                    name       = newUser.UserName,
                    picPath    = "salah.jpg"
                };

                await context.instaUsers.AddAsync(model);

                await context.SaveChangesAsync();
            }
            catch { return(String.Empty); }
            var ret = await manager.FindByNameAsync(user.name);

            return(new { id = ret.Id });
        }
        public async Task <ActionResult> Add(int postId, string comment)
        {
            var user = await _ctx.Users.FirstOrDefaultAsync(u => u.UserName == User.Identity.Name);

            var post = await _ctx.Posts.FirstOrDefaultAsync(p => p.Id == postId);

            var newComment = new Comment
            {
                Message   = comment,
                Post      = post,
                Timestamp = DateTime.Now,
                User      = user
            };

            _ctx.Comments.Add(newComment);
            await _ctx.SaveChangesAsync();

            var notification = await _notificationService.CreateNotification(post.User.UserName, User.Identity.Name, string.Format("{0} has commented on your post.", User.Identity.Name), _ctx);

            await _notificationService.SendNotification(notification, _ctx);

            return(PartialView("~/Views/Post/_Comments.cshtml", post.Comments.ToList()));
        }