Example #1
0
        public void AddToFavourite_ShouldReturnTrue(int userId, int idMedia)
        {
            var  myService = new FavouriteService(FavouriteRepository);
            bool result    = FavouriteService.Create(userId, idMedia);

            Assert.True(result);
        }
        public PartialViewResult AjaxMorePosts(GetMorePostsViewModel getMorePostsViewModel)
        {
            // Get the topic
            var topic = TopicService.Get(getMorePostsViewModel.TopicId);

            // Get the permissions for the category that this topic is in
            var permissions = PermissionService.GetPermissions(topic.Category, _membersGroup, MemberService, CategoryPermissionService);

            // If this user doesn't have access to this topic then just return nothing
            if (permissions[AppConstants.PermissionDenyAccess].IsTicked)
            {
                return(null);
            }

            var orderBy = !string.IsNullOrEmpty(getMorePostsViewModel.Order) ?
                          AppHelpers.EnumUtils.ReturnEnumValueFromString <PostOrderBy>(getMorePostsViewModel.Order) : PostOrderBy.Standard;



            var viewModel = new ShowMorePostsViewModel
            {
                Topic       = topic,
                Permissions = permissions,
                User        = CurrentMember
            };

            // Map the posts to the posts viewmodel

            // Get all favourites for this user
            var favourites = new List <Favourite>();

            if (CurrentMember != null)
            {
                favourites.AddRange(FavouriteService.GetAllByMember(CurrentMember.Id));
            }

            // Get the posts
            var posts = PostService.GetPagedPostsByTopic(getMorePostsViewModel.PageIndex, Settings.PostsPerPage, int.MaxValue, topic.Id, orderBy);

            // Get all votes for all the posts
            var postIds      = posts.Select(x => x.Id).ToList();
            var allPostVotes = VoteService.GetAllVotesForPosts(postIds);

            viewModel.Posts = new List <ViewPostViewModel>();
            foreach (var post in posts)
            {
                var postViewModel = PostMapper.MapPostViewModel(permissions, post, CurrentMember, Settings, topic, allPostVotes, favourites);
                viewModel.Posts.Add(postViewModel);
            }

            return(PartialView(PathHelper.GetThemePartialViewPath("AjaxMorePosts"), viewModel));
        }
Example #3
0
 public void AddToFavourite_AlreadyExistingFavourite_ShouldReturnFalse(int userId, int idMedia)
 {
     try
     {
         var  myService = new FavouriteService(FavouriteRepository);
         bool result    = FavouriteService.Create(userId, idMedia);
         Assert.False(result);
     }
     catch (EntityException e)
     {
         throw e;
     }
 }
Example #4
0
        public ActionResult Favourites(DialoguePage page)
        {
            using (UnitOfWorkManager.NewUnitOfWork())
            {
                var viewModel = new ViewFavouritesViewModel(page)
                {
                    PageTitle = Lang("Favourites.PageTitle")
                };

                var postIds  = FavouriteService.GetAllByMember(CurrentMember.Id).Select(x => x.PostId);
                var allPosts = PostService.Get(postIds.ToList());
                viewModel.Posts = allPosts;
                return(View(PathHelper.GetThemeViewPath("Favourites"), viewModel));
            }
        }
Example #5
0
 private void CreateServices()
 {
     Output       = new OutputService(this);
     System       = new SystemService(this);
     Status       = new StatusService(this);
     Statistics   = new StatisticsService(this);
     Missing      = new MissingService(this);
     Team         = new TeamService(this);
     Extended     = new ExtendedService(this);
     Favourite    = new FavouriteService(this);
     Insolation   = new InsolationService(this);
     Supply       = new SupplyService(this);
     Search       = new SearchService(this);
     Notification = new NotificationService(this);
 }
        public async Task AddFavourite()
        {
            var vm = new AddFavouriteVM()
            {
                MemeId = 1,
                UserId = "currentUserId",
            };

            favouriteRepoMock.Setup(x =>
                                    x.IsExistAsync(m => m.MemeRefId == vm.MemeId && m.UserId == vm.UserId))
            .ReturnsAsync(false);
            var  service = new FavouriteService(favouriteRepoMock.Object, new FavouriteValidator());
            bool result  = await service.InsertFavourite(vm);

            Assert.True(result);
        }
