roadie/Roadie.Api.Services/PlaylistService.cs

560 lines
26 KiB
C#
Raw Normal View History

2018-12-09 17:58:31 +00:00
using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Roadie.Library;
2018-11-07 04:33:22 +00:00
using Roadie.Library.Caching;
using Roadie.Library.Configuration;
2019-11-17 14:10:17 +00:00
using Roadie.Library.Data.Context;
2018-11-07 04:33:22 +00:00
using Roadie.Library.Encoding;
2018-12-09 17:58:31 +00:00
using Roadie.Library.Enums;
using Roadie.Library.Extensions;
2019-03-01 04:10:44 +00:00
using Roadie.Library.Imaging;
2018-11-07 04:33:22 +00:00
using Roadie.Library.Models;
using Roadie.Library.Models.Pagination;
using Roadie.Library.Models.Playlists;
2019-06-30 22:14:36 +00:00
using Roadie.Library.Models.Statistics;
2018-11-15 00:16:25 +00:00
using Roadie.Library.Models.Users;
2018-11-07 04:33:22 +00:00
using Roadie.Library.Utility;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
2018-11-07 04:33:22 +00:00
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using data = Roadie.Library.Data;
namespace Roadie.Api.Services
{
public class PlaylistService : ServiceBase, IPlaylistService
{
2019-06-30 22:14:36 +00:00
private IBookmarkService BookmarkService { get; }
2018-12-09 17:58:31 +00:00
2018-11-07 04:33:22 +00:00
public PlaylistService(IRoadieSettings configuration,
2019-06-30 22:14:36 +00:00
IHttpEncoder httpEncoder,
IHttpContext httpContext,
2019-11-17 14:10:17 +00:00
IRoadieDbContext dbContext,
2019-06-30 22:14:36 +00:00
ICacheManager cacheManager,
ILogger<PlaylistService> logger,
IBookmarkService bookmarkService)
2018-11-07 04:33:22 +00:00
: base(configuration, httpEncoder, dbContext, cacheManager, logger, httpContext)
{
2019-06-30 22:14:36 +00:00
BookmarkService = bookmarkService;
2018-12-09 17:58:31 +00:00
}
2018-12-30 20:57:06 +00:00
public async Task<OperationResult<PlaylistList>> AddNewPlaylist(User user, Playlist model)
{
var playlist = new data.Playlist
{
IsPublic = model.IsPublic,
Description = model.Description,
Name = model.Name,
UserId = user.Id
};
2019-06-30 22:14:36 +00:00
DbContext.Playlists.Add(playlist);
await DbContext.SaveChangesAsync();
var r = await AddTracksToPlaylist(playlist,
model.Tracks.OrderBy(x => x.ListNumber).Select(x => x.Track.Id));
2018-12-30 20:57:06 +00:00
var request = new PagedRequest
{
FilterToPlaylistId = playlist.RoadieId
};
2019-06-30 22:14:36 +00:00
var result = await List(request, user);
2018-12-30 20:57:06 +00:00
return new OperationResult<PlaylistList>
{
Data = result.Rows.First(),
IsSuccess = true
};
}
public async Task<OperationResult<bool>> AddTracksToPlaylist(data.Playlist playlist, IEnumerable<Guid> trackIds)
{
var sw = new Stopwatch();
sw.Start();
var result = false;
var now = DateTime.UtcNow;
2019-06-30 22:14:36 +00:00
var existingTracksForPlaylist = from plt in DbContext.PlaylistTracks
join t in DbContext.Tracks on plt.TrackId equals t.Id
where plt.PlayListId == playlist.Id
select t;
var newTracksForPlaylist = (from t in DbContext.Tracks
2018-12-30 20:57:06 +00:00
where (from x in trackIds select x).Contains(t.RoadieId)
where !(from x in existingTracksForPlaylist select x.RoadieId).Contains(t.RoadieId)
select t).ToArray();
foreach (var newTrackForPlaylist in newTracksForPlaylist)
2019-06-30 22:14:36 +00:00
DbContext.PlaylistTracks.Add(new data.PlaylistTrack
2018-12-30 20:57:06 +00:00
{
TrackId = newTrackForPlaylist.Id,
PlayListId = playlist.Id
});
playlist.LastUpdated = now;
2019-06-30 22:14:36 +00:00
await DbContext.SaveChangesAsync();
2018-12-30 20:57:06 +00:00
result = true;
2019-06-30 22:14:36 +00:00
var r = await ReorderPlaylist(playlist);
2018-12-30 20:57:06 +00:00
result = result && r.IsSuccess;
2019-06-30 22:14:36 +00:00
await UpdatePlaylistCounts(playlist.Id, now);
2018-12-30 20:57:06 +00:00
2018-12-31 05:16:42 +00:00
sw.Stop();
2018-12-30 20:57:06 +00:00
return new OperationResult<bool>
{
IsSuccess = result,
2018-12-31 05:16:42 +00:00
Data = result,
OperationTime = sw.ElapsedMilliseconds
};
}
2018-12-09 17:58:31 +00:00
public async Task<OperationResult<Playlist>> ById(User roadieUser, Guid id, IEnumerable<string> includes = null)
{
var sw = Stopwatch.StartNew();
sw.Start();
2019-06-30 22:14:36 +00:00
var cacheKey = string.Format("urn:playlist_by_id_operation:{0}:{1}", id,
includes == null ? "0" : string.Join("|", includes));
var result = await CacheManager.GetAsync(cacheKey, async () =>
2019-08-02 20:59:24 +00:00
{
return await PlaylistByIdAction(id, includes);
}, data.Artist.CacheRegionUrn(id));
2018-12-09 17:58:31 +00:00
sw.Stop();
if (result?.Data != null && roadieUser != null)
{
2019-01-02 02:03:17 +00:00
if (result?.Data?.Tracks != null)
{
2019-06-30 22:14:36 +00:00
var user = GetUser(roadieUser.UserId);
2019-01-02 02:03:17 +00:00
foreach (var track in result.Data.Tracks)
2019-08-02 20:59:24 +00:00
{
2019-08-04 16:30:19 +00:00
track.Track.TrackPlayUrl = MakeTrackPlayUrl(user, HttpContext.BaseUrl, track.Track.DatabaseId, track.Track.Id);
2019-08-02 20:59:24 +00:00
}
2019-01-02 02:03:17 +00:00
}
2018-12-09 17:58:31 +00:00
result.Data.UserCanEdit = result.Data.Maintainer.Id == roadieUser.UserId || roadieUser.IsAdmin;
2019-08-02 20:59:24 +00:00
var userBookmarkResult = await BookmarkService.List(roadieUser, new PagedRequest(), false, BookmarkType.Playlist);
2018-12-09 17:58:31 +00:00
if (userBookmarkResult.IsSuccess)
2019-08-02 20:59:24 +00:00
{
result.Data.UserBookmarked = userBookmarkResult?.Rows?.FirstOrDefault(x => x.Bookmark.Text == result.Data.Id.ToString()) != null;
}
2019-06-28 21:24:32 +00:00
if (result.Data.Comments.Any())
{
var commentIds = result.Data.Comments.Select(x => x.DatabaseId).ToArray();
2019-06-30 22:14:36 +00:00
var userCommentReactions = (from cr in DbContext.CommentReactions
2019-06-28 21:24:32 +00:00
where commentIds.Contains(cr.CommentId)
where cr.UserId == roadieUser.Id
select cr).ToArray();
foreach (var comment in result.Data.Comments)
{
2019-08-02 20:59:24 +00:00
var userCommentReaction = userCommentReactions.FirstOrDefault(x => x.CommentId == comment.DatabaseId);
2019-06-28 21:24:32 +00:00
comment.IsDisliked = userCommentReaction?.ReactionValue == CommentReaction.Dislike;
comment.IsLiked = userCommentReaction?.ReactionValue == CommentReaction.Like;
}
}
2018-12-09 17:58:31 +00:00
}
2019-06-30 22:14:36 +00:00
2018-12-09 17:58:31 +00:00
return new OperationResult<Playlist>(result.Messages)
{
Data = result?.Data,
IsNotFoundResult = result?.IsNotFoundResult ?? false,
Errors = result?.Errors,
IsSuccess = result?.IsSuccess ?? false,
OperationTime = sw.ElapsedMilliseconds
};
}
2019-01-08 22:40:26 +00:00
public async Task<OperationResult<bool>> DeletePlaylist(User user, Guid id)
{
var sw = new Stopwatch();
sw.Start();
2019-06-30 22:14:36 +00:00
var playlist = DbContext.Playlists.FirstOrDefault(x => x.RoadieId == id);
if (playlist == null)
{
return new OperationResult<bool>(true, string.Format("Playlist Not Found [{0}]", id));
}
2019-03-01 04:10:44 +00:00
if (!user.IsAdmin && user.Id != playlist.UserId)
2019-01-08 22:40:26 +00:00
{
2019-06-30 22:14:36 +00:00
Logger.LogWarning("User `{0}` attempted to delete Playlist `{1}`", user, playlist);
2019-01-08 22:40:26 +00:00
return new OperationResult<bool>("Access Denied")
{
IsAccessDeniedResult = true
};
}
2019-06-30 22:14:36 +00:00
DbContext.Playlists.Remove(playlist);
await DbContext.SaveChangesAsync();
var playlistImageFilename = playlist.PathToImage(Configuration);
if (File.Exists(playlistImageFilename))
{
File.Delete(playlistImageFilename);
}
Logger.LogWarning("User `{0}` deleted Playlist `{1}]`", user, playlist);
2019-06-30 22:14:36 +00:00
CacheManager.ClearRegion(playlist.CacheRegion);
2019-01-08 22:40:26 +00:00
sw.Stop();
return new OperationResult<bool>
{
IsSuccess = true,
Data = true,
OperationTime = sw.ElapsedMilliseconds
};
}
public Task<Library.Models.Pagination.PagedResult<PlaylistList>> List(PagedRequest request, User roadieUser = null)
2018-12-30 20:57:06 +00:00
{
var sw = new Stopwatch();
sw.Start();
2019-06-30 22:14:36 +00:00
var playlistWithArtistTrackIds = new int[0];
2018-12-30 20:57:06 +00:00
if (request.FilterToArtistId.HasValue)
{
2019-06-30 22:14:36 +00:00
playlistWithArtistTrackIds = (from pl in DbContext.Playlists
join pltr in DbContext.PlaylistTracks on pl.Id equals pltr.PlayListId
join t in DbContext.Tracks on pltr.TrackId equals t.Id
join rm in DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
join r in DbContext.Releases on rm.ReleaseId equals r.Id
join a in DbContext.Artists on r.ArtistId equals a.Id
2018-12-30 20:57:06 +00:00
where a.RoadieId == request.FilterToArtistId
select pl.Id
).ToArray();
}
2019-06-30 22:14:36 +00:00
var playlistReleaseTrackIds = new int[0];
2018-12-30 20:57:06 +00:00
if (request.FilterToReleaseId.HasValue)
{
2019-06-30 22:14:36 +00:00
playlistReleaseTrackIds = (from pl in DbContext.Playlists
join pltr in DbContext.PlaylistTracks on pl.Id equals pltr.PlayListId
join t in DbContext.Tracks on pltr.TrackId equals t.Id
join rm in DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
join r in DbContext.Releases on rm.ReleaseId equals r.Id
2018-12-30 20:57:06 +00:00
where r.RoadieId == request.FilterToReleaseId
select pl.Id
).ToArray();
}
2019-06-30 22:14:36 +00:00
var result = from pl in DbContext.Playlists
join u in DbContext.Users on pl.UserId equals u.Id
where request.FilterToPlaylistId == null || pl.RoadieId == request.FilterToPlaylistId
where request.FilterToArtistId == null || playlistWithArtistTrackIds.Contains(pl.Id)
where request.FilterToReleaseId == null || playlistReleaseTrackIds.Contains(pl.Id)
where roadieUser == null && pl.IsPublic || roadieUser != null && u.RoadieId == roadieUser.UserId || pl.IsPublic
where request.FilterValue.Length == 0 || request.FilterValue.Length > 0 && pl.Name != null && pl.Name.Contains(request.FilterValue)
select new PlaylistList
{
Playlist = new DataToken
{
Text = pl.Name,
Value = pl.RoadieId.ToString()
},
User = new DataToken
{
Text = u.UserName,
Value = u.RoadieId.ToString()
},
PlaylistCount = pl.TrackCount,
IsPublic = pl.IsPublic,
Duration = pl.Duration,
TrackCount = pl.TrackCount,
CreatedDate = pl.CreatedDate,
LastUpdated = pl.LastUpdated,
UserThumbnail = MakeUserThumbnailImage(Configuration, HttpContext, u.RoadieId),
Id = pl.RoadieId,
Thumbnail = MakePlaylistThumbnailImage(Configuration, HttpContext, pl.RoadieId)
};
2019-06-30 22:14:36 +00:00
var sortBy = string.IsNullOrEmpty(request.Sort)
? request.OrderValue(new Dictionary<string, string> { { "Playlist.Text", "ASC" } })
: request.OrderValue();
2018-12-30 20:57:06 +00:00
var rowCount = result.Count();
var rows = result
.OrderBy(sortBy)
.Skip(request.SkipValue)
.Take(request.LimitValue)
.ToArray();
2018-12-30 20:57:06 +00:00
sw.Stop();
return Task.FromResult(new Library.Models.Pagination.PagedResult<PlaylistList>
{
TotalCount = rowCount,
CurrentPage = request.PageValue,
TotalPages = (int)Math.Ceiling((double)rowCount / request.LimitValue),
OperationTime = sw.ElapsedMilliseconds,
Rows = rows
});
}
public async Task<OperationResult<bool>> ReorderPlaylist(data.Playlist playlist)
{
var sw = new Stopwatch();
sw.Start();
var result = false;
var now = DateTime.UtcNow;
if (playlist != null)
{
var looper = 0;
2019-06-30 22:14:36 +00:00
foreach (var playlistTrack in DbContext.PlaylistTracks.Where(x => x.PlayListId == playlist.Id)
.OrderBy(x => x.CreatedDate))
2018-12-30 20:57:06 +00:00
{
looper++;
playlistTrack.ListNumber = looper;
playlistTrack.LastUpdated = now;
}
2019-06-30 22:14:36 +00:00
await DbContext.SaveChangesAsync();
2018-12-30 20:57:06 +00:00
result = true;
}
return new OperationResult<bool>
{
IsSuccess = result,
Data = result
};
}
2019-05-29 22:25:40 +00:00
public async Task<OperationResult<bool>> UpdatePlaylist(User user, Playlist model)
{
var sw = new Stopwatch();
sw.Start();
var errors = new List<Exception>();
2019-06-30 22:14:36 +00:00
var playlist = DbContext.Playlists.FirstOrDefault(x => x.RoadieId == model.Id);
2019-05-29 22:25:40 +00:00
if (playlist == null)
{
return new OperationResult<bool>(true, string.Format("Playlist Not Found [{0}]", model.Id));
}
if (!user.IsAdmin && user.Id != playlist.UserId)
{
Logger.LogWarning("User `{0}` attempted to update Playlist `{1}`", user, playlist);
return new OperationResult<bool>("Access Denied")
{
IsAccessDeniedResult = true
};
}
2019-05-29 22:25:40 +00:00
try
{
var now = DateTime.UtcNow;
playlist.AlternateNames = model.AlternateNamesList.ToDelimitedList();
playlist.Description = model.Description;
playlist.IsLocked = model.IsLocked;
playlist.IsPublic = model.IsPublic;
2019-07-31 17:02:15 +00:00
var oldPathToImage = playlist.PathToImage(Configuration);
var didChangeName = playlist.Name != model.Name;
2019-05-29 22:25:40 +00:00
playlist.Name = model.Name;
playlist.Status = SafeParser.ToEnum<Statuses>(model.Status);
playlist.Tags = model.TagsList.ToDelimitedList();
playlist.URLs = model.URLsList.ToDelimitedList();
2019-07-31 17:02:15 +00:00
if (didChangeName)
{
if (File.Exists(oldPathToImage))
{
File.Move(oldPathToImage, playlist.PathToImage(Configuration));
}
}
2019-05-29 22:25:40 +00:00
var playlistImage = ImageHelper.ImageDataFromUrl(model.NewThumbnailData);
if (playlistImage != null)
{
// Save unaltered playlist image
2019-11-10 14:48:07 +00:00
File.WriteAllBytes(playlist.PathToImage(Configuration, true), ImageHelper.ConvertToJpegFormat(playlistImage));
2019-05-29 22:25:40 +00:00
}
playlist.LastUpdated = now;
2019-06-30 22:14:36 +00:00
await DbContext.SaveChangesAsync();
2019-05-29 22:25:40 +00:00
2019-06-30 22:14:36 +00:00
CacheManager.ClearRegion(playlist.CacheRegion);
Logger.LogInformation($"UpdatePlaylist `{playlist}` By User `{user}`");
2019-05-29 22:25:40 +00:00
}
catch (Exception ex)
{
2019-06-30 22:14:36 +00:00
Logger.LogError(ex);
2019-05-29 22:25:40 +00:00
errors.Add(ex);
}
2019-06-30 22:14:36 +00:00
2019-05-29 22:25:40 +00:00
sw.Stop();
return new OperationResult<bool>
{
IsSuccess = !errors.Any(),
Data = !errors.Any(),
OperationTime = sw.ElapsedMilliseconds,
Errors = errors
};
}
public async Task<OperationResult<bool>> UpdatePlaylistTracks(User user, PlaylistTrackModifyRequest request)
{
var sw = new Stopwatch();
sw.Start();
var errors = new List<Exception>();
2019-06-30 22:14:36 +00:00
var playlist = DbContext.Playlists.Include(x => x.Tracks).FirstOrDefault(x => x.RoadieId == request.Id);
2019-05-29 22:25:40 +00:00
if (playlist == null)
{
2019-05-29 22:25:40 +00:00
return new OperationResult<bool>(true, string.Format("Label Not Found [{0}]", request.Id));
}
if (!user.IsAdmin && user.Id != playlist.UserId)
{
Logger.LogWarning("User `{0}` attempted to update Playlist Tracks `{1}`", user, playlist);
return new OperationResult<bool>("Access Denied")
{
IsAccessDeniedResult = true
};
}
2019-05-29 22:25:40 +00:00
try
{
var now = DateTime.UtcNow;
playlist.Tracks.Clear();
2019-06-30 22:14:36 +00:00
var tracks = (from t in DbContext.Tracks
2019-05-29 22:25:40 +00:00
join plt in request.Tracks on t.RoadieId equals plt.Track.Id
select t).ToArray();
foreach (var newPlaylistTrack in request.Tracks.OrderBy(x => x.ListNumber))
{
var track = tracks.FirstOrDefault(x => x.RoadieId == newPlaylistTrack.Track.Id);
playlist.Tracks.Add(new data.PlaylistTrack
{
ListNumber = newPlaylistTrack.ListNumber,
PlayListId = playlist.Id,
CreatedDate = now,
TrackId = track.Id
});
}
2019-06-30 22:14:36 +00:00
2019-05-29 22:25:40 +00:00
playlist.LastUpdated = now;
2019-06-30 22:14:36 +00:00
await DbContext.SaveChangesAsync();
2019-05-29 22:25:40 +00:00
// await base.UpdatePlaylistCounts(playlist.Id, now);
2019-06-30 22:14:36 +00:00
Logger.LogInformation($"UpdatePlaylistTracks `{playlist}` By User `{user}`");
2019-05-29 22:25:40 +00:00
}
catch (Exception ex)
{
2019-06-30 22:14:36 +00:00
Logger.LogError(ex);
2019-05-29 22:25:40 +00:00
errors.Add(ex);
}
2019-06-30 22:14:36 +00:00
2019-05-29 22:25:40 +00:00
sw.Stop();
return new OperationResult<bool>
{
IsSuccess = !errors.Any(),
Data = !errors.Any(),
OperationTime = sw.ElapsedMilliseconds,
Errors = errors
};
}
2018-12-24 19:40:49 +00:00
private Task<OperationResult<Playlist>> PlaylistByIdAction(Guid id, IEnumerable<string> includes = null)
2018-12-09 17:58:31 +00:00
{
2019-08-04 16:30:19 +00:00
var timings = new Dictionary<string, long>();
var tsw = new Stopwatch();
2018-12-09 17:58:31 +00:00
var sw = Stopwatch.StartNew();
sw.Start();
2019-08-04 16:30:19 +00:00
tsw.Restart();
2019-06-30 22:14:36 +00:00
var playlist = GetPlaylist(id);
2019-08-04 16:30:19 +00:00
tsw.Stop();
timings.Add("getPlaylist", tsw.ElapsedMilliseconds);
2018-12-09 17:58:31 +00:00
if (playlist == null)
2019-08-02 20:59:24 +00:00
{
return Task.FromResult(new OperationResult<Playlist>(true, string.Format("Playlist Not Found [{0}]", id)));
}
2019-08-04 16:30:19 +00:00
tsw.Restart();
2018-12-09 17:58:31 +00:00
var result = playlist.Adapt<Playlist>();
result.AlternateNames = playlist.AlternateNames;
result.Tags = playlist.Tags;
result.URLs = playlist.URLs;
2019-08-02 20:59:24 +00:00
var maintainer = DbContext.Users.Include(x => x.UserRoles).Include("UserRoles.Role").FirstOrDefault(x => x.Id == playlist.UserId);
result.Maintainer = UserList.FromDataUser(maintainer, MakeUserThumbnailImage(Configuration, HttpContext, maintainer.RoadieId));
result.Thumbnail = MakePlaylistThumbnailImage(Configuration, HttpContext, playlist.RoadieId);
result.MediumThumbnail = MakeThumbnailImage(Configuration, HttpContext, id, "playlist", Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
2019-08-04 16:30:19 +00:00
tsw.Stop();
timings.Add("adapt", tsw.ElapsedMilliseconds);
2018-12-09 17:58:31 +00:00
if (includes != null && includes.Any())
{
2019-06-30 22:14:36 +00:00
var playlistTracks = (from pl in DbContext.Playlists
join pltr in DbContext.PlaylistTracks on pl.Id equals pltr.PlayListId
join t in DbContext.Tracks on pltr.TrackId equals t.Id
2018-12-09 17:58:31 +00:00
where pl.Id == playlist.Id
2019-03-01 04:10:44 +00:00
select new { t, pltr }).ToArray();
2018-12-09 17:58:31 +00:00
if (includes.Contains("stats"))
2019-08-02 20:59:24 +00:00
{
2019-08-04 16:30:19 +00:00
tsw.Restart();
2019-06-30 22:14:36 +00:00
result.Statistics = new ReleaseGroupingStatistics
2018-12-09 17:58:31 +00:00
{
ReleaseCount = result.ReleaseCount,
TrackCount = result.TrackCount,
TrackSize = result.DurationTime,
FileSize = playlistTracks.Sum(x => (long?)x.t.FileSize).ToFileSize()
};
2019-08-04 16:30:19 +00:00
tsw.Stop();
timings.Add("stats", tsw.ElapsedMilliseconds);
2019-08-02 20:59:24 +00:00
}
2018-12-09 17:58:31 +00:00
if (includes.Contains("tracks"))
2019-08-02 20:59:24 +00:00
{
2019-08-04 16:30:19 +00:00
tsw.Restart();
2018-12-09 17:58:31 +00:00
result.Tracks = (from plt in playlistTracks
2019-06-30 22:14:36 +00:00
join rm in DbContext.ReleaseMedias on plt.t.ReleaseMediaId equals rm.Id
join r in DbContext.Releases on rm.ReleaseId equals r.Id
join releaseArtist in DbContext.Artists on r.ArtistId equals releaseArtist.Id
join trackArtist in DbContext.Artists on plt.t.ArtistId equals trackArtist.Id into tas
2018-12-30 20:57:06 +00:00
from trackArtist in tas.DefaultIfEmpty()
select new PlaylistTrack
{
ListNumber = plt.pltr.ListNumber,
2019-01-02 02:03:17 +00:00
Track = TrackList.FromDataTrack(null,
2019-06-30 22:14:36 +00:00
plt.t,
rm.MediaNumber,
r,
releaseArtist,
trackArtist,
HttpContext.BaseUrl,
MakeTrackThumbnailImage(Configuration, HttpContext, plt.t.RoadieId),
MakeReleaseThumbnailImage(Configuration, HttpContext, r.RoadieId),
MakeArtistThumbnailImage(Configuration, HttpContext, releaseArtist.RoadieId),
MakeArtistThumbnailImage(Configuration, HttpContext, trackArtist == null ? null : (Guid?)trackArtist.RoadieId))
2018-12-30 20:57:06 +00:00
}).ToArray();
2019-08-04 16:30:19 +00:00
tsw.Stop();
timings.Add("tracks", tsw.ElapsedMilliseconds);
2019-08-02 20:59:24 +00:00
}
2019-06-28 21:24:32 +00:00
if (includes.Contains("comments"))
{
2019-08-04 16:30:19 +00:00
tsw.Restart();
2019-06-30 22:14:36 +00:00
var playlistComments = DbContext.Comments.Include(x => x.User)
2019-08-02 20:59:24 +00:00
.Where(x => x.PlaylistId == playlist.Id)
.OrderByDescending(x => x.CreatedDate)
.ToArray();
2019-06-28 21:24:32 +00:00
if (playlistComments.Any())
{
var comments = new List<Comment>();
var commentIds = playlistComments.Select(x => x.Id).ToArray();
2019-06-30 22:14:36 +00:00
var userCommentReactions = (from cr in DbContext.CommentReactions
2019-06-28 21:24:32 +00:00
where commentIds.Contains(cr.CommentId)
select cr).ToArray();
foreach (var playlistComment in playlistComments)
{
var comment = playlistComment.Adapt<Comment>();
comment.DatabaseId = playlistComment.Id;
comment.User = UserList.FromDataUser(playlistComment.User, MakeUserThumbnailImage(Configuration, HttpContext, playlistComment.User.RoadieId));
2019-08-02 20:59:24 +00:00
comment.DislikedCount = userCommentReactions.Count(x => x.CommentId == playlistComment.Id && x.ReactionValue == CommentReaction.Dislike);
comment.LikedCount = userCommentReactions.Count(x => x.CommentId == playlistComment.Id && x.ReactionValue == CommentReaction.Like);
2019-06-28 21:24:32 +00:00
comments.Add(comment);
}
result.Comments = comments;
}
2019-08-04 16:30:19 +00:00
tsw.Stop();
timings.Add("comments", tsw.ElapsedMilliseconds);
2019-06-28 21:24:32 +00:00
}
2018-12-09 17:58:31 +00:00
}
sw.Stop();
2019-08-04 16:30:19 +00:00
Logger.LogInformation($"ByIdAction: Playlist `{ playlist }`: includes [{includes.ToCSV()}], timings: [{ timings.ToTimings() }]");
2018-12-24 19:40:49 +00:00
return Task.FromResult(new OperationResult<Playlist>
2018-12-09 17:58:31 +00:00
{
Data = result,
IsSuccess = result != null,
OperationTime = sw.ElapsedMilliseconds
2018-12-24 19:40:49 +00:00
});
2018-11-07 04:33:22 +00:00
}
}
}