roadie/Roadie.Api.Services/BookmarkService.cs

193 lines
9.4 KiB
C#
Raw Normal View History

2018-12-11 23:09:52 +00:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Roadie.Library.Caching;
using Roadie.Library.Configuration;
using Roadie.Library.Encoding;
using Roadie.Library.Enums;
2019-06-30 22:14:36 +00:00
using Roadie.Library.Models.Collections;
using Roadie.Library.Models.Pagination;
2019-06-30 22:14:36 +00:00
using Roadie.Library.Models.Playlists;
using Roadie.Library.Models.Releases;
using Roadie.Library.Models.Users;
using Roadie.Library.Utility;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using data = Roadie.Library.Data;
2018-12-11 23:09:52 +00:00
using models = Roadie.Library.Models;
namespace Roadie.Api.Services
{
public class BookmarkService : ServiceBase, IBookmarkService
{
public BookmarkService(IRoadieSettings configuration,
2019-06-30 22:14:36 +00:00
IHttpEncoder httpEncoder,
IHttpContext httpContext,
data.IRoadieDbContext context,
ICacheManager cacheManager,
ILogger<BookmarkService> logger)
: base(configuration, httpEncoder, context, cacheManager, logger, httpContext)
{
}
2019-06-30 22:14:36 +00:00
public Task<Library.Models.Pagination.PagedResult<models.BookmarkList>> List(User roadieUser,
PagedRequest request, bool? doRandomize = false, BookmarkType? filterType = null)
{
var sw = new Stopwatch();
sw.Start();
2019-06-30 22:14:36 +00:00
var result = from b in DbContext.Bookmarks
join u in DbContext.Users on b.UserId equals u.Id
where b.UserId == roadieUser.Id
where filterType == null || b.BookmarkType == filterType
select new models.BookmarkList
{
Comment = b.Comment,
Position = b.Position,
User = new models.DataToken
{
Text = u.UserName,
Value = u.RoadieId.ToString()
},
DatabaseId = b.Id,
Id = b.RoadieId,
CreatedDate = b.CreatedDate,
LastUpdated = b.LastUpdated,
Type = b.BookmarkType,
BookmarkTargetId = b.BookmarkTargetId
};
2019-06-30 22:14:36 +00:00
var sortBy = string.IsNullOrEmpty(request.Sort)
? request.OrderValue(new Dictionary<string, string> { { "CreatedDate", "DESC" } })
: request.OrderValue();
var rowCount = result.Count();
2019-06-30 22:14:36 +00:00
var rows = result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArray();
2019-06-30 22:14:36 +00:00
var user = GetUser(roadieUser.UserId);
2019-01-02 02:03:17 +00:00
foreach (var row in rows)
switch (row.Type)
{
case BookmarkType.Artist:
2019-06-30 22:14:36 +00:00
var artist = DbContext.Artists.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
if (artist == null) continue;
row.Bookmark = new models.DataToken
{
2018-12-05 16:04:21 +00:00
Text = artist.Name,
Value = artist.RoadieId.ToString()
};
2019-06-30 22:14:36 +00:00
row.Artist =
models.ArtistList.FromDataArtist(artist, MakeArtistThumbnailImage(artist.RoadieId));
row.Thumbnail = MakeArtistThumbnailImage(artist.RoadieId);
2018-12-05 16:04:21 +00:00
row.SortName = artist.SortName ?? artist.Name;
break;
2018-11-16 03:37:00 +00:00
case BookmarkType.Release:
2019-06-30 22:14:36 +00:00
var release = DbContext.Releases.Include(x => x.Artist)
.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
if (release == null) continue;
row.Bookmark = new models.DataToken
{
2018-12-05 16:04:21 +00:00
Text = release.Title,
Value = release.RoadieId.ToString()
};
2019-06-30 22:14:36 +00:00
row.Release = ReleaseList.FromDataRelease(release, release.Artist, HttpContext.BaseUrl,
MakeArtistThumbnailImage(release.Artist.RoadieId),
MakeReleaseThumbnailImage(release.RoadieId));
row.Thumbnail = MakeReleaseThumbnailImage(release.RoadieId);
2018-12-05 16:04:21 +00:00
row.SortName = release.Title;
break;
2018-11-16 03:37:00 +00:00
case BookmarkType.Track:
2019-06-30 22:14:36 +00:00
var track = DbContext.Tracks
.Include(x => x.ReleaseMedia)
.Include(x => x.ReleaseMedia.Release)
.Include(x => x.ReleaseMedia.Release.Artist)
.Include(x => x.TrackArtist)
.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
if (track == null) continue;
row.Bookmark = new models.DataToken
{
2018-12-05 16:04:21 +00:00
Text = track.Title,
Value = track.RoadieId.ToString()
};
2019-08-04 16:30:19 +00:00
row.Track = models.TrackList.FromDataTrack(MakeTrackPlayUrl(user, HttpContext.BaseUrl, track.Id, track.RoadieId),
2019-06-30 22:14:36 +00:00
track,
track.ReleaseMedia.MediaNumber,
track.ReleaseMedia.Release,
track.ReleaseMedia.Release.Artist,
track.TrackArtist,
HttpContext.BaseUrl,
MakeTrackThumbnailImage(track.RoadieId),
MakeReleaseThumbnailImage(track.ReleaseMedia.Release.RoadieId),
MakeArtistThumbnailImage(track.ReleaseMedia.Release.Artist.RoadieId),
MakeArtistThumbnailImage(track.TrackArtist == null
? null
: (Guid?)track.TrackArtist.RoadieId));
2019-08-04 16:30:19 +00:00
row.Track.TrackPlayUrl = MakeTrackPlayUrl(user, HttpContext.BaseUrl, track.Id, track.RoadieId);
2019-06-30 22:14:36 +00:00
row.Thumbnail = MakeTrackThumbnailImage(track.RoadieId);
2018-12-05 16:04:21 +00:00
row.SortName = track.Title;
break;
2018-11-16 03:37:00 +00:00
case BookmarkType.Playlist:
2019-06-30 22:14:36 +00:00
var playlist = DbContext.Playlists
.Include(x => x.User)
.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
if (playlist == null) continue;
row.Bookmark = new models.DataToken
{
2018-12-05 16:04:21 +00:00
Text = playlist.Name,
Value = playlist.RoadieId.ToString()
};
2019-06-30 22:14:36 +00:00
row.Playlist = PlaylistList.FromDataPlaylist(playlist, playlist.User,
MakePlaylistThumbnailImage(playlist.RoadieId),
MakeUserThumbnailImage(playlist.User.RoadieId));
row.Thumbnail = MakePlaylistThumbnailImage(playlist.RoadieId);
2018-12-05 16:04:21 +00:00
row.SortName = playlist.Name;
break;
2018-11-16 03:37:00 +00:00
case BookmarkType.Collection:
2019-06-30 22:14:36 +00:00
var collection = DbContext.Collections.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
if (collection == null) continue;
row.Bookmark = new models.DataToken
{
2018-12-05 16:04:21 +00:00
Text = collection.Name,
Value = collection.RoadieId.ToString()
};
2019-06-30 22:14:36 +00:00
row.Collection = CollectionList.FromDataCollection(collection,
(from crc in DbContext.CollectionReleases
where crc.CollectionId == collection.Id
select crc.Id).Count(), MakeCollectionThumbnailImage(collection.RoadieId));
row.Thumbnail = MakeCollectionThumbnailImage(collection.RoadieId);
2018-12-05 16:04:21 +00:00
row.SortName = collection.SortName ?? collection.Name;
break;
2018-11-16 03:37:00 +00:00
case BookmarkType.Label:
2019-06-30 22:14:36 +00:00
var label = DbContext.Labels.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
if (label == null) continue;
row.Bookmark = new models.DataToken
{
2018-12-05 16:04:21 +00:00
Text = label.Name,
Value = label.RoadieId.ToString()
};
2019-06-30 22:14:36 +00:00
row.Label = models.LabelList.FromDataLabel(label, MakeLabelThumbnailImage(label.RoadieId));
row.Thumbnail = MakeLabelThumbnailImage(label.RoadieId);
2018-12-05 16:04:21 +00:00
row.SortName = label.SortName ?? label.Name;
break;
}
2019-06-30 22:14:36 +00:00
;
sw.Stop();
2019-06-30 22:14:36 +00:00
return Task.FromResult(new Library.Models.Pagination.PagedResult<models.BookmarkList>
2018-12-24 19:40:49 +00:00
{
TotalCount = rowCount,
CurrentPage = request.PageValue,
TotalPages = (int)Math.Ceiling((double)rowCount / request.LimitValue),
OperationTime = sw.ElapsedMilliseconds,
Rows = rows
2018-12-24 19:40:49 +00:00
});
}
}
2018-11-16 03:37:00 +00:00
}