Example #7
0
        public ActionResult FavouritePost(FavouritePostViewModel viewModel)
        {
            if (Request.IsAjaxRequest() && CurrentMember != null)
            {
                using (var unitOfwork = UnitOfWorkManager.NewUnitOfWork())
                {
                    try
                    {
                        var    post = PostService.Get(viewModel.PostId);
                        string returnValue;

                        // See if this is a user adding or removing the favourite
                        var existingFavourite = FavouriteService.GetByMemberAndPost(CurrentMember.Id, post.Id);
                        if (existingFavourite != null)
                        {
                            FavouriteService.Delete(existingFavourite);
                            returnValue = Lang("Post.Favourite");
                        }
                        else
                        {
                            var favourite = new Favourite
                            {
                                DateCreated = DateTime.UtcNow,
                                MemberId    = CurrentMember.Id,
                                PostId      = post.Id,
                                TopicId     = post.Topic.Id
                            };
                            FavouriteService.Add(favourite);
                            returnValue = Lang("Post.Favourited");
                        }
                        unitOfwork.Commit();
                        return(Content(returnValue));
                    }
                    catch (Exception ex)
                    {
                        unitOfwork.Rollback();
                        LogError(ex);
                        throw new Exception(Lang("Errors.Generic"));
                    }
                }
            }
            return(Content("error"));
        }
        /// <summary>
        /// Used to render the Topic (virtual node)
        /// </summary>
        /// <param name="model"></param>
        /// <param name="topicname">
        /// The topic slug which we use to look up the topic
        /// </param>
        /// <param name="p"></param>
        /// <returns></returns>
        public ActionResult Show(RenderModel model, string topicname, int?p = null)
        {
            var tagPage = model.Content as DialogueVirtualPage;

            if (tagPage == null)
            {
                throw new InvalidOperationException("The RenderModel.Content instance must be of type " + typeof(DialogueVirtualPage));
            }

            if (string.IsNullOrEmpty(topicname))
            {
                return(ErrorToHomePage(Lang("Errors.GenericMessage")));
            }

            // Set the page index
            var pageIndex = AppHelpers.ReturnCurrentPagingNo();

            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                // Get the topic
                var topic = TopicService.GetTopicBySlug(topicname);

                if (topic != null)
                {
                    // Note: Don't use topic.Posts as its not a very efficient SQL statement
                    // Use the post service to get them as it includes other used entities in one
                    // statement rather than loads of sql selects

                    var sortQuerystring = Request.QueryString[AppConstants.PostOrderBy];
                    var orderBy         = !string.IsNullOrEmpty(sortQuerystring) ?
                                          AppHelpers.EnumUtils.ReturnEnumValueFromString <PostOrderBy>(sortQuerystring) : PostOrderBy.Standard;

                    // Store the amount per page
                    var amountPerPage  = Settings.PostsPerPage;
                    var hasCommentHash = Request.Url != null &&
                                         Request.Url.PathAndQuery.IndexOf("#comment-",
                                                                          StringComparison.CurrentCultureIgnoreCase) >= 0;

                    if (sortQuerystring == PostOrderBy.All.ToString() || hasCommentHash)
                    {
                        // Overide to show all posts
                        amountPerPage = int.MaxValue;
                    }


                    // Get the posts
                    var posts = PostService.GetPagedPostsByTopic(pageIndex,
                                                                 amountPerPage,
                                                                 int.MaxValue,
                                                                 topic.Id,
                                                                 orderBy);

                    // Get the permissions for the category that this topic is in
                    var permissions = PermissionService.GetPermissions(topic.Category, _membersGroup, MemberService, CategoryPermissionService);

                    // If this user doesn't have access to this topic then
                    // redirect with message
                    if (permissions[AppConstants.PermissionDenyAccess].IsTicked)
                    {
                        return(ErrorToHomePage(Lang("Errors.NoPermission")));
                    }

                    // See if the user has subscribed to this topic or not
                    var isSubscribed = UserIsAuthenticated && (TopicNotificationService.GetByUserAndTopic(CurrentMember, topic).Any());

                    // Populate the view model for this page
                    var viewModel = new ShowTopicViewModel(model.Content)
                    {
                        Topic        = topic,
                        PageIndex    = posts.PageIndex,
                        TotalCount   = posts.TotalCount,
                        Permissions  = permissions,
                        User         = CurrentMember,
                        IsSubscribed = isSubscribed,
                        UserHasAlreadyVotedInPoll = false,
                        TotalPages = posts.TotalPages
                    };

                    // Get all votes for all the posts
                    var postIds      = posts.Select(x => x.Id).ToList();
                    var allPostVotes = VoteService.GetAllVotesForPosts(postIds);

                    // Get all favourites for this user
                    viewModel.Favourites = new List <Favourite>();
                    if (CurrentMember != null)
                    {
                        viewModel.Favourites.AddRange(FavouriteService.GetAllByMember(CurrentMember.Id));
                    }

                    // Map the topic Start
                    // Get the topic starter post
                    var topicStarter = PostService.GetTopicStarterPost(topic.Id);
                    viewModel.TopicStarterPost = PostMapper.MapPostViewModel(permissions, topicStarter, CurrentMember, Settings, topic, topicStarter.Votes.ToList(), viewModel.Favourites);

                    // Map the posts to the posts viewmodel
                    viewModel.Posts = new List <ViewPostViewModel>();
                    foreach (var post in posts)
                    {
                        var postViewModel = PostMapper.MapPostViewModel(permissions, post, CurrentMember, Settings, topic, allPostVotes, viewModel.Favourites);
                        viewModel.Posts.Add(postViewModel);
                    }

                    // If there is a quote querystring
                    var quote = Request["quote"];
                    if (!string.IsNullOrEmpty(quote))
                    {
                        try
                        {
                            // Got a quote
                            var postToQuote = PostService.Get(new Guid(quote));
                            viewModel.PostContent = postToQuote.PostContent;
                        }
                        catch (Exception ex)
                        {
                            LogError(ex);
                        }
                    }

                    // See if the topic has a poll, and if so see if this user viewing has already voted
                    if (topic.Poll != null)
                    {
                        // There is a poll and a user
                        // see if the user has voted or not
                        var votes = topic.Poll.PollAnswers.SelectMany(x => x.PollVotes).ToList();
                        if (UserIsAuthenticated)
                        {
                            viewModel.UserHasAlreadyVotedInPoll = (votes.Count(x => x.MemberId == CurrentMember.Id) > 0);
                        }
                        viewModel.TotalVotesInPoll = votes.Count();
                    }

                    // update the topic view count only if this topic doesn't belong to the user looking at it
                    var addView = true;
                    if (UserIsAuthenticated && CurrentMember.Id != topic.MemberId)
                    {
                        addView = false;
                    }

                    if (!AppHelpers.UserIsBot() && addView)
                    {
                        // Cool, user doesn't own this topic
                        topic.Views = (topic.Views + 1);
                        try
                        {
                            unitOfWork.Commit();
                        }
                        catch (Exception ex)
                        {
                            LogError(ex);
                        }
                    }

                    return(View(PathHelper.GetThemeViewPath("Topic"), viewModel));
                }
            }
            return(ErrorToHomePage(Lang("Errors.GenericMessage")));
        }
