예제 #1
0
        public bool GetNewFeed(int myUserId, int page, int pageSize, out List <Post> result, out Pagination pagination)
        {
            try
            {
                DbService.ConnectDb(out _context);

                var rs = (from post in _context.Posts
                          join author in _context.Users on post.AuthorId equals author.Id
                          join tour in _context.Tours on post.TourId equals tour.Id
                          join tourInfo in _context.TourInfos on tour.TourInfoId equals tourInfo.Id
                          join tourInfoAuthor in _context.Users on tourInfo.CreateById equals tourInfoAuthor.Id
                          join startPlace in _context.Places on tourInfo.StartPlaceId equals startPlace.Id
                          join destinationPlace in _context.Places on tourInfo.DestinatePlaceId equals destinationPlace.Id
                          join host in _context.Users on tourInfo.CreateById equals host.Id
                          join plContexts in _context.PostLikes on new { PostId = post.Id, UserId = myUserId } equals new
                          { PostId = plContexts.PostId, UserId = plContexts.UserId } into pl
                          from subPl in pl.DefaultIfEmpty()
                          join tmContexts in _context.TourMembers on new { TourId = post.Id, UserId = myUserId } equals new
                          { TourId = tmContexts.TourId, UserId = tmContexts.UserId } into tm
                          from subTm in tm.DefaultIfEmpty()
                          where (post.DeletedAt == null)
                          select new
                {
                    Id = post.Id,
                    Post = post,
                    Author = author,
                    Host = host,
                    Tour = tour,
                    TourInfo = tourInfo,
                    PostLike = subPl,
                    TourMember = subTm,
                    StartPlace = startPlace,
                    DestinationPlace = destinationPlace,
                    TourInfoAuthor = tourInfoAuthor,
                })?.AsEnumerable()?.ToList();

                var rswn = (from post in _context.Posts
                            join author in _context.Users on post.AuthorId equals author.Id
                            join plContexts in _context.PostLikes on new { PostId = post.Id, UserId = myUserId } equals new
                            { PostId = plContexts.PostId, UserId = plContexts.UserId } into pl
                            from subPl in pl.DefaultIfEmpty()
                            where post.TourId == null && post.DeletedAt == null
                            select new
                {
                    Id = post.Id,
                    Post = post,
                    Author = author,
                    Host = (User)null,
                    Tour = (Tour)null,
                    TourInfo = (TourInfo)null,
                    PostLike = subPl,
                    TourMember = (TourMember)null,
                    StartPlace = (Place)null,
                    DestinationPlace = (Place)null,
                    TourInfoAuthor = (User)null,
                })?.AsEnumerable()?.ToList();

                rs.AddRange(rswn);

                result = new List <Post>();

                // Sort by start date
                rs = rs.OrderByDescending(t => t.Id).ToList();

                var total = rs.Count();
                var skip  = pageSize * (page - 1);
                pageSize = pageSize <= 0 ? total : pageSize;

                result = rs
                         .Skip(skip)
                         .Take(pageSize)
                         .Select((e) =>
                {
                    var post = e.Post;

                    if (post.TourId != null)
                    {
                        var friendType               = _friendService.CalculateIsFriend(myUserId, e.Author.Id);
                        var hostFriendType           = _friendService.CalculateIsFriend(myUserId, e.Host.Id);
                        var tourInfoAuthorFriendType =
                            _friendService.CalculateIsFriend(myUserId, e.TourInfo.CreateById);

                        var postComments = _context.PostComments.Where(pc => pc.PostId == post.Id)
                                           .OrderByDescending(pc => pc.CreateAt)?.ToList();
                        var totalMember = _context.TourMembers.Count(u =>
                                                                     u.TourId == e.Post.TourId && u.DeletedAt == null && u.AcceptedAt != null);
                        var featuredComment = postComments.Count > 0 ? postComments[0] : null;

                        if (featuredComment != null)
                        {
                            var commentAuthorFriendType =
                                _friendService.CalculateIsFriend(myUserId, featuredComment.UserId);
                            var commentAuthor      = _context.Users.FirstOrDefault(u => u.Id == featuredComment.UserId);
                            featuredComment.Author = commentAuthor?.ToSimpleUser(commentAuthorFriendType);
                        }

                        post.Author          = e.Author.ToSimpleUser(friendType);
                        post.IsLike          = e.PostLike != null && e.PostLike.DeletedAt == null;
                        post.FeaturedComment = featuredComment;

                        var friends = (from tourMember in _context.TourMembers
                                       join friend in (from fr in _context.Friends.Where(fr =>
                                                                                         fr.AcceptedAt != null &&
                                                                                         (fr.UserId == myUserId || fr.RequestedUserId == myUserId))
                                                       select new
                        {
                            Id = fr.UserId == myUserId ? fr.RequestedUserId : fr.UserId
                        }
                                                       ) on tourMember.UserId equals friend.Id
                                       join user in _context.Users on friend.Id equals user.Id
                                       where tourMember.TourId == e.Id
                                       select user
                                       )?.AsEnumerable().ToList();

                        var listFriend = friends.Any()
                                ? friends.Select(u => u.ToSimpleUser(FriendType.Accepted)).ToList()
                                : new List <SimpleUser>();

                        // Add info for tour info
                        e.TourInfo.StartPlace     = e.StartPlace;
                        e.TourInfo.DestinatePlace = e.DestinationPlace;
                        e.TourInfo.CreateBy       = e.TourInfoAuthor.ToSimpleUser(tourInfoAuthorFriendType);

                        post.Tour = new SimpleTour(
                            e.Tour.Id,
                            e.Tour.Name,
                            e.Tour.StartDay,
                            e.Tour.EndDay,
                            totalMember,
                            e.Host.ToSimpleUser(hostFriendType),
                            listFriend,
                            e.Tour.Price,
                            e.TourInfo,
                            e.TourMember?.JoinAt,
                            e.TourMember?.AcceptedAt
                            );
                    }
                    // Post dont have tour
                    else
                    {
                        var friendType   = _friendService.CalculateIsFriend(myUserId, e.Author.Id);
                        var postComments = _context.PostComments.Where(pc => pc.PostId == post.Id)
                                           .OrderByDescending(pc => pc.CreateAt)?.ToList();
                        var featuredComment = postComments.Count > 0 ? postComments[0] : null;

                        if (featuredComment != null)
                        {
                            var commentAuthorFriendType =
                                _friendService.CalculateIsFriend(myUserId, featuredComment.UserId);
                            var commentAuthor      = _context.Users.FirstOrDefault(u => u.Id == featuredComment.UserId);
                            featuredComment.Author = commentAuthor?.ToSimpleUser(commentAuthorFriendType);
                        }

                        post.Author          = e.Author.ToSimpleUser(friendType);
                        post.IsLike          = e.PostLike != null && e.PostLike.DeletedAt == null;
                        post.FeaturedComment = featuredComment;
                    }


                    return(post);
                })
                         .ToList();

                pagination = new Pagination(total, page, pageSize);
            }
            finally
            {
                DbService.DisconnectDb(ref _context);
            }

            return(true);
        }
