Пример #1
0
        public static async Task <AlbumItem> GetMediaAsync(
            ApplicationDbContext dbContext,
            int mediaId)
        {
            UserMedia entity = await dbContext.UserMedias
                               .Include(a => a.Description)
                               .Include(a => a.Likes.Select(like => like.User))
                               .Include(a => a.CommentThread.Comments.Select(c => c.Text))
                               .Include(a => a.CommentThread.Comments.Select(c => c.User.Avatar))
                               .SingleOrDefaultAsync(a => a.UserMediaId == mediaId);

            if (entity == null)
            {
                return(null);
            }

            return(new AlbumItem
            {
                Id = entity.UserMediaId,
                UserId = entity.UserId,
                Title = entity.Title,
                Description = entity.Description?.Content,
                CreatedTime = entity.CreatedUtc.ToString("s"),
                CreatedTimeAgo = TimeOperations.GetTimeAgo(entity.CreatedUtc),
                MediaUrl = UserMediaOperations.GetUrl(entity),
                LikeUrl = UserMediaOperations.GetLikeUrl(entity),
                LikeGroups = LikeOperations.MakeLikeGroups(entity.Likes),
                CommentUrl = UserMediaOperations.GetCommentUrl(entity),
                Comments = CommentOperations.GetComments(entity.CommentThread)
            });
        }
Пример #2
0
        public static async Task <AlbumDetail> GetAlbumAsync(
            ApplicationDbContext dbContext,
            int albumId)
        {
            MediaAlbum entity = await dbContext.MediaAlbums
                                .Include(a => a.UserMedias.Select(um => um.Description))
                                .Include(a => a.UserMedias.Select(um => um.Likes.Select(like => like.User)))
                                .Include(a => a.UserMedias.Select(um => um.CommentThread.Comments.Select(c => c.Text)))
                                .Include(a => a.UserMedias.Select(um => um.CommentThread.Comments.Select(c => c.User.Avatar)))
                                .Include(a => a.Likes.Select(like => like.User))
                                .Include(a => a.CommentThread.Comments.Select(c => c.Text))
                                .Include(a => a.CommentThread.Comments.Select(c => c.User.Avatar))
                                .SingleOrDefaultAsync(a => a.MediaAlbumId == albumId);

            if (entity == null)
            {
                return(null);
            }

            return(new AlbumDetail
            {
                Title = entity.Title,
                UserId = entity.UserId,
                Description = entity.Description,
                Items = entity.UserMedias
                        .Select(
                    um =>
                    new AlbumItem
                {
                    Id = um.UserMediaId,
                    UserId = um.UserId,
                    Title = um.Title,
                    Description = um.Description?.Content,
                    CreatedTime = um.CreatedUtc.ToString("s"),
                    CreatedTimeAgo = TimeOperations.GetTimeAgo(um.CreatedUtc),
                    MediaUrl = UserMediaOperations.GetUrl(um),
                    LikeUrl = UserMediaOperations.GetLikeUrl(um),
                    LikeGroups = LikeOperations.MakeLikeGroups(um.Likes),
                    CommentUrl = UserMediaOperations.GetCommentUrl(um),
                    Comments = CommentOperations.GetComments(um.CommentThread)
                })
                        .ToList(),
                LikeUrl = $"/api/Albums/{entity.MediaAlbumId}/Like",
                LikeGroups = LikeOperations.MakeLikeGroups(entity.Likes),
                CommentUrl = $"/api/Albums/{entity.MediaAlbumId}/Comment",
                Comments = CommentOperations.GetComments(entity.CommentThread)
            });
        }