Example #9
0
        public ActionResult Search(DialoguePage page)
        {
            var term = Request["term"];

            if (!string.IsNullOrEmpty(term))
            {
                using (UnitOfWorkManager.NewUnitOfWork())
                {
                    // Set the page index
                    var pageIndex = AppHelpers.ReturnCurrentPagingNo();

                    // Returns the formatted string to search on
                    var formattedSearchTerm = AppHelpers.ReturnSearchString(term);

                    // Create an empty viewmodel
                    var viewModel = new SearchViewModel(page)
                    {
                        Posts             = new List <ViewPostViewModel>(),
                        AllPermissionSets = new Dictionary <Category, PermissionSet>(),
                        PageIndex         = pageIndex,
                        TotalCount        = 0,
                        Term = term
                    };

                    // if there are no results from the filter return an empty search view model.
                    if (string.IsNullOrWhiteSpace(formattedSearchTerm))
                    {
                        return(View(PathHelper.GetThemeViewPath("Search"), viewModel));
                    }

                    //// Get all the topics based on the search value
                    var posts = PostService.SearchPosts(pageIndex,
                                                        Settings.PostsPerPage,
                                                        DialogueConfiguration.Instance.ActiveTopicsListSize,
                                                        term);


                    // Get all the categories for this topic collection
                    var topics      = posts.Select(x => x.Topic).Distinct().ToList();
                    var categoryIds = topics.Select(x => x.CategoryId).Distinct().ToList();
                    var categories  = CategoryService.Get(categoryIds);

                    // create the view model
                    viewModel = new SearchViewModel(page)
                    {
                        AllPermissionSets = new Dictionary <Category, PermissionSet>(),
                        PageIndex         = pageIndex,
                        TotalCount        = posts.TotalCount,
                        Term       = formattedSearchTerm,
                        TotalPages = posts.TotalPages
                    };

                    // Current members favourites
                    var favourites = new List <Favourite>();
                    if (CurrentMember != null)
                    {
                        favourites = FavouriteService.GetAllByMember(CurrentMember.Id);
                    }

                    // Get all votes for all the posts
                    var postIds      = posts.Select(x => x.Id).ToList();
                    var allPostVotes = VoteService.GetAllVotesForPosts(postIds);

                    // loop through the categories and get the permissions
                    foreach (var category in categories)
                    {
                        var permissionSet = PermissionService.GetPermissions(category, _membersGroup, MemberService, CategoryPermissionService);
                        viewModel.AllPermissionSets.Add(category, permissionSet);
                    }

                    // Map the posts to the posts viewmodel
                    viewModel.Posts = new List <ViewPostViewModel>();
                    foreach (var post in posts)
                    {
                        // TODO - See if we can make this more efficient
                        var cat           = categories.FirstOrDefault(x => x.Id == post.Topic.CategoryId);
                        var postViewModel = PostMapper.MapPostViewModel(viewModel.AllPermissionSets[cat], post, CurrentMember, Settings, post.Topic, allPostVotes, favourites, true);
                        viewModel.Posts.Add(postViewModel);
                    }

                    viewModel.PageTitle = string.Concat(Lang("Search.PageTitle"), AppHelpers.SafePlainText(term));

                    return(View(PathHelper.GetThemeViewPath("Search"), viewModel));
                }
            }

            return(Redirect(Settings.ForumRootUrl));
        }