Пример #1
0
        public async void CanFindAPost()
        {
            DbContextOptions <InstaDbContext> options =
                new DbContextOptionsBuilder <InstaDbContext>
                    ().UseInMemoryDatabase("FindPost").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 fitService.FindFitnessPost(1);

                FitnessPost find = await context.FitnessPosts.FirstOrDefaultAsync(r => r.ID == post.ID);

                Assert.Equal("Golds Gym", post.Location);
            }
        }
Пример #2
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);
            }
        }
Пример #3
0
 //<summary>
 //Get a list of all the users the user provided is following
 //</summary>
 //<param name = "userName" ></ param >
 //< param name="_ctx"></param>
 //<returns></returns>
 public async Task <List <ApplicationUser> > GetFollowing(string userName, InstaDbContext _ctx)
 {
     return(await _ctx.Following
            .Where(f => f.UserFollowing.UserName == userName)
            .Select(f => f.UserFollowed)
            .ToListAsync());
 }
Пример #4
0
        public async void CanDeleteAPost()
        {
            DbContextOptions <InstaDbContext> options =
                new DbContextOptionsBuilder <InstaDbContext>
                    ().UseInMemoryDatabase("DeletePost").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    = "Golds Gym";
                post2.URL         = "crimson.jpeg";
                post2.Description = "Doing my best to stay fit";

                FitMangementService fitService = new FitMangementService(context);

                await fitService.DeleteAsync(post2.ID);

                var deleted = await context.FitnessPosts.FirstOrDefaultAsync(r => r.ID == post2.ID);

                Assert.Null(deleted);
            }
        }
Пример #5
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);
        }
Пример #6
0
 public AuthController(SignInManager <IdentityUser> signInManager,
                       UserManager <IdentityUser> manager, InstaDbContext context, IMapper mapper)
 {
     this.signInManager = signInManager;
     this.mapper        = mapper;
     this.manager       = manager;
     this.context       = context;
 }
        /// <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();
        }
        /// <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();
        }
Пример #9
0
        /// <summary>
        /// Generate a date ordered time line of posts from post by users the user is following
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="_ctx"></param>
        /// <returns></returns>
        public async Task <List <Post> > GetTimelinePosts(string userName, InstaDbContext _ctx)
        {
            // Get list of users being followed, we will display posts by them
            var following = await _followService.GetFollowing(userName, _ctx);

            // Remove any posts which aren't made by a user being followed
            var posts = await _ctx.Posts.OrderByDescending(p => p.Timestamp).ToListAsync();

            posts.RemoveAll(p => !following.Contains(p.User) && p.User.UserName != userName);

            return(posts);
        }
Пример #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);
        }
        /// <summary>
        /// Get the provided user's notifications.
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="_ctx"></param>
        /// <returns></returns>
        public Task <List <Notification> > GetNotifications(string userName, InstaDbContext _ctx, bool includeViewed = false)
        {
            var notifications = _ctx.Notifications.Where(n => n.ToUser.UserName == userName);

            return(includeViewed ? notifications.ToListAsync() : notifications.Where(n => !n.Viewed).ToListAsync());
        }
Пример #12
0
 public async Task <ApplicationUser> GetByUsername(string userName, InstaDbContext _ctx)
 {
     return(await _ctx.Users.FirstOrDefaultAsync(u => u.UserName == userName));
 }
Пример #13
0
 /// <summary>
 /// Get the total number of users the provided user is following.
 /// </summary>
 /// <param name="userName"></param>
 /// <param name="_ctx"></param>
 /// <returns></returns>
 public async Task <int> FollowingCount(string userName, InstaDbContext _ctx)
 {
     return(await _ctx.Following.Where(f => f.UserFollowing.UserName == userName).CountAsync());
 }
Пример #14
0
        /// <summary>
        /// Save the post media to the server
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="post"></param>
        /// <param name="imageFile"></param>
        /// <param name="uploadDirectory"></param>
        /// <param name="_ctx"></param>
        /// <returns></returns>
        public string SavePostMedia(string userName, Post post, HttpPostedFileBase imageFile, string uploadDirectory, InstaDbContext _ctx)
        {
            if (imageFile == null || imageFile.ContentLength <= 0)
            {
                return(string.Empty);
            }

            // Save the image to server
            var fileName = Path.GetRandomFileName().Replace(".", "") + ".png";

            if (!Directory.Exists(uploadDirectory))
            {
                Directory.CreateDirectory(uploadDirectory);
            }
            var path = Path.Combine(uploadDirectory, fileName);

            imageFile.SaveAs(path);

            return(fileName);
        }
Пример #15
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());
        }
Пример #16
0
 public ProfileController(InstaDbContext context, IMapper mapper)
 {
     this.context = context;
     this.mapper  = mapper;
 }
Пример #17
0
 public async Task <ApplicationUser> GetByUsernameOrId(string userName, InstaDbContext _ctx)
 {
     return(await _userRepository.GetByUsernameOrId(userName, _ctx));
 }
Пример #18
0
 public HomeController(InstaDbContext context, IMapper mapper, IHostingEnvironment env)
 {
     this.context = context;
     this.mapper  = mapper;
     this.env     = env;
 }
        /// <summary>
        /// Create a notification to be sent.
        /// </summary>
        /// <param name="toUser"></param>
        /// <param name="fromUser"></param>
        /// <param name="notificationMessage"></param>
        /// <param name="_ctx"></param>
        /// <returns></returns>
        public async Task <Notification> CreateNotification(string toUser, string fromUser, string notificationMessage, InstaDbContext _ctx)
        {
            var notification = new Notification
            {
                ToUser    = await _userService.GetByUsername(toUser, _ctx),
                FromUser  = await _userService.GetByUsername(fromUser, _ctx),
                Message   = notificationMessage,
                Viewed    = false,
                Timestamp = DateTime.Now
            };

            return(notification);
        }
Пример #20
0
        /// <summary>
        /// Check if a user is following another user.
        /// </summary>
        /// <param name="followerName"></param>
        /// <param name="followedName"></param>
        /// <param name="_ctx"></param>
        /// <returns></returns>
        public async Task <bool> IsFollowing(string followerName, string followedName, InstaDbContext _ctx)
        {
            var following = await _ctx.Following.FirstOrDefaultAsync(f =>
                                                                     f.UserFollowing.UserName == followerName && f.UserFollowed.UserName == followedName
                                                                     );

            return(following != null);
        }
Пример #21
0
 public FitMangementService(InstaDbContext context)
 {
     _context = context;
 }