Пример #3
0
        public static async Task <IList <AlbumSummary> > GetAlbumSummariesAsync(
            ApplicationDbContext dbContext,
            UserInfo user)
        {
            var results = await dbContext.MediaAlbums
                          .Where(a => a.UserId == user.UserInfoId)
                          .Include(a => a.Likes.Select(like => like.User))
                          .Include(a => a.CommentThread.Comments.Select(c => c.Text))
                          .Include(a => a.CommentThread.Comments.Select(c => c.User.Avatar))
                          .Select(a =>
                                  new
            {
                a.MediaAlbumId,
                a.Title,
                a.Description,
                Medias      = a.UserMedias.Take(4),
                LatestMedia = a.UserMedias.OrderByDescending(um => um.CreatedUtc).FirstOrDefault(),
                Likes       = a.Likes,
                a.CommentThread
            })
                          .ToListAsync();

            return(results
                   .Select(
                       a =>
                       new AlbumSummary
            {
                Id = a.MediaAlbumId,
                UserId = user.UserInfoId,
                Title = a.Title,
                Description = a.Description,
                SampleMediaUrls = a.Medias.Select(UserMediaOperations.GetUrl).ToList(),
                LastModifiedText = a.LatestMedia != null
                            ? TimeOperations.GetTimeAgo(a.LatestMedia.CreatedUtc)
                            : "",
                LikeUrl = $"/api/Albums/{a.MediaAlbumId}/Like",
                LikeGroups = LikeOperations.MakeLikeGroups(a.Likes),
                CommentUrl = $"/api/Albums/{a.MediaAlbumId}/Comment",
                Comments = CommentOperations.GetComments(a.CommentThread)
            })
                   .ToList());
        }
Пример #4
0
        private static async Task <IList <TimelineEntryDetails> > EntriesQueryToDetailsAsync(
            IQueryable <TimelineEntry> q)
        {
            var results = await q
                          .Include(te => te.User.Avatar)
                          .Include(te => te.Message.Likes.Select(like => like.User))
                          .Include(te => te.CommentThread.Comments.Select(c => c.Text))
                          .Include(te => te.CommentThread.Comments.Select(c => c.User.Avatar))
                          .Select(
                te =>
                new
            {
                te.TimelineEntryId,
                User     = te.User,
                Message  = te.Message.Content,
                DateTime = te.Message.CreatedUtc,
                Media    = te.Media.Select(tme => tme.Media),
                Likes    = te.Message.Likes,
                te.CommentThread
            })
                          .ToListAsync();

            return(results.Select(
                       te =>
                       new TimelineEntryDetails
            {
                UserId = te.User.UserInfoId,
                UserName = te.User.Name,
                AvatarUrl = UserOperations.GetAvatarUrl(te.User),
                Message = te.Message,
                DateTime = te.DateTime.ToString("s"),
                MediaUrls = te.Media.Select(UserMediaOperations.GetUrl).ToList(),
                LikeUrl = $"/api/Timeline/{te.TimelineEntryId}/Like",
                LikeGroups = LikeOperations.MakeLikeGroups(te.Likes),
                CommentUrl = $"/api/Timeline/{te.TimelineEntryId}/Comment",
                Comments = CommentOperations.GetComments(te.CommentThread)
            })
                   .ToList());
        }
Пример #5
0
        public static async Task <HttpStatusCode> AddAlbumCommentAsync(
            ApplicationDbContext dbContext,
            string userId,
            int albumId,
            CommentRequest comment)
        {
            MediaAlbum albumEntity = await dbContext.MediaAlbums
                                     .SingleOrDefaultAsync(te => te.MediaAlbumId == albumId);

            if (albumEntity == null)
            {
                // The entry id is part of the URL, so return a 404.
                return(HttpStatusCode.NotFound);
            }

            return(await CommentOperations.AddCommentAsync(
                       dbContext,
                       userId,
                       new CommentItemIds { AlbumId = albumId },
                       e => e.CommentThread,
                       albumEntity,
                       comment));
        }
Пример #6
0
        public static async Task <HttpStatusCode> AddTimelineEntryCommentAsync(
            ApplicationDbContext dbContext,
            string userId,
            int entryId,
            CommentRequest comment)
        {
            TimelineEntry entryEntity = await dbContext.TimelineEntries
                                        .SingleOrDefaultAsync(te => te.TimelineEntryId == entryId);

            if (entryEntity == null)
            {
                // The entry id is part of the URL, so return a 404.
                return(HttpStatusCode.NotFound);
            }

            return(await CommentOperations.AddCommentAsync(
                       dbContext,
                       userId,
                       new CommentItemIds { TimelineEntryId = entryId },
                       e => e.CommentThread,
                       entryEntity,
                       comment));
        }