예제 #2
0
        public bool TryGetTours(int userId, int page, int pageSize, string type, out List <SimpleTour> tours,
                                out Pagination pagination)
        {
            tours      = null;
            pagination = null;
            bool isSuccess;

            type = type?.ToLower();

            try
            {
                DbService.ConnectDb(out _context);

                var toursDb = (from t in _context.Tours
                               join ti in _context.TourInfos on t.TourInfoId equals ti.Id
                               join tm in _context.TourMembers on t.Id equals tm.TourId into tourInfoMember
                               from tim in tourInfoMember.DefaultIfEmpty()
                               join host in _context.Users on t.CreateById equals host.Id
                               let f = (from tourMember in _context.TourMembers
                                        join friend in (from fr in _context.Friends.Where(fr =>
                                                                                          fr.AcceptedAt != null && (fr.UserId == userId || fr.RequestedUserId == userId))
                                                        select new
                {
                    Id = fr.UserId == userId ? fr.RequestedUserId : fr.UserId
                }
                                                        ) on tourMember.UserId equals friend.Id
                                        join user in _context.Users on friend.Id equals user.Id
                                        select user
                                        )
                                       where (type != null && type.Equals("owner") && host.Id == userId) ||
                                       (type != null && type.Equals("member") && tim != null && tim.UserId == userId) ||
                                       ((type == null || (!type.Equals("owner") && !type.Equals("member")) &&
                                         (t.CreateById == userId || (tim != null && tim.UserId == userId))))
                                       select new
                {
                    t.Id,
                    t.Name,
                    t.StartDay,
                    t.EndDay,
                    t.Price,
                    Host = host,
                    Friends = f.ToList(),
                    TourInfo = ti,
                    tim.JoinAt,
                    tim.AcceptedAt
                }).AsEnumerable()?.Distinct((a, b) => a.Id == b.Id).ToList();

                // Sort by start date
                toursDb = toursDb.OrderByDescending(t => t.Id).ToList();

                var total = toursDb.Count();
                var skip  = pageSize * (page - 1);
                pageSize = pageSize <= 0 ? total : pageSize;

                tours = toursDb
                        .Skip(skip)
                        .Take(pageSize)
                        .Select((e) =>
                {
                    var totalMember = _context.TourMembers.Count(t => t.TourId == e.Id);
                    var listFriend  = e.Friends.Any()
                            ? e.Friends.Select(u => u.ToSimpleUser(FriendType.Accepted)).ToList()
                            : new List <SimpleUser>();

                    return(new SimpleTour(
                               e.Id,
                               e.Name,
                               e.StartDay,
                               e.EndDay,
                               totalMember,
                               e.Host.ToSimpleUser(_friendService.CalculateIsFriend(userId, e.Host.Id)),
                               listFriend,
                               e.Price,
                               e.TourInfo,
                               e.JoinAt,
                               e.AcceptedAt));
                })
                        .ToList();

                pagination = new Pagination(total, page, pageSize);
                isSuccess  = true;
            }
            finally
            {
                DbService.DisconnectDb(ref _context);
            }

            return(isSuccess);
        }